Determining the triggering item in the body of a rule

`[quote=“hildner, post:1, topic:2654, full:true”]
Hi all, I am searching for a way to determine what item has triggered a rule.
The “when” part of the rule could be an “or” of conditions, or it could be a group of items.

For example this piece of code that is supposed to output which motion sensor triggered a burglary alarm:

rule "burglary alarm"
when
	Item Alarm_Burglary_State_Toggle changed from OFF to ON or /* Panic */
	Item Fibaro_1_Motion changed from OFF to ON or
	Item Fibaro_2_Motion changed from OFF to ON or
	Item Aeotec_1_Motion changed from OFF to ON
then
	// if armed, trigger alarm
	// needs to know which item has triggered it for logging, updating a descriptive String item, etc.)
end

There are the implicit object receivedCommand and previousState (ON/OFF), but I cannot figure out how to navigate to an object reference.

An answer probably lies within https://github.com/openhab/openhab/wiki/Taking-Rules-to-New-Heights (“The Mother-of-All Light-Off Rules”), but I don’t get it.
[/quote]

For anyone searching for a solution this may help, as it took me a while to work it out.

rule "Battery updated"

    when 
    	// Group to monitor
        Item GF_Battery received update 
    
    then
    	
    	//Get item from group
		val lastItem = GF_Battery.members.sortBy[lastUpdate].last
		
		// Grab name of item
		val nameOfItem = lastItem.name
		
		// Grab value of item as appropriate type
		val valueOfItem = lastItem.state as DecimalType
		
		logInfo("Battery Updated", nameOfItem + " received: " + valueOfItem)
end

Hope this helps someone

3 Likes