Graceful Shutdown Sensor

just so this is checked in core: trigger based template sensor no longer triggers on 'shutdown' · Issue #104116 · home-assistant/core · GitHub

2 Likes

Thanks for posting it. Lately I have far less free time to contribute to this forum, reporting issues, etc.

I’m hoping the cause of the problem isn’t a design decision to shutdown the Template integration before issuing the homeassistant_stop event.

I tested 3 workarounds:
1 - Automation + MQTT: Create an MQTT sensor and update/check its status using start/shutdown events.
Problem: the sensor is not being read at start, despite the MQTT state being correct, the sensor remains unknown, causing the automation to set the state to interrupted. Any idea?

2 - Automation + text helper: Same logic as above.
Problem: Strange behavior, it always returns to the state before it was shutdown, it seems that the state set by the automation is not persistent.

3 - Automation + shell command + python_script:
It seems to be working well:

It seems that homeassistant_stop event works again, at least in 2023.12.1.
There is one more caveat when using a template sensor, though. Home Assistant itself does not immediately write the new sensor state to the disk after it has been changed (e.g. set to start after a graceful reboot). If a power failure happens at this moment, upon the next startup the state of the sensor will be start instead of ‘interrupt’ because the recent stored value in entity registry was shutdown.

That’s good news. I’ll check it when I upgrade from 2023.11.

Has this ever happened to you?

Because this alleged behavior hasn’t been observed and reported since the example was posted in March 2023.

in 2024.1 beta a pr was made to the Trigger Home Assistant shutdown automations right before the stop event instead of during it by tetele · Pull Request #91165 · home-assistant/core · GitHub

short discussion suggested to use

trigger:
  - platform: homeassistant
    event: shutdown
  - platform: homeassistant
    event: start

so hopefully this would be fixed now?

can confirm this to work (note I added a trigger.id as there are no trigger variables to the homeassistant platform to use?). See the top 2 attributes in the screen tp be the result of this latest version:

template:

  - trigger:
#       platform: event
#       event_type:
#         - homeassistant_started
#         - homeassistant_stop
      - platform: homeassistant
        event: shutdown
        id: shutdown
      - platform: homeassistant
        event: start
        id: start

    sensor:

      - unique_id: graceful_shutdown_sensor
        state: >
          {% if trigger.id == 'start' %}
            {{iif(this.state|default('unknown') == 'shutdown','start','interrupt')}}
          {% else %}
            shutdown
          {% endif %}
        icon: >
          mdi:restart{{'-alert' if this.state == 'interrupt'}}
        attributes:
          history: >
            {% set current = this.attributes.get('history',[]) %}
            {% set new = [{
              "event": trigger.id,
              "time": now().isoformat()}] %}
            {{(new + current)[:10]}}

The shutdown trigger is fixed in 2024.1 and should be used preferably. do keep in mind what Frenck mentioned in the beta:

unless the automation takes over 20 seconds, in that case, the automation is aborted to not block the shutdown process

3 Likes

Hi Marius,
Thanks for making this suggestion.
I have implemented your exact code but the state never reaches “interrupt” even though I just pull the plug on the power supply.
Also it seems your screenshot with history does not match the above code since it shows “event: stop” but the trigger ids only include “start” and “shutdown”. Just curious?

In my case, I seem to get a shutdown state briefly before the start state and this causes the interrupt to not be triggered. When I do normal reboot, the shutdown state duration is about 1 min - 1.5 min, but when I just pull the plug on the power supply and then connect it, I still get a shutdown state but it only lasts around 20 sec.

Any idea why this happens? And is anyone else seeing this behavior?

hmm, seems you are right… tbh, I hadn’t even realized that, and cant remember now how that would have been an option before, I need to go to the backups of that time.

currently I also only see this:

edit

@emkaywest just read the thread above, on the change of the trigger events. The previous screenshot I posted was made when the former events still were like that.

summary:

template:

  - trigger:
#       platform: event
#       event_type:
#         - homeassistant_started
#         - homeassistant_stop
      - platform: homeassistant
        event: shutdown
        id: shutdown
      - platform: homeassistant
        event: start
        id: start