Delay activation of automations

I have some automations that need to be disabled until the states of my z-wave devices are fully initialized. Otherwise they will trigger every time I restart home assistant.

To do this I set those automations to initial_state: 'off' and use an automation to turn them back on like this:

- alias: after homeassistant startup
  trigger:
    platform: homeassistant
    event: start
  action:
    - delay:
        seconds: 20
    - service: automation.turn_on
      entity_id: automation.whatever1
    - service: automation.turn_on
      entity_id: automation.whatever2

And to activate them after we use the “reload automation”-button, we add another automation that will trigger the first one:

- alias: after automation reload
  trigger:
    platform: event
    event_type: call_service
    event_data:
      domain: automation
      service: reload
  action:
    - delay:
        seconds: 10
    - service: automation.trigger
      entity_id: automation.after_homeassistant_startup

We need a delay in the automation that is long enough that the automations have time to be initialized. Without a delay this didn’t work for me.

5 Likes

Just for everyone finding this: In current releases (0.114+) the second part (activate automations after reload) did not work for me - the automation was triggered, but initializing the automations led to the running automation to abort, so the automations were never activated. I solved this kinda hacky by calling a script that kept running while the automation was aborted:

Automation:

- alias: 'Activate ZWave automations after reload'
  trigger:
    platform: event
    event_type: call_service
    event_data:
      domain: automation
      service: reload
  action:
    - service: script.turn_on
      data_template:
        entity_id: script.zwave_auto_activate

Script:

zwave_auto_activate:
  alias: Z-Wave Automations Reload
  icon: "mdi:z-wave"
  description: 'Activate ZWave Automations after reload'
  sequence:
    - delay: 00:00:20  # Wait 20sec for initializing of automations
    - service: automation.turn_on
      entity_id: group.auto_zwave_refresh