OH3 How to determine in javascript which group triggered rule

Using OH3, EECMAScript-2021

I have a number of rules in OH3 (version 3.3.0) that are triggered when a member of a group has changed. In javascript I can use event.itemName to determine the item that triggered the rule.

I now have a case where I need to determine which group triggered the rule, not which item.
The rule can have two or more groups in the “When” section, using “This triggers the rule if the state of a member of an item group has changed” [quoted from OH3]. Items are assigned to only one of the triggering groups. Items can also be assigned to other groups within OH.

I have searched the forum and found instances where group membership of an item can be determined. I have not found way of determining which group triggered the rule using javascript.

Any help in pointing me in the right direction would be appreciated.

Thanks

If items are assigned to only one of the triggering groups, then you just find out whether the Item event.ItemName belongs to group A or B

There’s Item.getGroupNames() that gives you an array (Java List) of the groups that the item belongs to.

What’s the nature of the Groups? Could you use an Item update or changed event instead of a Member of trigger? That would be the easiest and most straight forward.

That’s true for the raw Java classes and Objects. The JS helper library though does a pretty thorough job of always presenting JavaScript classes and Objects. So items.getItem(event.itemName) returns a JavaScript Item and items.getItem(event.itemName).groupNames returns a JabaScript Array of Strings containing the names of the Groups the Item belongs to.

This approach helps users remain in JavaScript land as much as possible so we don’t have to worry about whether that List is a Java List or a JavaScript Array and use different ways to interact with each.

So @KUnger you could use something like this:

if(items.getItem(event.itemName).groupNames.includes(<name of Group>)

to determine if the triggering Item belongs to <name of Group>. But you’ll have to do a switch or if/else for each Group to see which Group it belongs to. That’s why I said above, it be easier and more straight forward to change the triggers to trigger off of the Group itself, instead of it’s members.

Thanks for the response and suggestions.

Agreed.

If I understand you correctly I should be able to trigger off of a Group. Yet when I select a Group and then select “This triggers the rule if an item state has changed” or “updated” it never triggers the rule when an item within that Group changes.

The rule only triggers when I select one of the triggers that include “… state of member of an item group …” .

How do we trigger off of the Group?

I realize that I could do a switch or if/else statements. I wanted to avoid that if possible and the need to change the code anytime a change was made to the “When” trigger. I was hoping that I could get the triggering Group name and then use it to determine code execution.

Just like any other Item. But the Group needs to be defined correctly. It need to have a type and an aggregation function.

Are you certain that the Group changes or are you assuming it has? Do you see an entry in the log telling you the Group’s state changed?

That was it!

The Groups “Member Base Type” is now defined as a Number and aggregation function as SUM. In the rule the trigger for each Group is set to “if an item state has changed”. Now it works exactly as needed.

Anytime an item within the group changes, the group changes, the rule triggers and event.itemName returns the triggering Group.

Now I understand better how Groups and Group Settings work and of course the rule triggers.

Could we add something to the OH documentation on rules and triggers https://www.openhab.org/docs/tutorial/rules_introduction.html#triggers to better explain this? The current documentation suggests that Groups only work with “triggers the rule when any member of a Group …”. That’s where I began to stumble. Groups with the correctly defined Group Settings also allow the use of triggers within the Category = Item section of that table.

Thanks again for your help.

That page is not and never was intended to be the sole documentation. It presents just enough to get you started. Rules | openHAB, Rules | openHAB, and Items | openHAB are intended to be the complete documents. So always refer to the reference docs too, not just the Getting Started Tutorial which is, by necessity, incomplete.

I’m not really sure what to add to that page to help. There is a discussion of Groups on the Semantics page of the Getting Started Tutorial already. A Group is a type of Item so anything that applies to Items also apply to Groups.

I see your point.

The thing that I struggled with when I was creating my Rule is understanding what “a Group is a type of item” means, in particular when it came to rule triggers and especially those effected by Member Base Type and Aggregation. What confused me (and others perhaps as well) is that Rules | openhab says “Rules can be triggered on any Item event from any of its members”, which I had taken as meaning that only "… state of member of an item group …” triggers would work with Groups.

What you’ve done is shown me that it’s more complex than that and that Item type triggers can be used on Groups when Groups are configured appropriately. That was the part I was not able to figure out when reading through the Openhab documentation.

Anyway, enough said in this conversation. You have helped me understand Groups, triggers and Openhab much better. Thank you again for your time.

2 Likes

I’ll think on a better way to say that. The fact that a Group is an Item was supposed to imply that for the state of the Group itself that first row in the template applies.

1 Like