Thing Status Reporting

logo

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

Dependencies:

  • none known

Changelog

Version 0.1

  • initial release

Resources

https://raw.githubusercontent.com/rkoshak/openhab-rules-tools/main/thing_status/template/thing_status.yaml

6 Likes

Thanks! This looks just like something I have been trying to achieve! I’m a complete idiot on javascript, but is there a way to filter the things to monitor? I don’t need to know if a light bulb goes offline, but I would very much like to know when a smoke detector goes out!

Not without modifying the rule to the point it will be completely different from what is posted here.

But you can create a rule triggered by the status changes on the Things you care about and then do something based on that.

Or in the rule you write that gets called by this rule you can add some code to loo through the Things passed to it and ignore the ones you don’t care about.

I hope you can stand me drifting slightly of topic, but how can I make a rule that takes another rules output as an argument? I have been using OH for a while now, but this is completely new for me!

Once you’ve installed this rule template from the marketplace.

  1. Create a rule to be called. Remember it’s ID.
  2. Create a rule and choose this Rule Template from the list of templates.
  3. In the “Rule to Call” property, select the rule you created in 1.

In the rule created in step 1, you can access the list of Things passed to it from the template created rule using the code from above:

var things = context.getAttribute("things")

It will look like that line should work just like that as written in Rules DSL or JavaScript.

It really couldn’t be simpler.

1 Like

Aha, I think I see it now! The rule created using the template is executing a script that in turn is executing my pre-made rule (as defined in the template properties when instantiating the template) that can execute a script that fetches the list of things from the context object!
As you say, couldn’t be simpler! Just needed to figure out the whole template-thing!

Just to make this clear.

You code Rule 1
You create Rule 2 from the Template, filling out the properties as desired.

The data flows:

Rule 2 → Rule 1

The data is a List of Thing objects named “things”.

Rule 2 collects the Things that match the criteria and passes them to Rule 1 for further processing. All that the rule template above does is create that list of Things (e.g. all Things that are not ONLINE). Nothing more.

You need to be on the latest milestone. Then go to Settings → Automation and add the template from the list.

1 Like