Help with Trigger and Delay

Hi
I have an automation working for a door sensor that turns on the lights when the door is opened after sunset and turns them off after the door has been closed for 20 minutes.
The problem is that if the door is opened and closed again before the delay is complete the lights turn off as soon as the door is closed.

Automation is as below

  • alias: Entrance Light Off When Dark
    trigger:
    platform: state
    entity_id: sensor.garagehouse_door
    from: ‘Open’
    to: ‘Closed’
    condition:

    • condition: state
      entity_id: input_boolean.garagehousedoor
      state: ‘on’

    action:

    • delay: ‘00:20:00’
    • service: switch.turn_off
      entity_id: switch.front_door_3
    • service: light.turn_off
      entity_id: light.garage_main
    • service: input_boolean.turn_off
      entity_id: input_boolean.garagehousedoor

Many Thanks
Jman

First, when posting code, please format as instructed at the top of the page.

Next, things tend to not work so well if an automation is triggered when its actions are still running from a previous trigger event. You want to avoid that.

Although you don’t show it, I’m assuming you have another automation that turns the lights on when the door is opened. That is probably fine.

For this automation, what you want to do is to remove the delay from the actions, and add for: to the state trigger. Like this:

  trigger:
    platform: state
    entity_id: sensor.garagehouse_door
    from: 'Open'
    to: 'Closed'
    for:
      minutes: 20

Now the automation will not trigger until the door goes from Open to Closed, and stays Closed for 20 minutes. If the door is opened before the 20 minutes is up, the time will start over when the door is closed again.