Why are my lights switching off when presence is still detected?!

I’m struggling to identify the issue with the automation below. I’m using a mmwave presence sensor to detect occupancy in the bathroom, the lights should turn on when detected (depending on time of day) and then turn off only after occupancy ends +5mins. However at the moment they just turn off regardless after the 5mins is up. I extracted the original code from the motion lights blueprint and turned it into an automation as I wanted to extend the options available in it.

Any pointers gratefully received!

alias: Presence Lights - Bathroom Mirror + Lights (5mins / Conditional)
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.presence_sensor_main_bathroom_presence
    to: "on"
condition: []
action:
  - alias: Turn on the light
    service: light.turn_on
    target:
      entity_id: light.main_bathroom_mirror
    data: {}
  - if:
      - condition: time
        after: "07:00:00"
        before: "22:00:00"
    then:
      - service: light.turn_on
        metadata: {}
        data: {}
        target:
          entity_id: light.main_bathroom_lights
  - alias: Wait until there is no motion from device
    wait_for_trigger:
      - platform: state
        entity_id:
          - binary_sensor.presence_sensor_main_bathroom_presence
        from: "on"
        to: "off"
    timeout:
      hours: 0
      minutes: 0
      seconds: 0
      milliseconds: 0
  - alias: Wait the number of seconds that has been set
    delay:
      hours: 0
      minutes: 5
      seconds: 0
      milliseconds: 0
  - alias: Turn off the light
    service: light.turn_off
    target:
      entity_id:
        - light.main_bathroom_mirror
        - light.main_bathroom_lights
    data: {}
mode: restart
max_exceeded: silent

Your wait is timing out immediately because you have specified a timeout of 0 seconds… delete the timeout completely and save the automation. Sometimes this happens due to a UI glitch if you simply place your cursor in any of the timeout unit fields.

A wait in an automation is almost always a bad idea.
What you should do is to have a second automation handling the switching off with a for, ie…

- alias: "Turn on if movement detected"
  trigger:
    - platform: state
      entity_id:
        - binary_sensor.presence_sensor_main_bathroom_presence
      from: "off"
      to: "on"
  condition:
    - condition: time
      after: "07:00:00"
      before: "22:00:00"
  action:
    - service: light.turn_on
      target:
        entity_id: light.main_bathroom_lights
          
- alias: "Turn off if no movement after 5 min"
  trigger:
    - platform: state
      entity_id:
        - binary_sensor.presence_sensor_main_bathroom_presence
      from: "on"
      to: "off"
      for: "00:05:00"
  action:
    - service: light.turn_off
      target:
        entity_id:
          - light.main_bathroom_mirror
          - light.main_bathroom_lights
  

Might be 2 automations, but it’s leaner and doesn’t rely on the automation restart if movement is re-dected inside the 5 min window.

If you delegate the 5 min delay to the “presence_sensor_main_bathroom_presence” binary sensor somehow, you can easily combine the 2 automations.

Thanks so much - I would never have spotted that was the issue! It’s all working now!

Interesting - thanks for your input, what’s the risk with using ‘restart’ mode?

For what it’s worth, rather than automations sitting waiting.
I prefer to use timers from the helper UI.

 - service: timer.start
   data:
     duration: "01:00:00"
   target:
     entity_id: timer.livingroom_lights   

Start the timer whenever motion is detected. If the timer was already running - this will RESTART the timer - providing you pass a duration.

Then when motion has stopped being detected

 - service: timer.start
   data:
     duration: "00:05:00"
   target:
     entity_id: timer.livingroom_lights

For turning the lights off I use a trigger:

 - platform: event
   event: timer.finished
   event_data:
     entity_id: timer.livingroom_lights
   id: timer_finished

This means I can also put the timers on the dashboard. So I can see how much time is left, and if I need to - manually end the timer, which will trigger the timer finished event and turn the lights off.

1 Like

Not sure it is a risk, but it’s a hack, because you rely on an implicit safeguard mechanism to implement business logic (the fact that presence can be detected while waiting to turn the lights off).