When rule react on “changed” only, it turns light during startup, which is unacceptable, so on my test-bed I ended with:
rule "light"
when
Item sw1 changed from OPEN to CLOSED
or Item sw1 changed from CLOSED to OPEN
or Item sw2 changed from OPEN to CLOSED
or Item sw2 changed from CLOSED to OPEN
then
light.sendCommand(if(light.state == ON) OFF else ON)
end
and items are:
Contact sw1 "sw1" { gpio="pin:19" }
Contact sw2 "sw2" { gpio="pin:26" }
Switch light "light" { gpio="pin:18" }
This works as wanted, light is toggled on any change of contacts (wall-switches), and from mobile can control directly the light.
There stay minor problem:
When change both contacts at same time, the rules react in parallel, so light is toggle only once, as “both rules” read same old state and set same new state.
I tried solve this by lock/unlock:
rule "light"
when
Item sw1 changed from OPEN to CLOSED
or Item sw1 changed from CLOSED to OPEN
or Item sw2 changed from OPEN to CLOSED
or Item sw2 changed from CLOSED to OPEN
then
logWarn("gt", "begin")
lock.lock()
logWarn("gt", "begin2")
try {
light.sendCommand(if(light.state == ON) OFF else ON)
} finally{
logWarn("gt", "end")
lock.unlock()
}
end
But probably even if i guard the sendCommand, it looks like it didn’t guard the real “ItemStateChangedEvent”.
So sometimes the “second rule” still can read state before first rule applied.
Worse problem is, that I can achieve such problem when physical contact bounce:
Will examine this more, with this I crete new thread Contact bounce - lost events?