Best way for handling a delay?

Hello guys.
Been reading around regarding the delay/timer thing….
I’m planning a small automation where a timer runs out after 2 min - but can be reset if it’s triggered again before it runs out.

I’ve read around, so far I’m at using delays or a timer - as I understand both needs to be canceled if they exist - so I need to make a if statement to see if it’s already created before starting it - I assumed it’s same for both timers and delays?

Also I’ve read someone states that it’s easier to use a time stamp when it was triggered and set an action after some time….

Can anyone please explain a bit?
Thanks :slight_smile:

If you use a delay in an automation or script you can use the “restart” mode. e.g.

action:
  - delay: 120
mode: restart

Note that reloading/saving automations or restarting home assistant will cancel the delay.

Though this will change soon to only reload/save any automation that has been edited.

3 Likes

That is great news :slight_smile:

Yeah it was a WTH that was picked up by the devs.

1 Like

This script is triggered by a helper, which is based on the door state. Any idea why it is instead turning the light off when the door is opened and closed again?

I’m mobile, so hopefully this formats correctly.

alias: Bathroom - Light - Toggle
sequence:
  - if:
      - condition: state
        entity_id: input_boolean.bathroom_occupied
        state: "on"
    then:
      - service: light.turn_on
        data:
          brightness_pct: 100
        target:
          entity_id: light.bathroom
    else:
      - if:
          - condition: state
            entity_id: sun.sun
            state: below_horizon
        then:
          - service: scene.turn_on
            data: {}
            target:
              entity_id: scene.bathroom_idle
        else:
          - delay:
              hours: 0
              minutes: 1
              seconds: 30
              milliseconds: 0
          - service: light.turn_off
            data: {}
            target:
              entity_id: light.bathroom
mode: restart