How Do I Find Which Element of a Group Triggered an Event?

I have 4 door sensor items and a rule to send an email message when any one of the doors opens. I would like to include the name of the door sensor that triggered the message. In the rule I “capture” the name of the triggering item (val door=triggeringItem) and include it in the message (“Mail Notification”, door.name) but door.name = DoorSensors not which of the doors triggered the message. How can I get to the actual sensor?
Here is the rule:

rule "Send Any Door via Mail"
when
    Item DoorSensors changed
then
	val door=triggeringItem
    if(DoorSensors.state == "Opening")
    {
        logInfo("doors", "Sending notification via mail.")
        sendMail("Removed_Name@yahoo.com", "Mail Notification", 
            door.name)
    }
end

Here are the items:

Group:String:OR("Opening","Closing") DoorSensors
String BackDoor "Back Dr Status  [MAP(my.map):%s]" <frontdoor> (DoorSensors) {mqtt="<[mosquitto:BackDoor:state:default]"} 
String FrontDoor  "Front Dr Status [MAP(my.map):%s]" <frontdoor>  (DoorSensors)   {mqtt="<[mosquitto:FrontDoor:state:default]"}
String GarageDoor "Garage Dr Status [MAP(my.map):%s]" <frontdoor>  (DoorSensors)   {mqtt="<[mosquitto:GarageDoor:state:default]"}
String PorchDoor "Porch Dr Status [MAP(my.map):%s]" <frontdoor>  (DoorSensors)    {mqtt="<[mosquitto:PorchDoor:state:default]"}

See https://www.openhab.org/docs/configuration/rules-dsl.html#member-of-triggers and https://www.openhab.org/docs/configuration/rules-dsl.html#implicit-variables-inside-the-execution-block.

1 Like

First, please use code fences when posting code

Then what is the trigger? A group item. Therefore the triggering Item is the group item.
You may want to explore the Member of trigger as mentionned in the post above

Third, your post didn’t have a topic so I move it to Script and Rules

I believe I used the code fences correctly now. Thanks for pointing that out.
I wasn’t able to ferret out what I need from those links and other searches unless I wasn’t interpreting it correctly. I believe they are showing how to work with an item in the group. I think I would have to go through each item to see which caused the “Opening” trigger. That sort of defeats the purpose of using groups.
I understand that “val door=triggeringItem” yields DoorSensors because that is the (Group) item that cause the trigger. I am trying to drill down one level to see which item in the group caused the “Opening” trigger. It should be available because the log shows:
“DoorSensors changed from Closing to Opening through FrontDoor”
In this case the FrontDoor sensor was the one out of four items that triggered the DoorSensors group item.

I think I will have to pass on this and be happy with knowing it was a DoorSensors trigger.

Thanks.
Pete

Change your rule like this:

when
    Member of DoorSensors changed
then

You have to change your rule slightly:

rule "Send Any Door via Mail"
when
    Member of DoorSensors changed
then
    if(DoorSensors.state == "Opening") {
        logInfo("doors", "Sending notification via mail, because {} is opening.",triggeringItem.name)
        sendMail("Removed_Name@yahoo.com", "Mail Notification", triggeringItem.name)
    }
end

The whole point of triggeringItem is, to get the Item, which triggered the rule. But if there is only one Item, then, well, this item is triggerering the rule every time.
When trigering the rule via Member of, this is the same as writing down all members as different item triggers:

rule "Send Any Door via Mail"
when
    Item BackDoor   changed or
    Item FrontDoor  changed or
    Item GarageDoor changed or
    Item PorchDoor  changed
then
   ...
2 Likes

Wow, you guys nailed it. I had to make one change. Just in case someone comes across this thread, I had to change, “DoorSensors.state == “Opening”” to "triggerItem.state=“Opening” because once any door is open "DoorSensors.state becomes “Opening” and remains that way until all are closed. So opening 2 doors and closing one door triggered Member of… and with DoorSensors still “Opening”, the closing results in a message being sent.
I have to say OpenHAB is wonderful. I seem to have many problems interpreting the docs and can not find what I need. But when the solution arrives as in this case it is simple and elegant.
Thanks Again

1 Like