Note, in the first half of 2023 this rule template will be adjusted to only support JS Scripting.
A rule that runs periodically and calls a user defined rule with the list of Things that match a configured criteria. For example, one can run it once a minute or so and call another rule with the list of Things that are OFFLINE, or the ones that are not ONLINE.
A list of Things that match the criteria is passed to the called rule and is available with context.getAttribute("things");
. This list can be processed any way desired.
The conditions of the rule called are taken into account so one can put limits on when or how often the the rule runs (e.g. don’t run at night).
By default it is configured with a cron trigger to run periodically based on the desired periodicity. One could customize the rule by changing the triggers to list each Things’s status change individually or use any other criteria desired to trigger the rule.
One useful trick will be to keep track of the old list of Things in the called rule so one can see which ones changed since the last call. (var oldthings = (oldthings === undefined) ? context.getAttribute("things") : oldthings;
).
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.
All but the rule properties are optional. If unchanged the rule template will cause the rule to run every 30 seconds and call your rule with all the Things that are OFFLINE.
Language: Nashorn JavaScript or JSScripting
Dependencies:
- in OH 4 Nashorn must 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: Support for Nashorn will be dropped sometime after OH 3.2 is released.
Version 0.1
- initial release