Zigbee2MQTT availability message notification using wildcard

Trying to figure out how to send a notification to my phone whenever a Zigbee device drops of the network with the device its name.
I have enabled the availability setting in Zigbee2MQTT.
In the automation the trigger would be:

platform: mqtt
topic: zigbee2mqtt/+/availability
payload: offline

So that any device that goes offline will trigger the automation. Now is there a way to get the friendly name to appear in the notifcation or is node-red a better option for this?

Great idea but i don’t know the answer:-)

I am using this: Offline detection for Z2M devices with last_seen

Found this: Automation Trigger Variables - Home Assistant
And using the example was able to make this automation. Gonna refine it a bit to just include the sensors instead of all the Zigbee devices.

alias: Zigbee Offline Notify
description: ""
trigger:
  - platform: mqtt
    topic: zigbee2mqtt/+/availability
    payload: offline
action:
  - service: notify.mobile_app_s20_charles
    data:
      message: |
        {{ trigger.topic.split('/')[1] }} is {{ trigger.payload }}
mode: single
3 Likes

Automation edited to work with just the sensors.
I named all my sensors wiht the text Sensor at the end. Like: Woonkamer Sensor

alias: Zigbee Sensor Offline Notify
description: ""
trigger:
  - platform: mqtt
    topic: zigbee2mqtt/+/availability
    payload: offline
condition:
  - condition: and
    conditions:
      - condition: template
        value_template: "{{ trigger.topic.split('/')[1][-6:] == 'Sensor'}} "
action:
  - service: notify.mobile_app_s20_charles
    data:
      message: |
        {{ trigger.topic.split('/')[1] }} is {{ trigger.payload }}
mode: queued
max: 10

1 Like

Thanks so much for sharing - this is exactly what I needed - and I think I also learnt that a + in mqtt topic is a wildcard?

Its a single level wildcard, for catching more levels you can use the # but that only works in 1 direction.

Thanks for this! Nice and simple. One thing to note, I do not have “Legacy mode” enabled in my zigbee2mqtt instance. This means that my payloads are in json format, and for me, offline needed to be changed to {“state”:“offline”} to trigger properly. So if you have legacy mode enabled, then the code posted should work as is. If you don’t have legacy mode enabled, then you need to adjust the trigger payload value.

I added the {“state”:“offline”} and it keeps giving me a error of “Message malformed: template value should be a string for dictionary value @ data[‘payload’]”

@IBot07 I ran into a similar issue, thought it was just me doing something stupid. I ended up doing it in the visual editor. Picture attached, after that I switched to the yaml view and the formatting is weird, but it does work. Screenshot of ui attached and then the resulting yaml code. I have a binary switch that turns on when ha is shutdown or rebooted, and then waits 30 seconds after ha is completely back up before turning back off. I check this switch for any automation that might be falsely triggered during a reboot if an integration or device takes a bit to be available for instance. That’s what switch.ha_shutdown_switch is.

alias: Notify - Zigbee Offline Notify
description: ""
trigger:
  - platform: mqtt
    topic: zigbee2mqtt/+/availability
    payload: "{\"state\":\"offline\"}"
condition:
  - condition: state
    entity_id: switch.ha_shutdown_switch
    state: "off"
action:
  - service: notify.mobile_app_sm_s908u
    data:
      message: |
        {{ trigger.topic.split('/')[1] }} is offline.
mode: single
6 Likes

this works well people, thanks.
I am wondering if there is a way to prevent spamming these notifications to me?
Be notified when a device goes offline, without hearing it every minute?
I could make a simple wait, but then i would miss if other devices go offline.

Maybe one of you guys have a solution to this.

Unless a device goes offline/online/offline you will only get 1 message.
Since the trigger is: going offline not being offline

yesterday i tried the automation above and got notified every 30s about the same device (power outlet plug of the brand frient). maybe it was being found and went offline again every few seconds, i dont know.
also when manually listening to zigbee2mqtt/+/availability for {“state”:“offline”} i saw that the device was getting spammed there.

It seems i am incorrect that it only triggers once. Using the topic to listen i see multiple online or offline messages form the same devices. Not all tho.

This might be a Z2M setting, in settings i have a 20 minute timeout and for battery devices a 90 minute timeout. So i never get spammed.