How to access the Z2M "Available" field in Jinja?

Goal: To iterate over all Z2M devices, and generate alerts for any offline devices.

I have previously made this blueprint for the community, to notify when any Zigbee(ZHA), or ZWave devices have gone offline.

I was just looking at “nice” ways to extend this for Zigbee2Mqtt.

I see that Z2M has an “availability” “field”, but it is not part of the H.A entity, rather it is published separately as an MQTT topic, e.g: zigbee2mqtt/LOUNGE_MOTION_SENSOR/availability

My question is: How can I create a blueprint that will effectively iterate over all mqtt sensors, and then create a list of ones where availability==false

e.g I can generate a list of all MQTT entities easily enough (see code below), but I am not sure how to access MQTT payloads from within Jinja? (and I want this to be fully automatic - I don’t want to have to manually create a MQTT Sensor for each devicve)

{{ states | selectattr('entity_id', 'in', integration_entities('mqtt')) | map(attribute='entity_id') | list }}

Even easier if you remove all the unnecessary parts:

{{ integration_entities('mqtt') }}

If an entity is unavailable, its state value is unavailable. Therefore select the entities you want based on their state value.

For example, this lists all MQTT-based entities that are currently available (i.e. the entities that are not unavailable).

{{ expand(integration_entities('mqtt'))
   | selectattr('state', '!=', 'unavailable')
   | map(attribute='entity_id')
   | list }}