MQTT Event Bus Publication [3.2.0;3.4.9]

Note, in the first half of 2023, this rule template will be rewritten to only support JS Scripting.

This is one of three rules used to create an MQTT Even Bus. The other rules are MQTT Online Status and MQTT Event Bus Subscribe.This rule is responsible for parsing the messages received on an event bus and commanding or updating Items as necessary. See MQTT 2.5+ Event Bus for details on how to set up and configure an MQTT Even Bus.

Warning, to avoid infinite loops, do not publish and subscribe the same event on the same Items.

openHAB instance Publishes Subscribes
Linked to device updates commands
Remote commands 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.

Language: Nashorn JavaScript

Dependencies:

  • MQTT Binding installed
  • MQTT Broker Thing created and configured
  • A Group containing those Items who should have their commands published.
  • A Group containing those Items who should have their updates published.
  • In OH 4 the Nashorn add-on needs to be separately installed or change the script type to application/javascript;version=ECMAScript-2021.

Changelog

Version 0.3

  • adjusted for breaking change, will no longer work for versions of OH prior to December 15th

Version 0.2

  • Made compatible with Nashorn and JSScripting
    Note: will remove support for Nashorn sometime after OH 3.2 release.

Version 0.1

  • initial release

Resources

https://raw.githubusercontent.com/rkoshak/openhab-rules-tools/main/rule-templates/mqttEb/mqtt_eb_pub.yaml

I upgrade openhab to 5.1.0 and saw this warning (twice):

event.itemState is deprecated, use event.receivedState for update triggers or event.newState for change triggers instead.

In my /var/lib/openhab/jsondb/automation_rules.json I found that is it probably caused by this rule.

I’m surprised this works at all in 5.1, let alone in JS Scripting too (note the template indicates it requires Nashorn JS). I know you are running this in JS Scripting because that’s where the warning message comes from.

The numbers in the title of the post: [3.2.0;3.4.9], mean that this rule is only expected to work on OH 3.2 to OH 3.4.9. If you remove the template you will find that it’s not even listed in the Add-On Store anymore (unless you’ve changed the Community Marketplace settings to show everything).

There is a new rule posted to the marketplace that combines the three original rules (MQTT Event Bus Publication, MQTT Event Bus Subscribe, and MQTT Online Status) into a single rule.

  1. Disable the three MQTT Event Bus rules created from these old templates.
  2. From theAdd-on store, remove the three old templates.
  3. Install the new MQTT Event Bus template.
  4. Create a new rule, selecting the same Groups and Channels and other properties you used in the old templates (you can click on the Code tab on the disabled rules to see what properties you original set). Note, reporting the ONLINE status to a LWT is now configured on the MQTT Broker Thing and does not need a separate rule anymore. Everything is documented in the link above.
  5. Once the new rule is created and tested, remove the three disabled rules.

Note, any rule created from a template after OH 5.0 will have a “regenerate” button. Clicking this will let you change the properties of the rule without creating a new rule. If you update the rule template (I post updates a few times a year as bugs are reported or new features are introduced to OH) you just need to:

  1. remove the old template
  2. add the template again (this installs the latest version)
  3. select your rule(s) generated from the template and click regenerate

This will refresh your rules with the latest version of the template.

1 Like