just so this is checked in core: trigger based template sensor no longer triggers on 'shutdown' · Issue #104116 · home-assistant/core · GitHub
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
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
need to get back to this once again, as today I realized a couple of details I hadn’t noticed before (luckily there aren’t too many interrupts…)
given the fact I use an icon template with the this state, the entity is nit showing what I want it to in case of the state interrupt.
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]}}
as TheFes explained to me once again:
the
this
variable is created on trigger for trigger based template sensors
so before the new state template is rendered, and will therefor have the “previous” state
and that is not what I hoped it would do, so I guess we can only repeat the state template and then use that for mdi: icon,
icon: >
{% if trigger.id == 'start' %}
mdi:{{iif(this.state|default('unknown') == 'shutdown','restart','restart-alert')}}
{% else %}
mdi:power
{% endif %}
or, truly self-reference:
{% set restart = states('sensor.graceful_shutdown_sensor') %}
mdi:restart{{'-alert' if restart == 'interrupt'}}
Ive now added the sensor entity to my recorder, so at least the system records it in history etc, but hope to adapt the config to show it in the history attribute too
also, I just discovered the the ‘interrupt’ is Not recorded in the history attribute. Checking the template again makes that obvious, but I hadn’t realized before.
You’ve got it working now, but if it helps here’s a little info on self-referencing:
As TheFes explained, the this
variable is defined at the trigger for trigger-based template sensors. But keep in mind the state machine isn’t updated until the entire sensor has been rendered, which includes both state
and attributes
. So in a trigger-based template sensor’s state
or attribute
definition, there is no difference between using this
compared to states('sensor.<the sensor being defined>')
because the state machine will still have the value of the sensor as it was previously rendered, which is exactly what is in this
.
The options to get the new state (or new attribute) are either:
Option 1: Duplicate the template code to calculate the new state (or new attribute)
Option 2: Re-render the entire sensor by adding itself as a trigger
Option 2 can be hazardous if the template will change every time it renders, because you’ll quickly put your HA instance into a death spiral. But if you avoid doing something stupid it will only update twice in rapid succession. On the 3rd render nothing should change, so it won’t trigger again. This may sound dangerous but this is already exactly how state-based template sensors work when you use this
in them.
To wrap this story up: the order of how states and attributes within a template sensor are rendered is irrelevant because no matter how you attempt to self-reference, you will be referencing stale data. You can’t reference the newly-rendered data without re-rendering the entire template sensor.