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.”
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
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