Working with Device's Birth, LWT?

My apologies if this duplicates other questions. I’ve seen people asking questions regarding notification when devices go offline, but searches regarding a device’s birth and LWT messages are difficult as the results refer to Home Assistant’s own birth and LWT messages.

My question: can the MQTT Integration automatically handle a device’s birth and LWT messages? If not (and it doesn’t appear to me that it can), do people have suggestions as to how to handle them?

I’m looking for ways to be notified when a device has gone offline. The automatic send of a LWT message by the MQTT broker seems like a good method, if there’s a way to easily handle those messages in Home Assistant. Given that I’m using MQTT Discovery for my devices, I’m really hoping for something very automated, but beggars can’t be choosers. Or something like that. :grinning:

Thanks!

You should get a “unavailable” state on the devices when they go offline.

For MQTT devices that is handled by the LWT message automatically in the broker, which sends out the LWT message to all clients subscribed to that topic. And since HA is usually subscribed to the topic for the devices it should also receive those messages.

So in your automation you need to check for the state being “unavailable”.

But home assistant remembers last state and it does not change it.
I turn off one mqtt device for more than half of hour and home assistant did not change state to unavailable

Does that device send a proper LWT message in that half hour ?

Its is a shelly1 switch and i did not find any way for device to send that message.

Do you use the Shelly discovery script ?

No, because devices are on another network using a VPS / VM and the devices are in my home but the vm is in another country xD
Also after i discover them this script will not run anymore ( i think ). I need something to check is device connected or not.

I fount that there is a expire_after MQTT Sensor - Home Assistant option for MQTT Sensors. But i did not found one for MQTT Switch. Is there similar option for MQTT Switch ?

The only think that is come to mind right now is to get state_topic: "shellies/shelly1-id/relay/0" and create a mqtt sensor with it and add a expire_after

I asked, because the discovery script does create an availability as part of the discovery message.

Can’t check myself, as all my Shellies run Tasmota.

I think this is most simple/stupid work around.
It give you constant return of information in about every 30s, and when house power goes down ( for example ) HA will change state.
Stupid thing is that it needs another card to show the actual status.
If HA Devs add expire_after: 60 in a MQTT Switch will be great!

So yea this is my workaround:

mqtt:
  sensor:
    ### Shelly Switches availability
    # Physiical device switch
    - unique_id: shelly1-switch-availability
      name: shelly1-availability
      state_topic: "shellies/shelly1-ID/relay/0"
      expire_after: 60

Still im open for suggestions