Graceful Shutdown Sensor

I have an automation that I want to run when HA restarts after a power outage. But at the moment I cannot see a way to detect when that has happened. I’ve tried setting a toggle when the shutdown event fires so I can check it at start up and run the automation (which includes re-setting the toggle) but that doesn’t work because the by the time the shutdown event is triggered the system is sufficiently far through shutting down that the toggle doesn’t get set.

So could we have a binary sensor or some such toggle set by the system when a shutdown is requested that we can then check on startup and reset via a start up automation please.

trigger:
  - platform: homeassistant
    event: start
condition: []
action:

I use that on every automation I want to be done after a HA restart. You could do anything based on this…

No need for any toggles :wink:

I think you missed my point @BebeMischa . I use that too. But I have an automation that I want to run ONLY if HA has restarted after a power outage, not after EVERY restart. So I want the toggle to go in the ‘condition’ part of that automation.

I see, well, that would be far behind my capacity :slight_smile:

I don’t think a power outage to HA would allow the system to do anything predictable - it stops; it does not run the shutdown service (there is no power to do this). That’s why I have a UPS to keep my HA running if there is a power outage. The UPS gives me the time to detect the outage and do specific things (notify me, set any conditions/states for restart, shutdown the system gracefully, etc.).

1 Like

Or you could do something like this:

These 2 makes no sense to me,

  1. A power-outages usually comes “out-of-the blue” no way your system will ever “request” a shutdown ( unless you have a UPS
  2. However , normally , there will be “notification” in system-log files saying something like ( the system was unexpected shut down / unexpected shutdown / or similar … based upon this you could trigger a script / automation during startup of HA

Can you post the code you’re using to set your “clean shutdown” flag (toggle) at shutdown? I know there are some settings which survive a reboot, like input_number, and some which don’t.

I appear to have not made my proposal clear. Let me try again…

Assume we have a sensor binary_sensor.graceful_shutdown
When a system shutdown is initiated, this sensor is set to ‘true’.

When HA restarts, my automation checks the condition of binary_sensor.graceful_shutdown
If ‘true’ then take no action except to set binary_sensor.graceful_shutdown ‘false’.
If ‘false’ then execute the restart_after_outage automation and then set binary_sensor.graceful_shutdown ‘false’.

So if there is a power outage, when HA restarts and restores the status of binary_sensor.graceful_shutdown it will be found to be ‘false’ and my automation executes.

Thank you for directing me to the file sensor suggestion @MaxK . That looks like it should work. I may indeed use that as a workaround but it seems to me less elegant than the system maintaining a binary_sensor specifically for this purpose.

1 Like

Looking again at the file sensor suggestion I’m not sure that will work because it also relies on an automation triggered by the event ‘shutdown’ which in my experience so far does not get executed. But I’ll try again with that code and see what happens

I agree a binary_sensor would be a more elegant solution.

I have not tried the file sensor version but, conceptually it should work because the last entry in the log would not be “shutdown” if there was a power failure.

Yes you also have to take into consideration, “restart/reboot” do to updates

EDIT: Regardless of how and/or which OS-file to check for “abrupt” shutdown/reboot, i Voted , for whatever solution that will be “implemented” … So HA-environment knows that it “rebooted/restarted” after a “abrupt/failure/crash” State :+1:

… Suddenly i actually find it weird, that this is not “obviously” shown in logs, should be with ! WARNING notification under “Notification”

Yes, I understood that. What I’m not clear on is whether and under what conditions binary sensors are preserved across shutdowns and reboots. I’ve had to resort to using input_number or input_text to ensure values are retained.

I’m no expert. For all I know, there are even better ways, which is why I suggested posting your automation code. There’s a lot of knowledge on this forum, and I’ve found many here are willing to help improve or correct anything that’s posted.

alias: Utility - Hass Shutdown Routine
description: Set Boolean to show system closed down cleanly
trigger:
  - platform: homeassistant
    event: shutdown
condition: []
action:
  - service: input_boolean.turn_on
    data: {}
    target:
      entity_id: input_boolean.ha_clean_shutdown
mode: single

This is my code.
This automation never even triggers on shutdown

I have had better luck using an Event Trigger to detect the homeassistant_stop event.

  - platform: event
    event_type: homeassistant_stop

You may have already thought about this but, the system could crash and restart for other reasons besides a power failure (hardware error, software error, etc.). Not sure if that would have an impact on your restart_after_outage automation.

Perhaps you can create a script that sets the boolean then restarts.
It’s a bit of an inconvenience but if you place the script as a button on the dashboard then at least it’s visible and easy to get to.

Here’s a clever way to know if a config is valid before restarting

Thanks @123 , I’ll give that a try

Thanks @Hellis81 , I am considering doing that. But not an ideal solution.
Would also need to find a way to cater for a restart after installing a new version.

OK, tried it. Still doesn’t trigger

alias: Utility - HA Shutdown Routine 2
description: Set Boolean to show system closed down cleanly
trigger:
  - platform: event
    event_type: homeassistant_stop
condition: []
action:
  - service: input_boolean.turn_on
    data: {}
    target:
      entity_id: input_boolean.ha_clean_shutdown
mode: single