This rule implements an MQTT Even Bus. An event bus is a mechanism where Item events generated by an openHAB instance (commands and updates) get published in a well defined way to MQTT topics. On the other side, an openHAB instance can subscribe for these events and transfer them to a local Item. In this way, two openHAB instances or an openHAB and third party tool can be linked with minimal configuration required.
Note, MQTT can be a challenge to understand and use. Be sure you have a basic understanding of the technology before attempting to use this rule template. This rule template also requires a good deal of configuration outside of the rule itself.
The MQTT Event Bus consists of two parts:
- Items that are members of one or both of two Groups will have their commands and/or updates published
- The MQTT Broker defines a wild card subscription to an Event Bus’s MQTT topic hierarchy and translates the messages received into commands and/or updates on local Items
This rule implements both behaviors, choosing what to do based on how the rule is triggered. For example, if triggered by a member of the command Group receiving a command, the rule will publish that command to the topic that represents this Item. If triggered be a Thing Channel Event, it assumes this is a message received by the event bus and the message is parsed and updates or commands the local Item as required.
Warning, to avoid infinite loops, do not publish and subscribe the same event on the same Items. Follow this table:
openHAB instance | Publishes | Subscribes |
---|---|---|
Linked to device | updates | commands |
Remote | commands | updates |
In other words, the openHAB instance linked to the device publishes only updates but subscribes for commands. The remote openHAB instance will only publish commands and subscribe to the updates.
The rule has the following parameters:
Parameter | Purpose |
---|---|
MQTT Broker Thing | Select the MQTT Broker Thing to publish to. |
openHAB instance name | The name of this openHAB instance, will be used as the root of the topic events are published to. |
Command Group | The Group whose member’s commands are published to the event bus. |
Update Group | The Group who memember’s updates are published to the event bus. |
Subscription Chanel | The Event Channel configured to with the wildcard subscription on the event bus topics. |
LWT Topic | The topic where the ONLINE message is to be published. |
Language: JS Scripting
Dependencies:
Prerequisites
- openHAB 4.0 SNAPSHOT #3303+
- openhab-js 4.1.0 or later
- openhab_rules_tools 2.0.1 or later
Config
-
MQTT Broker installed and configured (e.g. Mosquitto)
-
MQTT Binding installed
-
MQTT Broker Thing created and configured to connect to the MQTT Broker
- be sure to configure a LWT topic with “OFFLINE” as the LWT message, enable retained
- be sure to configure a Birth topic with “ONLINE” as the message, enable retained
-
A Group containing those Items who should have their commands published.
-
A Group containing those Items who should have their updates published.
-
A Trigger Channel defined on an MQTT Thing (it can be defined on the MQTT Broker Thing)
-
to subscribe for all events on the event bus (handy in cases where the remote instance has sensors only) use the
#
wild card (e.g.remote-openhab/out/#
). To only subscribe to commands or state updates use+
(e.g.remote-openhab/out/+/command
orremote-openhab/out/+/state
). Note that states are published with the retained flag set to true, commands set to false. -
configure
#
as the “Separation character” so that the topic and the message are included in the event sent to the rule.
-
Changelog
Version 0.3
- now throws an exception if openhab-js or openhab_rules_tools are too old
Version 0.2
- The MQTT Broker Thing now supports publishing the ONLINE message so removed that from this rule template.
Version 0.1
- initial release
Sponsorship
If you want to send a tip my way or sponsor my work you can through Sponsor @rkoshak on GitHub or PayPal. It won’t change what I contribute to OH but it might keep me in coffee or let me buy hardware to test out new things.