[SOLVED] Switch light on when motion detected but only after a certain time

Hi folks,

I wrote my question already in the topic of this thread:

I want to switch on a light when the motion sensor detects motion but only after a certain time. (The light shall be turned on when I leave my bedroom in the morning, but not before 6 a.m. and not during the night, e.g. when I go to the toilet.)

So, my focus is the time polling. Do I use “Time cron” for this?

Thanks!
Christoph

Quick and dirty:

rule "limited light"
when
    Item motionSensor changed to ON
then
    if (now.getHourOfDay() >= 6 && now.getHourOfDay() < 23) {
        lightItem.sendCommand(ON)
    }
end

But I recommend that you have a look at:

Hi @vzorglub,

thank you very much! :blush:
This seems very likely to solve my problem! I‘m going to check this.

Regards
Christoph

Just one more question concerning the now.getHourOfDay() command:

What about for instance 5:30 a.m. instead of 6 a.m.? Do I use now.getMinuteOfHour() for this?

Thanks and regards
Christoph

Use getMinuteOfDay(5*60+30)

Aaah! :blush::+1:
And how do I use it in the ”if“ expression in your rule example?

rule "limited light"
when
    Item motionSensor changed to ON
then
    if (now.getMinuteOfDay() >= 330 && now.getHourOfDay() < 23) {
        lightItem.sendCommand(ON)
    }
end

But you really should look at:

This rule will go wrong on the date the time changes.
The work around is:

rule "limited light"
when
    Item motionSensor changed to ON
then
    if (now.getMinuteOfDay() >= now.withTimeAtStartOfDay.plusDays(1).minusMinutes(1110).getMinuteOfDay() && now.getHourOfDay() < 23) {
        lightItem.sendCommand(ON)
    }
end

We compare to the minute from now.withTimeAtStartOfDay.plusDays(1) midnight tonight take away 1440 minutes in a days less 330 minutes (05:30) = 1100 minutes

I’ll do … I promise … :crossed_fingers:

Uff … sounds a very little bit confusing … :cold_sweat:

Anyway: thank you very much for your support! :slightly_smiling_face::+1: