Graceful Shutdown Sensor

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

OK, tried it. Still doesn’t trigger

type or paste code here

I don’t know what to tell you other than it works for me.

I’m using two Event Triggers (to detect homeassistant_started and homeassistant_stop) in a Trigger-based Template Sensor that records the time when Home Assistant starts and stops (i.e. shutdown).

I don’t use it in any automation, but in the mqtt integration it has the “Enable will message” option that apparently works fine.

You could set the will message to “Soft restart” and check it when the HA starts.
If topic = “Soft restart” do nothing and set the topic to “Running”
If topic = “Running” HA was not turned off properly, run your automations and set the topic to “Running”.

It wasn’t well explained, but I think it’s enough for you to get the idea.

Here’s a Trigger-based Template Sensor I recently created that reports one of two states on startup:

  • start if Home Assistant was shutdown normally prior to starting.
  • interrupt if Home Assistant was not shutdown normally prior to starting.

It also maintains a history of the ten most recent stop/started events, sorted in reverse chronological order (i.e. most recent is first). This attribute is optional.

- trigger:
    - platform: event
      event_type:
        - homeassistant_started
        - homeassistant_stop
  sensor:
    - name: Start Stop
      state: >
        {% if trigger.event.event_type == 'homeassistant_started' %}
          {{ iif(this.state|default('unknown') == 'shutdown', 'start', 'interrupt') }}
        {% else %}
          shutdown
        {% endif %}
      attributes:
        history: >
          {% set current = this.attributes.get('history', []) %}
          {% set new = [{
            "event": trigger.event.event_type[14:],
            "time": now().isoformat() }] %}
          {{ (new + current)[:10] }}

After creating the Trigger-based Template Sensor, it will initially report unknown. Initialize it by restarting Home Assistant (Developer Tools> YAML > Restart). On startup, it should now report start.

Here’s what it looks like on startup after Home Assistant was interrupted (i.e. not shutdown properly). The sensor reports interrupt. The history attribute shows there was no stop event prior to the most recent started event, indicating Home Assistant had been interrupted and didn’t get a chance to shutdown normally.

You can create an automation with a State Trigger to monitor the sensor when it changes to: interrupt and have it notify you that there was a recent interruption (potentially due to a power failure).


NOTE 1

Originally I tried to have the Event Trigger detect homeassistant_start which occurs before homeassistant_started but it failed to be detected. I assume it may be because the Template integration is loaded after the homeassistant_start event occurs so the Trigger-based Template Sensor never gets the opportunity to detect it.

NOTE 2

The history attribute is optional and can be eliminated without affecting the sensor’s operation. It’s included merely as a convenience; the sensor’s recorded History (i.e. in the database) shows the same information.

10 Likes

This is very clever. Thank you for sharing it.

That is indeed clever. And it seems to work for me too. Maybe because it’s not part of an automation. Whereas what I was trying was part of an automation.
Thanks very much.

1 Like

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.

I have used this idea to create an event state history for timers which will allow me to differentiate between timer.started, timer.paused, timer.restarted, timer.cancelled, and timer.finished instead of the timer states of active, idle, and paused.

Thank you, again, for the seed.

Hi Taras,

Ive been using this for the last week, and it works just fine (though I had an interrupt upon first restart ;-0

I just can not be sure I fully understand the state template, more specifically the iif section. Could you please explain what that does and why? Why is the ‘shutdown’ also listed in thehomeassistant_started if?

thx,

Here’s its theory of operation:

  • When Home Assistant is shutdown gracefully, the sensor sets its state to shutdown.

  • On startup, the sensor checks its previous state.

    • If it was shutdown, it changes its state to start.
    • If it was not shutdown, it changes its state to interrupt.

So if Home Assistant is not shutdown gracefully, the sensor never gets a chance to set its state to shutdown. It’s state remains as start (or unknown). On startup, the sensor detects that its previous state was not shutdown and reports interrupt.

The pseudo code for this:

        {% if trigger.event.event_type == 'homeassistant_started' %}
          {{ iif(this.state|default('unknown') == 'shutdown', 'start', 'interrupt') }}
        {% else %}
          shutdown
        {% endif %}

would be something like this:

IF Home Assistant just started
THEN
  IF my previous state was 'shutdown' (if I had no previous state then use 'unknown')
  THEN
    Set my state to 'start'
  ELSE 
    Set my state to 'interrupt'
ELSE 
  Set my state to 'shutdown'
1 Like

thanks for that last bit, because that (and the use of the ‘==’ operator) was what I am struggling with, especially given what is said in the warning box under:Templating - Home Assistant

I take it you’ve guarded that?