|
| 1 | +# Ntfy Binding |
| 2 | + |
| 3 | +The Ntfy binding enables openHAB to publish notifications to Ntfy-compatible servers (for example [ntfy.sh](https://ntfy.sh) or a self-hosted ntfy-compatible endpoint). |
| 4 | + |
| 5 | +Ntfy is a simple HTTP-based notification service and message broker; see [ntfy.sh](https://ntfy.sh) for details and public servers. |
| 6 | + |
| 7 | +It is intended for integrations where openHAB should push alert or informational messages to mobile or desktop clients that support the Ntfy protocol. The binding supports basic text messages as well as common rich features supported by the protocol: message priority, tags, icon URLs, attachments, click actions and simple action buttons. |
| 8 | + |
| 9 | +Typical uses include doorbell alerts, security notifications, system health messages, or any automation that should notify users in real time via an Ntfy-compatible notification channel. |
| 10 | + |
| 11 | +## Supported Things |
| 12 | + |
| 13 | +This binding provides the following Thing types. The Thing type IDs match the definitions in `src/main/resources/OH-INF/thing/thing-types.xml`. |
| 14 | + |
| 15 | +- `ntfy:ntfyConnection` (bridge) — Represents a connection to an Ntfy server. Configure the bridge with the server hostname (for example `https://ntfy.sh`) and optional credentials (username/password). The bridge holds shared connection settings (hostname, username, password, connectionTimeout) which are used by topic Things. |
| 16 | +- `ntfy:ntfyTopic` (Thing) — Represents a topic/channel on a configured Ntfy server. Each topic Thing must be associated with an `ntfyConnection` bridge and requires the `topicname` configuration parameter. |
| 17 | + |
| 18 | +## Thing Configuration |
| 19 | + |
| 20 | +### `ntfyConnection` Bridge Configuration |
| 21 | + |
| 22 | +| Name | Type | Description | Default | Required | Advanced | |
| 23 | +|-------------------|---------|-----------------------------------------|-----------------|----------|----------| |
| 24 | +| hostname | text | Hostname or base URL of the ntfy server | [https://ntfy.sh](https://ntfy.sh) | yes | no | |
| 25 | +| username | text | Optional username for basic auth | N/A | no | no | |
| 26 | +| password | text | Optional password for basic auth | N/A | no | no | |
| 27 | +| connectionTimeout | integer | WebSocket / HTTP connection timeout ms | 60000 | no | yes | |
| 28 | + |
| 29 | +Configure the `ntfyConnection` as a bridge to hold shared server and authentication settings. Topic Things reference the bridge to reuse these settings. |
| 30 | + |
| 31 | +### `ntfyTopic` Thing Configuration |
| 32 | + |
| 33 | +| Name | Type | Description | Default | Required | Advanced | |
| 34 | +|-----------|------|---------------------------|---------|----------|----------| |
| 35 | +| topicname | text | Name of the topic/channel | N/A | yes | no | |
| 36 | + |
| 37 | +## Channels |
| 38 | + |
| 39 | +### `ntfyTopic` Channels |
| 40 | + |
| 41 | +| Channel | Type | Read/Write | Description | |
| 42 | +|-------------|----------|------------|---------------------------------------------| |
| 43 | +| lastMessage | String | R | Last received message payload (read-only) | |
| 44 | +| messageTime | DateTime | R | Timestamp of the last message (read-only) | |
| 45 | + |
| 46 | +## Full Example |
| 47 | + |
| 48 | +Below are examples for textual configuration files showing a bridge (`ntfyConnection`), a topic Thing (`ntfyTopic`), Items bound to the Thing's channels, and a simple sitemap to display the last message and its timestamp. |
| 49 | + |
| 50 | +### Thing Configuration |
| 51 | + |
| 52 | +```things |
| 53 | +Bridge ntfy:ntfyConnection:myConn "Ntfy Server" [ hostname="https://ntfy.sh", connectionTimeout=60000 ] |
| 54 | +
|
| 55 | +Thing ntfy:ntfyTopic:home "Front Door Notifications" (ntfy:ntfyConnection:myConn) [ topicname="home" ] |
| 56 | +``` |
| 57 | + |
| 58 | +If your server requires authentication, supply `username` and `password` in the bridge configuration. The `topicname` must be provided for each `ntfyTopic` Thing. |
| 59 | + |
| 60 | +### Item Configuration (items file) |
| 61 | + |
| 62 | +Bind Items to the read-only channels exposed by the topic Thing to show the last received message and its timestamp: |
| 63 | + |
| 64 | +```items |
| 65 | +String FrontDoorLastMessage "Front Door Message" { channel="ntfy:ntfyTopic:home:lastMessage" } |
| 66 | +DateTime FrontDoorMessageTime "Last message received" { channel="ntfy:ntfyTopic:home:messageTime" } |
| 67 | +``` |
| 68 | + |
| 69 | +Note: Sending notifications from openHAB to ntfy is done via the binding's Actions from rules (the binding exposes Actions to publish/delete messages). The channels above are read-only and show incoming or last-state values. |
| 70 | + |
| 71 | +### Sitemap Configuration (sitemap file) |
| 72 | + |
| 73 | +Simple sitemap demonstrating how to display the last message and its timestamp: |
| 74 | + |
| 75 | +```sitemap |
| 76 | + sitemap notifications label="Notifications" |
| 77 | +{ |
| 78 | + <Frame label="Front Door"> |
| 79 | + Text item=FrontDoorLastMessage label="Last message [%s]" |
| 80 | + Text item=FrontDoorMessageTime label="Received [%1$td.%1$tm.%1$tY %1$tR]" |
| 81 | + </Frame> |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +## Actions (Rules DSL and JavaScript) |
| 86 | + |
| 87 | +This binding exposes Rule Actions to send and manage messages on a configured topic. Actions are available for use from the Rules DSL and the ECMAScript/JavaScript automation scripts. The actions support a builder-style API to configure a message (message text, priority, tags, icon, attachments, actions, sequence id, ...) and then send it. |
| 88 | + |
| 89 | +Important: Always check that the returned actions object is not null (the Thing must exist and be handled by the binding) before invoking methods. |
| 90 | + |
| 91 | +### Available action methods |
| 92 | + |
| 93 | +Below is a quick reference of the builder-style methods provided by the binding actions. Use these to construct messages before calling `send()`. |
| 94 | + |
| 95 | +| Method | Parameters | Description | Ntfy docs | |
| 96 | +|--------|------------|-------------|----------| |
| 97 | +| withMessage | (String message) | Set the main message text. | [publish](https://docs.ntfy.sh/publish/#message-title) | |
| 98 | +| withPriority | (int priority) | Set message priority (0-5). | [priority](https://ntfy.sh/docs/publish/#priority) | |
| 99 | +| withTag | (String tag) | Add a tag to the message. | [tags](https://ntfy.sh/docs/publish/#tags) | |
| 100 | +| withIcon | (String url) | Set an icon URL for the notification. | [icons](https://ntfy.sh/docs/publish/#icons) | |
| 101 | +| withAttachment | (String url, String filename) | Attach a file or resource URL with optional filename. | [attachments](https://docs.ntfy.sh/publish/#attach-file-from-a-url) | |
| 102 | +| withViewAction | (String label, Boolean clearNotification, String url) | Add a view action (opens URL) with optional clear flag. | [actions](https://docs.ntfy.sh/publish/#open-websiteapp) | |
| 103 | +| withCopyAction | (String label, Boolean clearNotification, String value) | Add an action that copies text to clipboard on the client. | [actions](https://docs.ntfy.sh/publish/#copy-to-clipboard) | |
| 104 | +| withHttpAction | (String label, Boolean clearNotification, String url, String method, String headers, String body) | Add an HTTP action with optional method, headers and body. | [actions](https://docs.ntfy.sh/publish/#send-http-request) | |
| 105 | +| withBroadcastAction | (String label, Boolean clearNotification, String params) | Add a broadcast action to trigger local apps/handlers. | [actions](https://docs.ntfy.sh/publish/#send-android-broadcast) | |
| 106 | +| withDelay | (String delay) | Assign a delay. | [delay](https://docs.ntfy.sh/publish/#scheduled-delivery) | |
| 107 | +| withSequenceId | (String sequenceId) | Assign a sequence id for later reference (useful for delete/update). | [sequence id](https://docs.ntfy.sh/publish/#updating-deleting-notifications) | |
| 108 | +| send | () | Send the built message; returns a message ID string. | [publish](https://ntfy.sh/docs/publish/) | |
| 109 | +| send | (String file, String filename, String sequenceId) | Send a file; returns a message ID string. | [publish_local_file](https://docs.ntfy.sh/publish/#attach-local-file) | |
| 110 | +| delete | (String sequenceId) | Delete a message previously sent with the given sequence id. | [delete](https://ntfy.sh/docs/publish/#deleting-notifications) | |
| 111 | + |
| 112 | +For more information about ntfy features and the notification format, see the ntfy project documentation: [https://ntfy.sh/docs/](https://ntfy.sh/docs/) |
| 113 | + |
| 114 | +### Rules DSL example |
| 115 | + |
| 116 | +```rules |
| 117 | +// Obtain the Thing-specific actions and use the builder to send a message |
| 118 | +val bindingActions = getActions("ntfy", "ntfy:ntfyTopic:frontdoor") |
| 119 | +if (bindingActions !== null) { |
| 120 | + // simple one-liner: send a message |
| 121 | + val msgId = bindingActions.withMessage("Someone is at the door").withPriority(4).send() |
| 122 | +
|
| 123 | + // or build up a more complex message |
| 124 | + bindingActions.withMessage("Doorbell pressed") |
| 125 | + .withSequenceId("doorbell-1") |
| 126 | + .withTag("door") |
| 127 | + .withIcon("https://example.org/icons/door.png") |
| 128 | + .withViewAction("Open UI", true, "https://example.org/ui") |
| 129 | + .send() |
| 130 | +
|
| 131 | + // delete a previously sent message by sequence id |
| 132 | + bindingActions.delete("doorbell-1") |
| 133 | +} |
| 134 | +``` |
| 135 | + |
| 136 | +The builder methods available include: withMessage(String), withPriority(int), withTag(String), withIcon(String), withAttachment(String, String), withViewAction(...), withCopyAction(...), withHttpAction(...), withBroadcastAction(...), withSequenceId(String), send(), and delete(String). |
| 137 | + |
| 138 | +### ECMAScript / JavaScript example |
| 139 | + |
| 140 | +In ECMAScript scripts you can obtain the Thing Actions and use the same builder API. The example below assumes the automation engine provides an `actions` helper (typical in openHAB JavaScript environment): |
| 141 | + |
| 142 | +```javascript |
| 143 | +// get the Thing actions for the topic Thing |
| 144 | +var bindingActions = actions.get("ntfy", "ntfy:ntfyTopic:frontdoor"); |
| 145 | +if (bindingActions) { |
| 146 | + // send a simple message |
| 147 | + var id = bindingActions.withMessage("Garage opened").withPriority(3).send(); |
| 148 | + |
| 149 | + // build and send a message with extra features |
| 150 | + bindingActions.withMessage("Motion detected in garage") |
| 151 | + .withSequenceId("garage-1") |
| 152 | + .withTag("motion") |
| 153 | + .withHttpAction("Open Camera", true, "https://camera/local/snap", "POST", null, null) |
| 154 | + .send(); |
| 155 | +} |
| 156 | +``` |
| 157 | + |
| 158 | +If your scripting environment exposes a different API to retrieve Thing Actions, adapt the call accordingly. The important part is to obtain the Thing-specific actions object for the binding and then use the builder methods shown above. |
0 commit comments