Send "device offline" notifications based on MQTT LWT

Using tasmota devices with mqtt, they send LWT (Last Will and Testament) messages when they leave.
e.g. this retained message

gen-01/tele/LWT Online

becomes

gen-01/tele/LWT Offline

I would like to use this to send notifications when particular devices go offline. I’m not sure though if these LWT messages are being noted by hassio… I have used auto-discovery for the tasmota devices, so I’m not even sure what the complete configuration is for these. I have been searching, but the mqtt device tracker seems to be more for identifying locations and not for identifying online/offline. Any ideas or examples? :slight_smile:

Since I’m not using Tasmota I’m unable to verify it, but I’d suggest you check if auto discovery already created some binary_sensor for you that solves your problem. If it did not the following should do the trick

binary_sensor:
  - platform: mqtt
    name: 'robin13_tasmota_connectivity'
    state_topic: 'gen-01/tele/LWT'
    payload_on: 'Online'
    payload_off: 'Offline'
    qos: 1
    device_class: connectivity

You can work with this sensor then :slight_smile:

If you want an example how to directly use MQTT in an automation you can do

automation:
  trigger:
    platform: mqtt
    topic: gen-01/tele/LWT
    payload: "Offline"
    ...

Thanks for the ideas @Florian - this looks like a way to do it, but I think there must be a more elegant way… There is no binary_sensor created from the auto-discover.

The autodiscover mqtt messages are:

homeassistant/sensor/295AB4_ANALOG_A0/config {"name":"gen-01 ANALOG A0","stat_t":"~SENSOR","avty_t":"~LWT","pl_avail":"Online","pl_not_avail":"Offline","uniq_id":"295AB4_ANALOG_A0","device":{"identifiers":["295AB4"],"connections":[["mac","3C:71:BF:29:5A:B4"]]},"~":"gen-01/tele/","unit_of_meas":" ","val_tpl":"{{value_json['ANALOG'].A0}}"}
homeassistant/sensor/295AB4_DS18S20_Id/config {"name":"gen-01 DS18S20 Id","stat_t":"~SENSOR","avty_t":"~LWT","pl_avail":"Online","pl_not_avail":"Offline","uniq_id":"295AB4_DS18S20_Id","device":{"identifiers":["295AB4"],"connections":[["mac","3C:71:BF:29:5A:B4"]]},"~":"gen-01/tele/","unit_of_meas":" ","val_tpl":"{{value_json['DS18S20'].Id}}"}
homeassistant/sensor/295AB4_DS18S20_Temperature/config {"name":"gen-01 DS18S20 Temperature","stat_t":"~SENSOR","avty_t":"~LWT","pl_avail":"Online","pl_not_avail":"Offline","uniq_id":"295AB4_DS18S20_Temperature","device":{"identifiers":["295AB4"],"connections":[["mac","3C:71:BF:29:5A:B4"]]},"~":"gen-01/tele/","unit_of_meas":"°C","val_tpl":"{{value_json['DS18S20'].Temperature}}","dev_cla":"temperature"}
homeassistant/sensor/295AB4_status/config {"name":"gen-01 status","stat_t":"~HASS_STATE","avty_t":"~LWT","pl_avail":"Online","pl_not_avail":"Offline","json_attributes_topic":"~HASS_STATE","unit_of_meas":" ","val_tpl":"{{value_json['RSSI']}}","ic":"mdi:information-outline","uniq_id":"295AB4_status","device":{"identifiers":["295AB4"],"connections":[["mac","3C:71:BF:29:5A:B4"]],"name":"gen-01","model":"Generic","sw_version":"7.1.1(sensors)","manufacturer":"Tasmota"},"~":"gen-01/tele/"}

I think the relevant part of that is "pl_avail":"Online","pl_not_avail":"Offline" but I’m still not sure how to trigger on it…

I’ll doubt that. This should just be the declaration what is send as payload (therefore “pl_”) when the device is available (“Online”) and what is send when it is not (“Offline”).

You should monitor the topic mentioned in your first post to get the device’s state.

When you don’t want some kind of visual representation inside Home Assistant the automation example above is an easy way to trigger a notification as soon as “Offline” is posted on the topic.

I’m not sure what you mean when you’re searching for a more elegant solution, but whatever it is I’m sure you need to listen to the topic you’ve posted first and not to the auto discovery message itself.

I hope you will find what you are searching for :slight_smile:

I think I have a solution (which seems to work). It seems that as long as the device is online the state of the *_status sensor is the RSSI, but when the LWT message is sent that it is offline, the state changes to ‘unavailable’

- alias: notify_when_device_unavailable
  initial_state: 'on'
  trigger:
    platform: state
    entity_id:
      - sensor.sp111_01_status
      - sensor.sp111_02_status
      - sensor.sp111_03_status
      - sensor.sp111_04_status
    to: 'unavailable'
  action:
  - service: notify.notify
    data_template:
      message: "{{ trigger.to_state.attributes.friendly_name }} has gone offline"

It seems to work, but it would be wonderful if someone could confirm that the logic is correct here. :slight_smile: