Move a switch at a certain time when an item is off

Hello there,

I´m stuck with a - for sure - simple problem:

I wan´t to turn of lights at 22:00 (that works :sweat_smile:).

But if the TV is still running, the lights should remain on. If the TV is then turned off, the lights should go out too.

So the rule must be something like this

rule LightsOff
when
Time cron "0 0 22 ? * MON-THU *"
then
if(TV.state != ON)
Switch01.sendCommand("OFF")
else ???
end

There need to be a loop/timer to check whether the TV is turned off in the meantime. But I don´t get it.

Thanks a lot.

You need two Rule triggers. You need to trigger at the right time and you need to trigger when the TV turns OFF.

When the Rule triggers because the TV turned off, you need to check if now is between 22:00 and some time in the morning.

rule "Lights off" // the quotes are required I beleive
when
    Time cron "0 0 22 ? MON-THU *" or
    Item TV changed to OFF
then
    // do nothing unless it is Monday through Thursday
    if(now.getDayOfWeek < 1 && now.getDayOfWeek > 5) return;

    val morningStart = now.withTimeAtStartOfDay.plusHours(6) // choose an appropriate time
    val nightStart = now.withTimeAtStartOfDay.plusHours(22)

    // do nothing unless it is night time
    if(now.isAfter(morningStart) && now.isBefore(nightStart)) return;

    // turn OFF the light if the TV is OFF
    if(TV.state != ON) Switch01.sendCommand(OFF)
end

The Rule triggers at 22:00 and whenever the TV turns OFF. It immediately exits if it isn’t between Monday and Thursday and if it isn’t between 22:00 and 06:00. If it didn’t immediately exit is means we should turn OFF the Switch but only if the TV isn’t ON.

2 Likes

Thank you so much, that works perfect. There’s still a lot to learn about rules and how to use them :thinking: