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