Trigger rule based on state changes from members in semantic groups

Hey guys,

in DSL it is possible to trigger a rule if an item inside a group changes its state. For example, I have a group that has status switches for all devices. Whenever one of these switches inside the group changes, the rule fires to notify me that a device went offline.

- id: "0"
  groupName: gMonitor
  state: ON
  previousState: OFF
type: core.GroupStateChangeTrigger
conditions: []
- inputs: {}
id: script
  type: application/vnd.openhab.dsl.rule
  script: > ...

I want to do something similar for my battery levels. I want to receive a notification whenever the battery level of a device becomes low.

I was wondering if I can use a push approach, e.g. whenever a switch inside the semantic group Low Battery changes from OFF to ON, send me a notification. However, I couldn’t figure out how to do it.

The only other option is polling with a cronjob which is not ideal, cause I have to manually add all Low Battery items to a group…


Careful with the terminology. Unless you created an Equipment Group for all your Batteries, which would be unusual and not really the best way to use the model, you do not have a Battery Group. You have a number of Point Items tagged with something indicating it’s a battery (lowBattery perhaps).

OK, having said that, you can create a Group outside the model and put all your Battery Switches in there. You can set an aggregation function on the Group so that the state of the Group itself reflects some aggregation of all it’s members. For example, the Group can be ON if any of it’s members are ON.

Once you have this Group you can trigger the rule with a Member of trigger which will trigger the rule when any member of the Group has the given event. Inside the rule, you can get the Item that actually caused the rule to trigger using triggeringItem (for Rules DSL) or event.itemName for JavaScript/Python/Groovy?.

Thanks, yes that’s the workaround I am currently using.

You are right, what I actually meant is that all switch items that indicate that the battery of a device is low belong to the semantic class “Low Battery”. Now I was wondering if it is possible to create a rule with a trigger that fires whenever the state of an item that belongs to a semantic class - in this case “Low Battery” changes.

My workaround (which of course is not ideal) is to create a group without any semantic attribution, add all items that belong to the semantic class of “Low Battery” manually, and create a rule-based trigger.

However, that way, I always have to manually add new items for new equipment and I was wondering if there’s a way to do it automatically. But it seems triggers for items in semantic classes do not (yet!?) exist.

Sorry answer is no. Longer answer is maybe if you use text based rules it’s easy to create rules that create rules. You can pull all the items and filter them to just those with a certain tag and then create a new rule with triggers for those items.

The “workaround” is the recommended approach.

They do not and I’m not so sure how easy that would be to add if it can be added at all.