home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
73.41k stars 30.65k forks source link

Automation at HA shutdown event does not work. #59160

Closed skynetua closed 1 year ago

skynetua commented 3 years ago

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

https://www.home-assistant.io/docs/automation/trigger/

probot-home-assistant[bot] commented 3 years ago

trigger documentation trigger source (message by IssueLinks)

probot-home-assistant[bot] commented 3 years ago

automation documentation automation source (message by IssueLinks)

probot-home-assistant[bot] commented 3 years ago

Hey there @home-assistant/core, mind taking a look at this issue as it has been labeled with an integration (automation) you are listed as a code owner for? Thanks! (message by CodeOwnersMention)

martin3000 commented 2 years ago

I have home assistant core installation on ubuntu and I see that HA does not stop if I request a STOP in the server control menu. Some things are stopped, but the service does not stop completely. I have to stop it manually with "systemctl stop homeassistant".

emontnemery commented 2 years ago

@martin3000 Please open a separate issue for that, it seems completely unrelated to this issue.

martin3000 commented 2 years ago

@martin3000 Please open a separate issue for that, it seems completely unrelated to this issue.

If HA does not shut down, the automation is not triggered. It seems that this hint is absolutely related to the problem.

emontnemery commented 2 years ago

OP is not complaining about HA not shutting down, OP is complaining that the automation does not run. In your case, something is blocking HA shutdown. It should eventually shut down after a few minutes have you verified that it doesn't?

HepoH3 commented 2 years ago

I have the same problem, and afaik that's(1) a widespread(2) problem(3).

github-actions[bot] commented 2 years ago

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment đź‘Ť This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.

skynetua commented 2 years ago

The issue still exists.

github-actions[bot] commented 2 years ago

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment đź‘Ť This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.

skynetua commented 2 years ago

Still actual for Home Assistant 2022.8.0b3

rodrigogbs commented 2 years ago

I am facing the same problem since April 2022, the shutdown event doen't trigger the automation to pause uptime robot during a HA restart.

martin3000 commented 2 years ago

Th whole shutdown does not work. I have the HA core installation and if I want to stop HA, it does not stop. It always hangs while shutting down.

rodrigogbs commented 2 years ago

Guys, I realized that the trigger is “working” properly, but doesn’t logging! I created an automation triggered by HA shutdown and the action is to call a shell command service, and it is correctly called, but the automation doesn’t show the last trigger log.

Did you check if the action is executed, even if the automation doesn’t log the execution?

IetIesAai commented 1 year ago

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

issue-triage-workflows[bot] commented 1 year ago

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment đź‘Ť This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.