Hallway Lighting - Automatic On/Off with Motion Sensor and Time Restriction (not working as desired)

Hi everyone,

I’m trying to set up an automation in Home Assistant to control the lighting in my hallway. I want the light to:

  • Turn on automatically when motion is detected in the hallway between 6:00 PM and 6:00 AM on all weekdays.
  • Stay on as long as motion is detected in the hallway.
  • Turn off automatically 45 seconds after motion was last detected.

Problem:

  • The light turns off after 45 seconds, even if I continue to move in the hallway.

What I’ve tried:

  • I have added a condition to check if the sensor is detecting “on” (motion).
  • I have adjusted the time delay to different values.

Automation code:

alias: "Gang- Nattmodus - 45s forsinkelse "
description: >-
  Nattmodus - Gangbelysning: Automatiseringen slår på lyset i gangen ved
  deteksjon av bevegelse etter kl. 20:00 og før kl. 06:00 på hverdager
  (mandag-fredag). Lyset vil forbli på i 45 sekunder før det slås av automatisk.
trigger:
  - platform: state
    entity_id:
      - binary_sensor.gangen
    from: "off"
    to: "on"
condition:
  - condition: time
    after: "18:00:00"
    before: "06:00:00"
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
      - sat
      - sun
  - condition: state
    entity_id: binary_sensor.gangen
    state: "on"
action:
  - service: light.turn_on
    metadata: {}
    data:
      brightness_pct: 20
    target:
      area_id: gang
  - delay: "00:00:45"
  - service: light.turn_off
    metadata: {}
    data: {}
    target:
      area_id: gang
mode: restart

Questions:

  • How can I get the automation to keep the light on as long as motion is detected in the hallway?
  • Is there anything else I can improve in my automation?

Additional information:

  • Home Assistant Core: 2024.3.0
  • Home Assistant Supervisor: 2024.03.0
  • Home Assistant Operating System: 12.1.rc1
  • Frontend: 20240306.0
  • Device: Raspberry Pi 5 8 gb
  • Motion sensor: Ikea Trådfri motion sensor
  • Lights: Various Ikea Trådfri lights

Thank you for your help

Based on the behaviour of my IKEA motion sensors, I would change the trigger to

trigger:
  - platform: state
    entity_id:
      - binary_sensor.gangen
    to: "on"

Why is there a condition that sensor must be on??

Adding that as a condition will prevent re-triggering…

Note that ikea tradfri sensor has a 1.5 minute delay build in which might interfere with your logic (meaning the sensor will stay “on” for 1.5 mins and will go to “off” state after that regardless if there is still motion; i.o.w. as long as it is in “on” mode it cannot re-detect motion).

So my advice would be to keep the light on for at least 2 mins or so, so the sensor has time to turn off and therefore can re-trigger the automation before the light turns off (so basically you have a 30s window to re-detect motion).

PS:With Zigbee2mqtt you can nowadays change that delay, i have set mine all to 10s which work a lot better: