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 or JSScripting

Dependencies:

  • none known

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

Resources

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

7 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

That’s the thing monitoring, I needed - Thanks!

I see this code in my blockly:

offlineThings = ctx['things'];

Which seems to work fine.
However, the things are cryptic:
org.openhab.core.thing.internal.ThingImpl@bc5a35e
Is there a way to translate this?

I know how to work with transformations like MAP, but where to look for the reference of this thing?
(your Threshold Alert rule returns the ItemLabel if I am not wrong)

Threshold Alert sends a List of the Items that are NULL or UNDEF, a comma separated string of the labels of the Items that are NULL or UNDEF, a List of the items that meet the criteria, and a comma separated string of the labels of the Items.

It’s all explained in the Marketplace posting.

The Thing Status rule template sends a List of the raw Thing Objects that match the criteria. What you are seeing and call cryptic is the name of the Class that implements the Thing and the @bc5a35e is the address in memory.

You will need to call methods on the Thing Object to get information out of it and I’m not sure Blockly supports that. Consider using Rules DSL (since I bet that’s what you know best) for this one Script action. It’s not a problem to have more than one language going in your set up.

The reference for this and all other openHAB classes are at Overview (openHAB Core 3.3.0-SNAPSHOT API) (or something like that, I’m having weird issues on my phone bringing up pages right now so can’t double check). Search for “thing” and you’ll find the doc for that class.

That’s not neccessary, Rich.
Even a single check from you is more than enough.

So, thanks for the explanation I will build in my DSL rule stuff for this topic then.
I have a lot of well working DSL rules, but try to move as much as possible over to mainUI rules - (preferrably Blockly). Maybe it’s better to move slowly on my migration path.

Just remember, you could still move this to MainUI. Just choose Rules DSL as the script language instead of Blockly.

Yes, thank you.
I did this for some rules (e.g. geofencing with Location items within a group).
Maybe I should look into JS direct for the future, but will start with my DSL code for now.
Again, thanks for your help, Rich.

But what about this:
image

which in code is this:

is this the same like

you are referring to in the threshold-alert thread?

or is there no way to get the context attribute from the Threshold-alert in Blockly?

You can get the things and you can probably even manipulate the List of Things. But I don’t think you can then call any of these methods on the Thing Objects which means you can’t even get the name or uid of the Things that are offline from Blockly.

I assume though that you can get to the variables passed to the script using Rules DSL. In Nashorn it’s called context and it has a comtext.getAttribute('key'). In JSScripting the passed in stuff just appears as a variable. So the things is just directly accessed (see below).

I don’t know whether or how it appears in Rules DSL. I would assume it’s one of these two ways.

Here is my script that gets called by the Thing Status Alert for reference. It’s in JSScripting.

configuration: {}
triggers: []
conditions: []
actions:
  - inputs: {}
    id: "1"
    configuration:
      type: application/javascript;version=ECMAScript-2021
      script: >-
        var logger = log('Thing Status Processor');


        logger.debug("There are " + things.length + " things that are not ONLINE");
    type: script.ScriptAction

Thank you!
I will look into this and see, how I can make use of your code.

@NCO sorry for resurecting this, but I was wondering if you had found a solution to display a “readable” list of things that were offline and if you would mind sharing it?
I know i resurect an old topic, but i think it complements nicely the Thing Status Reporting.
Thanks,

Sorry, I did not follow this path yet.
I had more urgent things to fix :wink: