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.
Any help in pointing me in the right direction would be appreciated.
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.
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.
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.
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.
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.
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.