Setting MQTT Birth and Last will with the new MQTT Integration

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.

Indeed - the homeassistant shutdown event is pretty pointless tbh.

So it will look like this?

            {
                "connection_class": "local_push",
                "data": {
                    "broker": "core-mosquitto",
                    "discovery": true,
                    "password": "PRIVATE",
                    "port": 1883,
                    "protocol": "3.1.1",
                    "username": "homeassistant",
                    "birth_message": {
                        "topic": "hass/status",
                        "payload": "online",
                        "qos": 0,
                        "retain": false
                    },
                    "will_message": {
                        "topic": "hass/status",
                        "payload": "offline",
                        "qos": 0,
                        "retain": false
                    },
                },
                "domain": "mqtt",
                "entry_id": "3c48f9e8137d4d63a80b34a670076368",
                "options": {},
                "source": "hassio",
                "system_options": {
                    "disable_new_entities": false
                },
                "title": "Mosquitto broker",
                "unique_id": null,
                "version": 1
            },

Technically it should be…

            {
                "connection_class": "local_push",
                "data": {
                    "broker": "core-mosquitto",
                    "discovery": true,
                    "password": "PRIVATE",
                    "port": 1883,
                    "protocol": "3.1.1",
                    "username": "homeassistant",
                    "birth_message": {
                        "topic": "hass/status",
                        "payload": "online",
                        "qos": 0,
                        "retain": false
                    },
                    "will_message": {
                        "topic": "hass/status",
                        "payload": "offline",
                        "qos": 0,
                        "retain": false
                    }
                },
                "domain": "mqtt",
                "entry_id": "3c48f9e8137d4d63a80b34a670076368",
                "options": {},
                "source": "hassio",
                "system_options": {
                    "disable_new_entities": false
                },
                "title": "Mosquitto broker",
                "unique_id": null,
                "version": 1
            },

You have a comma after the will block but no more key-value-pairs, and the JSON parser is picky about syntax. Other than that it looks correct.

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.

I have add those 2 automations:

###########################################################################################
#
# 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

Hope this will help

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.

Hope my explanation is correct.

1 Like