core: Automation at HA shutdown event does not work.
The problem
I have simple automation based on HA shutdown trigger.
trigger:
- platform: homeassistant
event: shutdown
Seems like it fires too late when some part of HA services has already stoped. The are even no traces that automation triggered.
I found some stale closed issues regarding this but I believe it should be fixed.
What version of Home Assistant Core has the issue?
core-2021.11
What type of installation are you running?
Home Assistant OS
Integration causing the issue
Automation
Link to integration documentation on our website
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 3
- Comments: 17 (8 by maintainers)
It’s not just that the logging doesn’t work, it is really hit-and miss depending on the action you try to execute in a shutdown automation, and due to the logging not working you tend to have no clue as to what has fired and what not. From searching the forums it seems the only thing more or less reliably working is sending a notification, and possibly sending something to MQTT (haven’t tried that yet). For example a call to set some z-wave parameters on shutdown completely fails, with a very undrscriptive error in the logs.
2023-01-07 15:13:02.436 ERROR (MainThread) [homeassistant.components.automation.hass_stopfixer] Hass Stopfixer: Error executing script. Unexpected error for call_service at pos 1: tuple index out of range Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 451, in _async_step await getattr(self, handler)() File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 684, in _async_call_service_step await service_task File "/usr/src/homeassistant/homeassistant/core.py", line 1755, in async_call task.result() File "/usr/src/homeassistant/homeassistant/core.py", line 1792, in _execute_service await cast(Callable[[ServiceCall], Awaitable[None]], handler.job.target)( File "/usr/src/homeassistant/homeassistant/components/zwave_js/services.py", line 441, in async_set_config_parameter raise_exceptions_from_results(nodes_list, results) File "/usr/src/homeassistant/homeassistant/components/zwave_js/services.py", line 91, in raise_exceptions_from_results lines = [ File "/usr/src/homeassistant/homeassistant/components/zwave_js/services.py", line 93, in <genexpr> f"{zwave_object} - {error.__class__.__name__}: {error.args[0]}"It would seem to me that the shutdown automation should be triggered before anything else starts shutting down inside home assistant, so that any things you want to be set before the shutdown occurs can be done. I guess that could introduce problems with it not shutting down at all if the automation is taking too long or anything… But wanting to save or set some things on shutdown and undo them on startup does seem to be a common use case… Creating your own automation that finally calls home assistant to shutdown works, but only if you do that from within home assistant, it doesn’t work when eg docker compose restarts the container(s). And docker doesn’t seem to have any kind of useful shutdown hook either to alleviate this.
At the very least, the docs could use some clarification on this, and ideally a list of what still can be done and what can’t…