Global rule vs item triggering in the rule

Hi everyboy

Is it possible to have a global rule for different items and have only the item that triggered the rule to have the command send? For exemple, itemx changed:

itemx changed from on to off or
itemy changed from on to off or
itemz changed from on to of

then
send conmmand to itemx
end

Hi Jose,

This should indeed work. This code snippet is part of my live rules set and working flawlessly.

rule “Tuinverlichting act on HM_Button press long”
when
Item HM_ButtonTopLong received update ON or
Item HM_ButtonBottomLong received update ON

then
if (Rule_State.state == ON) {

  if (HM_ButtonLongToggle==0) {
  	sendCommand(GF_Garden_Right_Light, ON)
  	sendCommand(GF_Garden_Left_Light, ON)
  	HM_ButtonLongToggle=1
  	logInfo("Switches.Rules", "GF_Garden_Right_Light & GF_Garden_Left_Light switched on, triggered by HM_Button.")
  }
  else {
  	sendCommand(GF_Garden_Right_Light, OFF)
  	sendCommand(GF_Garden_Left_Light, OFF)
  	HM_ButtonLongToggle=0
  	logInfo("Switches.Rules", "GF_Garden_Right_Light & GF_Garden_Left_Light switched switched off, triggered by HM_Button.")
  }

}
end

Put all the items in a group and loop it?

rule "group loop"
when 
      Item myGroupOfItems changed from on to off
then
myGroupOfItems?.members.forEach[item|
       if (item.state == ON) {
          item.sendCommand(something)
        }
 }
end

Not directly but you can cheat if you put all your items in a group and grab the one most recently updated. This only works if you have persistence configured and working.

Thread::sleep(100) // give persistence time to same the change, experiment with different times
myGroupOfItems.members.sortBy[lastUpdate].last.sendCommand(newState)

Hi

I tried your rule but it doesn’t work.
I have persistence for the items db4o.
Is it possiblle to have an exemple with logging the item that was
identified?

Thanks
jose

You have to give us more to work with than “it didn’t work”. What was the error. Post exactly what your Items and rule looks like. Post and log statements that site the error.

I know my code works. I copied and pasted it from one of my working rules.