Was 100% miss here. I’m fairly certain that the first thing that happens when you hit shutdown is it stops everything, and then sends the ‘event’ to say it’s done it. This, of course, means that the automations have already stopped working so they never trigger.
I keep meaning to file a bug report, but basically I’ve never (the whole time I’ve used homeassistant, ~4 years) been able to get an automation to react to the shutdown event.
I couldn’t remember if they ever fixed the shutdown event order. It looks like the MQTT client is shutdown either before or during automation execution. The only viable solution on the issues page is to use a shell process, since those can be fired outside of the MQTT client by using the mosquitto client.
There was no follow up from the devs and it was auto-closed and locked (, which I feel is a terrible feature of github,) so most likely they don’t have a good solution for it.
Yes saw that as wel… Also read that the will not always work… Maybe a idea to make a feature request to add this hard in the mqtt broker addon of HA itself.
###########################################################################################
#
# SEND A BIRTH SIGNAL MQTT OUT AND ALSO WHEN THERE IS A WILL SIGNAL WHEN OFFLINE HA
#
###########################################################################################
- alias: "System - HA Birth MQTT Signal"
trigger:
- platform: homeassistant
event: start
action:
- service: mqtt.publish
data:
topic: "hass/status"
payload: "online"
- alias: "System - HA Will MQTT Signal"
trigger:
- platform: homeassistant
event: shutdown
action:
- service: mqtt.publish
data:
topic: "hass/status"
payload: "offline"
qos: 0
retain: false
Ive had no issue with the birth message, as that is a function of the MQTT client protocol, so I do not think the startup automation is necessary, the shutdown automation calling the MQTT service generally does not work because by the time the service is called the client is shutdown.
The only real option I can think of is a “Feature Request” and that is to have the HA mqtt client code support an option to publish the will on close, this wouldn’t be a protocol violation, just a custom feature for HA.