Wait for time to pass action often fails breaking automation

I have an automation using a wait for time to pass (5 mins) action:

alias: shop left/middle/right lights on/off with kitchen door open/close
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.cs_kitchen_door
    from: 'off'
    to: 'on'
condition: []
action:
  - service: light.turn_on
    target:
      entity_id:
        - light.shop_left_lights
        - light.shop_middle_lights
        - light.shop_right_lights
  - wait_for_trigger:
      - platform: state
        entity_id: binary_sensor.cs_kitchen_door
        from: 'on'
        to: 'off'
  - delay:
      hours: 0
      minutes: 5
      seconds: 0
      milliseconds: 0
  - service: light.turn_off
    target:
      entity_id:
        - light.shop_left_lights
        - light.shop_middle_lights
        - light.shop_right_lights
mode: restart

that often just stops after the 5 minutes have passed:

or just fails/breaks at that time:

Is there a better way to handle this that would avoid this? The automation is just turning lights on when a door is open and turning them off 5 minutes after the door is closed unless the door is opened again then it resets.

Yes. Use a second automation:

trigger:
  - platform: state
    entity_id: binary_sensor.cs_kitchen_door
    from: 'on'
    to: 'off'
    for:
      minutes: 5
condition: []
action:
  - service: light.turn_off
    target:
      entity_id:
        - light.shop_left_lights
        - light.shop_middle_lights
        - light.shop_right_lights

This can also be combined with the first automation if you want:

trigger:
  - platform: state
    entity_id: binary_sensor.cs_kitchen_door
    from: 'off'
    to: 'on'
    id: 'on'
  - platform: state
    entity_id: binary_sensor.cs_kitchen_door
    from: 'on'
    to: 'off'
    for:
      minutes: 5
    id: 'off'
condition: []
action:
  - service: "light.turn_{{ trigger.id }}"
    target:
      entity_id:
        - light.shop_left_lights
        - light.shop_middle_lights
        - light.shop_right_lights

I’ll give that a try thanks. Why does my automation sometimes work?

Reloading automations or restarting Home Assistant while waiting?

Neither. I also have the same automation but for motion sensor (so no motion for one minute instead of door closed for 5) that work fine.

What mode would this new automation be? Single?

Yes single.

Is there a way to adapt this to motion sensors when you are waiting on multiple motion sensors to clear before turning the lights off:

alias: kitchen sink motion lighting
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.ms_kitchen
    from: 'off'
    to: 'on'
  - platform: state
    entity_id: binary_sensor.ms_kitchen_sink
    from: 'off'
    to: 'on'
condition:
  - condition: or
    conditions:
      - condition: sun
        after: sunset
        after_offset: '00:10:00'
      - condition: sun
        before: sunrise
        before_offset: '-00:10:00'
  - condition: state
    entity_id: light.kitchen_high_lights
    state: 'off'
action:
  - service: switch.turn_on
    target:
      entity_id: switch.kitchen_sink_lights
  - wait_for_trigger:
      - platform: state
        entity_id: binary_sensor.ms_kitchen
        from: 'on'
        to: 'off'
      - platform: state
        entity_id: binary_sensor.ms_kitchen_sink
        from: 'on'
        to: 'off'
  - delay:
      hours: 0
      minutes: 5
      seconds: 0
      milliseconds: 0
  - service: switch.turn_off
    target:
      entity_id: switch.kitchen_sink_lights
mode: restart

What you posted above would work fine for the on part but how would I adapt that to include two sensors in the off part?

Scratch that. You need to split this automation up in to one for turning the lights on and one for turning them off. Much simpler.

Thanks I’ll give this a try.

light.kitchen_high_lights is my override. If it is on I want it to stop this rule. If this rule has already fired and light.kitchen_high_lights it turned on I have another rule that overrides (turns off this automation and the lights it turned on) this rule.

I changed my post. Give me a second I’ll repost the automations.

I think this will do what you want.

trigger:
  - platform: state
    entity_id:
      - binary_sensor.ms_kitchen
      - binary_sensor.ms_kitchen_sink
    to: 'on'
    from: 'off'
condition:
  - condition: state
    entity_id: light.kitchen_high_lights  
    state: 'off'
action:
  - service: switch.turn_on
    target:
      entity_id: switch.kitchen_sink_lights


trigger:
  - platform: state
    entity_id:
      - binary_sensor.ms_kitchen
      - binary_sensor.ms_kitchen_sink
    to: 'off'
    from: 'on'
    for:
      minutes: 5
condition:
  - condition: state
    entity_id:
      - light.kitchen_high_lights  
      - binary_sensor.ms_kitchen
      - binary_sensor.ms_kitchen_sink
    state: 'off'
action:
  - service: switch.turn_off
    target:
      entity_id: switch.kitchen_sink_lights

Combined that with what I learned from above:

alias: kitchen sink motion lighting
description: ''
trigger:
  - platform: state
    entity_id:
      - binary_sensor.ms_kitchen
      - binary_sensor.ms_kitchen_sink
    from: 'off'
    to: 'on'
    id: 'on'
  - platform: state
    entity_id:
      - binary_sensor.ms_kitchen
      - binary_sensor.ms_kitchen_sink
    from: 'on'
    to: 'off'
    for:
      minutes: 5
    id: 'off'
condition:
  - condition: or
    conditions:
      - condition: sun
        after: sunset
        after_offset: '00:10:00'
      - condition: sun
        before: sunrise
        before_offset: '-00:10:00'
  - condition: state
    entity_id: light.kitchen_high_lights
    state: 'off'
action:
  - service: switch.turn_{{ trigger.id }}
    target:
      entity_id: switch.kitchen_sink_lights
mode: single

And this seems to be working.

When grouping triggers like this:

  - platform: state
    entity_id:
      - binary_sensor.ms_kitchen
      - binary_sensor.ms_kitchen_sink
    from: 'off'
    to: 'on'
    id: 'on'

is it an AND or OR situation?

In a trigger it is OR. It will trigger if either motion sensor turns on.
The same thing in a condition is AND. They all must have the specified state to pass.

How does this actually work then? Both motion sensors need to be “off” for 5 minutes before shutting the lights off. I just tested and it seems to work but I don’t see why/how if the trigger is OR?

The lights don’t turn off unless both motion sensors are off. The last trigger that happens when both sensors are off will be 5 minutes after the last motion sensor turns off. Then the condition passes and the lights will turn off. The automation ‘triggers’ for both sensors turning off but only runs when they are both off because of the condition.

Just tested it again twice and it’s not working that way. Soon as one trigger is off for 5 mins it shuts the lights off (even if the other trigger was reset before then).

Can you post your conditions.

I think I got it now. Here’s what I ended up with:

alias: kitchen sink motion lighting
description: ''
trigger:
  - platform: state
    entity_id:
      - binary_sensor.ms_kitchen
      - binary_sensor.ms_kitchen_sink
    from: 'off'
    to: 'on'
  - platform: state
    entity_id:
      - binary_sensor.ms_kitchen
      - binary_sensor.ms_kitchen_sink
    from: 'on'
    to: 'off'
    for:
      minutes: 5
condition:
  - condition: or
    conditions:
      - condition: sun
        after: sunset
        after_offset: '00:10:00'
      - condition: sun
        before: sunrise
        before_offset: '-00:10:00'
  - condition: state
    entity_id: light.kitchen_high_lights
    state: 'off'
action:
  - choose:
      - conditions:
          - condition: state
            entity_id:
              - binary_sensor.ms_kitchen
              - binary_sensor.ms_kitchen_sink
            state: 'off'
            for:
              minutes: 5
        sequence:
          - service: switch.turn_off
            target:
              entity_id: switch.kitchen_sink_lights
    default:
      - service: switch.turn_on
        target:
          entity_id: switch.kitchen_sink_lights
mode: single

You didn’t split the automations. You want the lights to turn off with no motion even during daylight hours if someone turned them on no?

Try these.

alias: kitchen sink motion lighting off
description: ''
trigger:
  - platform: state
    entity_id:
      - binary_sensor.ms_kitchen
      - binary_sensor.ms_kitchen_sink
    from: 'on'
    to: 'off'
    for:
      minutes: 5
condition:
  - condition: state
    entity_id:
      - binary_sensor.ms_kitchen
      - binary_sensor.ms_kitchen_sink
      - light.kitchen_high_lights
    state: 'off'
  - condition: state
    entity_id: switch.kitchen_sink_lights
    state: 'on'
action:    
action:
  - service: switch.turn_off
    target:
      entity_id: switch.kitchen_sink_lights
mode: single
alias: kitchen sink motion lighting on
description: ''
trigger:
  - platform: state
    entity_id:
      - binary_sensor.ms_kitchen
      - binary_sensor.ms_kitchen_sink
    from: 'off'
    to: 'on'
condition:
  - condition: or
    conditions:
      - condition: sun
        after: sunset
        after_offset: '00:10:00'
      - condition: sun
        before: sunrise
        before_offset: '-00:10:00'
  - condition: state
    entity_id: light.kitchen_high_lights
    state: 'off'
  - condition: state
    entity_id: switch.kitchen_sink_lights
    state: 'off'    
action:
  - service: switch.turn_on
    target:
      entity_id: switch.kitchen_sink_lights
mode: single