Note: This rule template is deprecated. I’ll leave it up for older versions of OH but sometime in 2022 support for wild card Things status update triggers was added. In place of the String ID you can use wild cards to match those IDs you want. For example “chromecast:*” will trigger on all things whose ID starts with “chromecast:”. Therefore the need for this rule template is no longer there.
But the UI doesn’t directly support this yet so I will keep this up but won’t create a new version for OH 5. To use the trigger in the UI, add a Things status update trigger to any random Thing, click on the code tab and replace the Thing ID you selected with your matching pattern.
A rule that triggers when a Thing changes its status and calls a user defined rule with the Thing and information about the Thing. The conditions of the called script are enforced so events can be filtered there (e.g. only process changes from ONLINE to some other status).
The following properties are passed to the called rule:
Property | Contents |
---|---|
thingID |
The UID of the Thing that changed status |
oldStatus |
The status the Thing was in before the change |
oldDetail |
Some Thing statuses have additional detail. For example, a Thing that has been disabled would have a status of UNINITIALIZED with detail DISABLED . If there is no detail, it will be ‘NONE’. |
newStatus |
The status the Thing changed to. |
newDetail |
The detail the Thing changed to, or NONE . |
thing |
The actual JS Scripting Thing Object (see Thing - Documentation) |
The original Java Thing Object can be reached through thing.rawThing
. The Most of the properties besides the thing
are included to make writing the rule called by this one easier to write in Blockly.
Additional conditions can be placed on this rule or on the called rule to further limit when it runs (e.g. don’t run at night).
Feel free to customize this rule as much as desired to meet your needs. You could even replace the code that calls your rule and put that processing inline.
An important note, this version works significantly differently from the older OH 3.x version of the rule template. But it works much closer to how I envisioned the original one to work in the first place but didn’t know how to configure the trigger properly.
Limitations:
I’ve seen situations where this rule will throw a bunch of exceptions during startup as it gets ponded with Things coming into existence and initializing. Once runlevel 100 is reached the rule works as expected. If you see this you can do the following:
- Create a Runlevel Number Item
- Create a rule that triggers at system runlevel 40
- Add an Item Action and update the Runlevel Item to 40
- Repeat 2-3 for runlevels 50-100.
- Add a condition to this rule to not run unless the runlevel is >=100.
I’m looking into a rule template that will enable/disable rules based on the runlevel reached so stay tuned to the marketplace. See Delay Start [4.0.0.0;4.9.9.9].
Language: JS Scripting (ECMAScript 2021 with the openhab-js helper library injection enabled)
Dependencies:
- OH 4.0.0 +
- JS Scripting add-on installed
- openhab-js 4.1.0 or later
- openhab_rules_tools 2.0.1 or later
Changelog
Version 0.2
- throws an exception of openhab-js or openhab_rules_tools are not new enough
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.