Trigger with "for" template and midnight problem

I have a lamp that tuns on at certain time of the night every day. Then it turns off if no motion has been registered by a motion sensor for 20 minutes. The whole thing works fine as long as the “for” period does not go over midnight. If the lamp is on and motion is detected at 23:55 then the lamp will not turn off at 00:15 as expected; actually it will not turn off at all. The strangest thing is that if the lamp is on and motion detector gets triggered at 23:05 and at 00:05 then it will not work either that is, the lamp will not turn off at 00:25 as expected.

  - alias: "Decoration time to off time"
# It only works until midnight
    trigger:
      platform: time
      at: '22:30:00'
    condition:
      condition: state
      entity_id: group.indoors_motion
      state: 'off'
      for:
        minutes: 20
    action:
      - service: script.decoration_off

Any hint appreciated.

That’s because you’re telling it to turn off at a specific time only if motion hasn’t been detected for 20 mins.

Really you want it the other way round?

  - alias: "Decoration time to off time"
# It only works until midnight
    trigger:
      platform: state
      entity_id: group.indoors_motion
      state: 'off'
      for:
        minutes: 20
    condition:
      condition: time
      after: '22:30:00'
      before: '06:00:00'
    action:
      - service: script.decoration_off

So that way if motion isn’t detected for 20 minutes and it’s after 22:30:00 it will turn off. You also need a before time due to the switch over to midnight and with just the “after: ‘22:30’00’” this becomes false after midnight as it’s then before 22:30:00.

Or you could do an and condition and use the sunrise as a reference like I do.

2 Likes

That’s an interesting approach I’m to try. The only drawback I see is that your proposal would be triggered almost continuously 24h a day (motion sensor continuously triggering it). I wonder if it is to impact the performance of other automations.

Well it’s the only way it will work.

Like I say your approach will only turn the light off at 22:30 if there’s no motion. So it only triggers once.

But no. There’s no impact on performance. I have 5 motion sensors all working this way and they work perfectly.