HA Shutdown Event

There is a home_assistant_stop event but it is recommended that:

“If you want to trigger automation on a Home Assistant stop event, we recommend using the special Home Assistant trigger instead of listening to these events.”

The following Home Assistant trigger:

automation:
  trigger:
    - platform: homeassistant
      event: shutdown

Fires when Home Assistant shuts down.

The question is…

How long do you get to perform any actions in any automation based on the shutdown trigger?

The best answer would be that HA knows when automations are triggering on this ‘event’ and so it waits until they are finished.

(But that would beg the, hopefully hypothetical(!), question… What happens if one of those automations has a wait that never ends?)

When you press the restart button, HA fires an event but it also immediately begins teminating things. A shutdown triggered automation may get triggered but most likely it will not. Since the shutdown process proceeds quite quickly, your automation will fail to run. The only option I see is to only use a script to restart HA and never use the HA restart button. The last thing your script would do is execute service: homeassstant.restart

alias: Home Assistant - Restart
sequence:
  - service: alexa_media.update_last_called
    data: {}
  - service: notify.persistent_notification
    data:
      title: Home Assistant
      message: Restarting...
  - service: script.notify_alexa_media
    data:
      message: home assistant is restarting
  - service: homeassistant.restart
    data: {}
mode: single
1 Like

The failure to trigger/execute automations that are triggered by HA shutdown appears to have finally been rectified in HA 2024.1.0! My non-working yet still in place HA shutdown automation finally started working!

alias: Home Assistant Shutdown
description: ""
trigger:
  - platform: homeassistant
    event: shutdown
condition: []
action:
  - service: persistent_notification.create
    data:
      title: Home Assistant
      message: Shutdown initiated
  - service: script.update_last_alexa_and_wait
    data: {}
  - service: script.notify_alexa_media
    data:
      message: shutting down
mode: single