Rules are triggered by events. An if statement checks state. What you are looking for is to detect three events, not detect one event and check the state of two Items.
So, as I said, rules are triggered by events so all three of these events AND you care about the order and timing of these events. This is not going to be easy.
If you don’t care about the order of the events or the timing of the events, you can just trigger the rule on all three events and leave the body the same:
rule "Going To Bed"
when
Item Landing_First_Floor_Motion changed from CLOSED to OPEN or
Item Ceiling_Light changed from OFF to ON or
Item Floor_Light_Living_Room changed from ON to OFF
then
if(Ceiling_Light.state != ON && Floor_Light_Living_Room.state != OFF){
Bed_Side_Light.sendCommand(20)
}
end
You might add a time check in there so this rule only executes at night or during a time that you are likely to be going to bed.
Clearly the order of the events does not matter as far as this rule is concerned. Whenever any one of those three events occur, if the lights are in the indicated states the bedroom lamp will turn on. This may be adequate, it may not. It depends on your use case.
If you really want them to occur in order there are several approaches you can take but all of them involve keeping up with when the events occurred and checking to see if they occurred in order.
Perhaps the easiest is if you set up persistence on the light switches. Then you can test the lastUpdate time.
rule "Going To Bed"
when
Item Landing_First_Floor_Motion changed from CLOSED to OPEN
then
if(Floor_Light_Living_Room.state == OFF && Ceiling_Light.state == ON){
val livingTime = Floor_Light_Living_Room.lastUpdate
val ceilingTime = Ceiling_Light.lastUpdate
if(livingTime.isBefore(ceilingTime) && livingTime.isAfter(now.minusMinutes(5).millis) ) {
Bed_Side_Light.sendCommand(20)
}
}
end
In the above, when the motion sensor goes off, if living room lamp is OFF, and the ceiling light is ON, and the lamp was turned off no more than five minutes ago and the ceiling light was turned ON even closer to now (i.e. ceiling light was turned ON after the living room light was turned ON) then turn on the Bed_Side_Light.
Without Persistence you can keep track of the times these events occur manually:
var livingTime = null
var ceiling = null
rule "Living room lamp turned off"
when
Item Floor_Light_Living_Room changed from ON to OFF
then
livingTime = now
end
rule "Celing light turned on"
when
Item Ceiling_Light changed from OFF to ON
then
ceiling = now
end
rule "Going To Bed"
when
Item Landing_First_Floor_Motion changed from CLOSED to OPEN
then
if(living.isBefore(ceiling) && living.isBefore(now.minusMinutes(5)) {
Bed_Side_Light.sendCommand(20)
}
end
The problem isn’t that the states are wrong, it is that @Maximo is misunderstanding how the if statement works.