Cannot delay an automation

Hi, I’m trying to create an automation, than first open downstairs door, and, 40 seconds later, upstairs door. I created a timer for 40 sc. called ‘upstairs’ in helpers, but, when I trigger the automation, it opens both doors, it doesn’t wait 40sc. Here it’s my code:

alias: OpenDoors
description: ""
trigger: []
condition: []
action:
  - type: turn_on
    device_id: xxxxxx
    entity_id: door1
    domain: switch
  - service: timer.start
    data: {}
    target:
      entity_id: timer.upstairs
  - type: turn_on
    device_id: xxxx
    entity_id: door2
    domain: door2
mode: single

Use:

  - delay: 00:00:40

instead of a timer?

3 Likes

A timer runs in the background, and you then have to wait for it to finish, as explained in the timer integration docs.

The correct approach for a delay is to use a delay as explained by Markus99.

1 Like

Was in a hurry earlier, here’s an automation I have to turn on my pool’s spa pump for 10 minutes:

  action:
  - data:
      entity_id: switch.spa_pump
    service: switch.turn_on
  - delay: 00:10:00
  - data:
      entity_id: switch.spa_pump
    service: switch.turn_off

Worth noting that this way will not survive a restart, but for 40sec low likelihood this will be a problem.

1 Like

I’m sorry, but I forgot to explain that I want a timer to stop if accidentally I press it, to stop opening upstairs door. With the delay, there is no way back, that’s why I want a timer that trigger a picture in lovelace, with a click to stop all automation if you trigger by error…

I use a timer to turn on the bathroom fan after 3 minutes.

This starts the timer

- id: '1612134477709'
  alias: Half Bath Timer Activation
  description: Activate the timer for the half bath
  trigger:
  - platform: state
    entity_id: switch.half_bath_light
    from: 'off'
    to: 'on'
  condition: []
  action:
  - service: timer.start
    data:
      duration: '180'
    target:
      entity_id: timer.halfbath_timer
  mode: single

This activates the fan if necessary

- id: '1624471421039'
  alias: Half Bath Fan Run
  description: Turn on Half bath fan if light was turned after after being on for
    at least 3 minutes
  trigger:
  - platform: state
    entity_id: switch.half_bath_light
    from: 'on'
    to: 'off'
  condition:
  - condition: state
    entity_id: timer.halfbath_timer
    state: idle
  action:
  - service: switch.turn_on
    target:
      entity_id: switch.half_bath_fan
  - delay:
      seconds: '{{ states(''input_number.fan_runtime'')| int }}'
  - service: switch.turn_off
    target:
      entity_id: switch.half_bath_fan
  mode: singlee
1 Like

A timer doesn’t make the automation (or script ) “wait”.

The service call timer.start makes a timer begin its countdown to zero (based on the timer’s duration). Any action appearing after timer.start is executed immediately (the automation doesn’t wait for the timer to finish).

When the timer finishes counting down to zero, it reports it has finished by producing a timer.finished event. If you want an automation to be triggered by a timer’s “finished” event, you would use an Event Trigger.

An automation executes its actions in sequence. However if it encounters a delay statement, it will pause for the length of the delay and then resume executing any remaining actions.

If Home Assistant is restarted or automations are reloaded while the delay is in progress, the delay is immediately cancelled and the automation is terminated (meaning any remaining actions are not executed). In contrast, a timer is able to survive a restart/reload (provided you configure it correctly).


EDIT

If you only want the automation to wait for 40 seconds, use a delay because it’s simpler than employing a timer. Forty seconds is a short time period and the chance of a restart/reload interrupting it during that narrow 40-second window of opportunity is low.

2 Likes

You don’t have an “automation”. You have a “script”. An “automation” with no trigger is just a “script”.

a script will run each action step sequentially once activated.

in your script above you have this:

  • turn on door 1 switch
  • start the timer
  • turn on door 2 switch

there is nothing there to tell it to wait until the timer finishes before running the last step. So it just goes from one step to the next and both doors open at pretty much the same time and you have a 40 second timer running with no purpose.

as suggested you can either just use a delay of 40 seconds or you need to use a wait for trigger event of the timer finishing before executing the last step.

1 Like