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:
|The UID of the Thing that changed status
|The status the Thing was in before the change
|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’.
|The status the Thing changed to.
|The detail the Thing changed to, or
|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.
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 [18.104.22.168;22.214.171.124].
Language: JS Scripting (ECMAScript 2021 with the openhab-js helper library injection enabled)
- OH 4.0.0 +
- JS Scripting add-on installed
- openhab-js 4.1.0 or later
- openhab_rules_tools 2.0.1 or later
- throws an exception of openhab-js or openhab_rules_tools are not new enough
I have but didn’t really read it closely to understand what it says. But on a closer read, none of the changes seem to invalidate what I’m trying to do here. They are just some changes to the names of the configuration properties and the way the matching filter works (if I read that issue correctly). I fully expect I’ll need to adjust most if not all of my rule templates over the coming months so that sort of thing doesn’t bother me. This one was a simple one that will let me experiment with the marketplace and such.
I don’t plan on adding the “published” tag until we get closer to release so changes I have to make shouldn’t be too disruptive.
However, if I’m misunderstanding the PR and I can’t actually use the GenericEventTrigger to trigger a rule on ThingStatusChangedEvents at all I’ll have more concerns.
Ideally, this whole rule template would best be replaced with something like a Member of trigger except for Things. So I look at this as a stop gap (perhaps a long term one ).
Thanks for the heads up!
Edit: since the change was merged I’ve now updated the rule template to use the new property names and a glob for the topic instead of the regex.
I have installed thing status reporting from the marketplace but I am missing openhab_rules_tools and I can’t work out how to install it? Am I missing something obvious?
Script execution of rule with UID '8c4b7b49f7' failed: org.graalvm.polyglot.PolyglotException: TypeError: Cannot load CommonJS module: 'openhab_rules_tools'
If you are on openHABian it can be installed through openhabian-config.
If not, from
$OH_CONF/automation/js run the command
npm install openhab_rules_tools
I can’t quite see how i can access the context in my rule. Is there some documentation around i’m missing?
What rules’s language are you using?
In JS Scripting and Rules DSL you just reference the variable name:
In Nashorn JS you’d use
I don’t know about jRuby.
In Blockly you’d use the “get context attribute” block which can be found under the Rule and Process category.
I’m using DSL. I tried:
var thingLabel = thing.label
but i get:
The name 'thing' cannot be resolved to an item or type;
It’s been a very long time since I tried it in Rules DSL and now that I think about it I don’t know that I ever really did try it in Rules DSL. I may have just assumed it gets passed into the rule the same as everything else that gets passed into the rule. I ran a couple of tests and as far as I can tell, you cannot pass arguments into a Rules DSL rule. If it is possible, I don’t know how to do it.