How to detect if any MQTT devices are offline?

I’m wondering if there’s a way to get/parse all (zigbee2)mqtt devices whose availability is offline?

I’d like to create a sensor like this one for dead Z-Wave nodes:
sensor:

  - name: "Dead Z-Wave Devices"
    unique_id: dead_zwave_devices
    unit_of_measurement: entities
    state: >
      {% if state_attr('sensor.dead_z_wave_devices','entity_id') != none %}
        {{ state_attr('sensor.dead_z_wave_devices','entity_id') | count }}
      {% else %}
        {{ 0 }}
      {% endif %}
    attributes:
      entity_id: >
        {% set exclude_filter = ['sensor.700_series_based_controller_node_status'] %}
        {{
          expand(integration_entities('Z-Wave JS') )
          | rejectattr("entity_id", "in", exclude_filter)
          | selectattr("entity_id", "search", "node_status")
          | selectattr('state', 'in', 'dead, unavailable, unknown')
          | map(attribute="object_id")
          | map('regex_replace', find='(.*)_node_status', replace='button.\\1_ping', ignorecase=False)
          | list
        }}

I tried expand(integration_entities('mqtt') but the result is all templates

[<template TemplateState(<state update.aqara_fp1_presence_sensor=off; auto_update=False, installed_version=54, in_progress=False, latest_version=54, release_summary=None, release_url=None, skipped_version=None, title=None, action=None, approach_distance=far, device_temperature=21, linkquality=76, monitoring_mode=undirected, motion_sensitivity=high, power_outage_count=8, presence=False, presence_event=leave, update=installed_version=54, latest_version=54, state=idle, update_available=None, device_class=firmware, entity_picture=https://github.com/Koenkk/zigbee2mqtt/raw/master/images/logo.png, friendly_name=Aqara FP1 Presence Sensor, supported_features=1 @ 2023-08-13T16:07:16.904445-07:00>)>, <template TemplateState(<state update.body_sensor=off; auto_update=False, installed_version=26, in_progress=False, latest_version=26, release_summary=None, release_url=None, skipped_version=None, title=None, battery=100, detection_interval=60, device_temperature=28, linkquality=47, motion_sensitivity=medium, occupancy=False, power_outage_count=18, update=installed_version=26, latest_version=26, state=idle, update_available=None, voltage=3142, device_class=firmware, entity_picture=https://github.com/Koenkk/zigbee2mqtt/raw/master/images/logo.png, friendly_name=Body Sensor, supported_features=1 @ 2023-08-13T16:10:41.508996-07:00>)>, <template TemplateState(<state update.phillips_hue_switch=off; auto_update=False, installed_version=33565954, in_progress=False, latest_version=33565954, release_summary=None, release_url=None, skipped_version=None, title=None, action=, battery=100, linkquality=65, update=installed_version=33565954, latest_version=33565954, state=idle, update_available=None, device_class=firmware, entity_picture=https://github.com/Koenkk/zigbee2mqtt/raw/master/images/logo.png, friendly_name=Phillips Hue Switch, supported_features=1 @ 2023-08-13T16:10:41.462135-07:00>)>, <template TemplateState(<state update.aquarium_light=unavailable; device_class=firmware, entity_picture=https://github.com/Koenkk/zigbee2mqtt/raw/master/images/logo.png, friendly_name=Aquarium Light, supported_features=1 @ 2023-08-13T16:05:03.191043-07:00>)>, <template TemplateState(<state update.bedroom_lamp_2=off; auto_update=False, installed_version=46, in_progress=False, latest_version=46, release_summary=None, release_url=None, skipped_version=None, title=None, brightness=251, color=h=41, hue=41, s=47, saturation=47, x=0.3757, y=0.3737, color_mode=color_temp, color_temp=243, energy=3.26, level_config=on_level=previous, linkquality=18, power=3, update=installed_version=46, latest_version=46, state=idle, update_available=False, device_class=firmware, entity_picture=https://github.com/Koenkk/zigbee2mqtt/raw/master/images/logo.png, friendly_name=Bedroom Lamp 2, supported_features=1 @ 2023-08-13T16:10:41.346622-07:00>)>, <template TemplateState(<state update.bedroom_lamp_1=off; auto_update=False, installed_version=46, in_progress=False, latest_version=46, release_summary=None, release_url=None, skipped_version=None, title=None, brightness=251, color=h=41, hue=41, s=47, saturation=47, x=0.3757, y=0.3737, color_mode=color_temp, color_temp=243, energy=3.22, level_config=on_level=previous, linkquality=14, power=1.3, update=installed_version=46, latest_version=46, state=idle, update_available=False, device_class=firmware, entity_picture=https://github.com/Koenkk/zigbee2mqtt/raw/master/images/logo.png, friendly_name=Bedroom Lamp 1, supported_features=1 @ 2023-08-13T16:10:41.335714-07:00>)>, <template TemplateState(<state update.office_light_3=off; auto_update=False, installed_version=46, in_progress=False, latest_version=46, release_summary=None, release_url=None, skipped_version=None, title=None, brightness=203, color=h=41, hue=41, s=43, saturation=43, x=0.3708, y=0.3705, color_mode=color_temp, color_temp=236, energy=7.16, level_config=on_level=previous, linkquality=69, power=1.3, update=installed_version=46, latest_version=46, state=idle, update_available=None, device_class=firmware, entity_picture=https://github.com/Koenkk/zigbee2mqtt/raw/master/images/logo.png, friendly_name=Office Light 3, supported_features=1 @ 2023-08-13T16:10:41.300291-07:00>)>, <template TemplateState(<state update.office_light_2=off; auto_update=False, installed_version=46, in_progress=False, latest_version=46, release_summary=None, release_url=None, skipped_version=None, title=None, brightness=203, color=h=41, hue=41, s=43, saturation=43, x=0.3708, y=0.3705, color_mode=color_temp, color_temp=236, energy=6.78, level_config=on_level=previous, linkquality=65, power=2.1, update=installed_version=46, latest_version=46, state=idle, update_available=None, device_class=firmware, entity_picture=https://github.com/Koenkk/zigbee2mqtt/raw/master/images/logo.png, friendly_name=Office Light 2, supported_features=1 @ 2023-08-13T16:10:41.293054-07:00>)>, <template TemplateState(<state update.office_light_1=off; auto_update=False, installed_version=46, in_progress=False, latest_version=46, release_summary=None, release_url=None, skipped_version=None, title=None, brightness=203, color=h=41, hue=41, s=43, saturation=43, x=0.3708, y=0.3705, color_mode=color_temp, color_temp=236, energy=6.46, level_config=on_level=previous, linkquality=69, power=2.9, update=installed_version=46, latest_version=46, state=idle, update_available=False, device_class=firmware, entity_picture=https://github.com/Koenkk/zigbee2mqtt/raw/master/images/logo.png, friendly_name=Office Light 1, supported_features=1 @ 2023-08-13T16:10:41.240846-07:00>)>, <template TemplateState(<state number.patio_lighting_countdown=unknown; min=0.0, max=43200.0, step=1.0, mode=auto, brightness=70, countdown=None, light_type=led, linkquality=69, max_brightness=255, min_brightness=1, power_on_behavior=previous, unit_of_measurement=s, friendly_name=Patio Lighting countdown @ 2023-08-13T16:05:04.461719-07:00>)>, <template TemplateState(<state number.body_sensor_detection_interval=60; min=2.0, max=65535.0, step=1.0, mode=auto, battery=100, detection_interval=60, device_temperature=28, linkquality=47, motion_sensitivity=medium, occupancy=False, power_outage_count=18, update=installed_version=26, latest_version=26, state=idle, update_available=None, voltage=3142, unit_of_measurement=s, friendly_name=Body Sensor detection interval @ 2023-08-13T16:10:41.509524-07:00>)>, <template TemplateState(<state select.patio_lighting_light_type=led; options=['led', 'incandescent', 'halogen'], brightness=70, countdown=None, light_type=led, linkquality=69, max_brightness=255, min_brightness=1, power_on_behavior=previous, friendly_name=Patio Lighting light type @ 2023-08-13T16:10:41.577261-07:00>)>, <template TemplateState(<state select.patio_lighting_power_on_behavior=previous; options=['off', 'previous', 'on'], brightness=70, countdown=None, light_type=led, linkquality=69, max_brightness=255, min_brightness=1, power_on_behavior=previous, icon=mdi:power-settings, friendly_name=Patio Lighting power on behavior @ 2023-08-13T16:10:41.577502-07:00>)>, <template TemplateState(<state select.christmas_1_power_on_behavior=unknown; options=['off', 'previous', 'on'], linkquality=51, power_on_behavior=None, icon=mdi:power-settings, friendly_name=Christmas 1 power on behavior @ 2023-08-13T16:05:04.454396-07:00>)>, <template TemplateState(<state select.aqara_fp1_reset_nopresence_status=unknown; options=[''], action=None, approach_distance=far, device_temperature=21, linkquality=76, monitoring_mode=undirected, motion_sensitivity=high, power_outage_count=8, presence=False, presence_event=leave, update=installed_version=54, latest_version=54, state=idle, update_available=None, assumed_state=True, friendly_name=Reset Nopresence Status @ 2023-08-13T16:05:04.413474-07:00>)>, <template TemplateState(<state select.aqara_fp1_motion_sensitivity=high; options=['low', 'medium', 'high'], action=None, approach_distance=far, device_temperature=21, linkquality=76, monitoring_mode=undirected, motion_sensitivity=high, power_outage_count=8, presence=False, presence_event=leave, update=installed_version=54, latest_version=54, state=idle, update_available=None, icon=mdi:tune, friendly_name=Motion Sensitivity @ 2023-08-13T16:07:16.905125-07:00>)>, <template TemplateState(<state select.aqara_fp1_approach_distance=far; options=['far', 'medium', 'near'], action=None, approach_distance=far, device_temperature=21, linkquality=76, monitoring_mode=undirected, motion_sensitivity=high, power_outage_count=8, presence=False, presence_event=leave, update=installed_version=54, latest_version=54, state=idle, update_available=None, friendly_name=Approach Distance @ 2023-08-13T16:07:16.905321-07:00>)>, <template TemplateState(<state select.aqara_fp1_monitoring_mode=undirected; options=['undirected', 'left_right'], action=None, approach_distance=far, device_temperature=21, linkquality=76, monitoring_mode=undirected, motion_sensitivity=high, power_outage_count=8, presence=False, presence_event=leave, update=installed_version=54, latest_version=54, state=idle, update_available=None, friendly_name=Monitoring Mode @ 2023-08-13T16:07:16.905564-07:00>)>, <template TemplateState(<state select.body_sensor_motion_sensitivity=medium; options=['low', 'medium', 'high'], battery=100, detection_interval=60, device_temperature=28, linkquality=47, motion_sensitivity=medium, occupancy=False, power_outage_count=18, update=installed_version=26, latest_version=26, state=idle, update_available=None, voltage=3142, icon=mdi:tune, friendly_name=Body Sensor motion sensitivity @ 2023-08-13T16:10:41.509825-07:00>)>, <template TemplateState(<state select.garage_light_power_on_behavior=previous; options=['off', 'previous', 'on'], linkquality=51, power_on_behavior=previous, icon=mdi:power-settings, friendly_name=Garage Light power on behavior @ 2023-08-13T16:10:41.504334-07:00>)>, <template TemplateState(<state select.aquarium_devices_power_on_behavior=on; options=['off', 'previous', 'on'], linkquality=98, power_on_behavior=on, power_on_behavior_l1=on, state_l1=ON, state_l2=ON, state_l3=ON, state_l4=ON, state_l5=ON, icon=mdi:power-settings, friendly_name=Aquarium Devices power on behavior @ 2023-08-13T16:10:08.641079-07:00>)>, <template TemplateState(<state binary_sensor.aqara_fp1_update_available=off; action=None, approach_distance=far, device_temperature=21, linkquality=76, monitoring_mode=undirected, motion_sensitivity=high, power_outage_count=8, presence=False, presence_event=leave, update=installed_version=54, latest_version=54, state=idle, update_available=None, device_class=update, friendly_name=Update Available @ 2023-08-13T16:07:16.905786-07:00>)>, <template TemplateState(<state binary_sensor.aqara_fp1_presence=off; action=None, approach_distance=far, device_temperature=21, linkquality=76, monitoring_mode=undirected, motion_sensitivity=high, power_outage_count=8, presence=False, presence_event=leave, update=installed_version=54, latest_version=54, state=idle, update_available=None, device_class=occupancy, friendly_name=Presence @ 2023-08-13T19:23:06.007108-07:00>)>, <template TemplateState(<state binary_sensor.outside_occupancy=off; battery=100, detection_interval=60, device_temperature=28, linkquality=47, motion_sensitivity=medium, occupancy=False, power_outage_count=18, update=installed_version=26, latest_version=26, state=idle, update_available=None, voltage=3142, device_class=motion, friendly_name=Outside Occupancy, templates=icon=if (state == "on") return "mdi:motion-sensor"; return "mdi:human-male";, icon_color=if (state == "on") return "yellow"; return "grey"; @ 2023-08-13T17:09:53.701263-07:00>)>, <template TemplateState(<state binary_sensor.powder_room_occupancy=off; battery=100, device_temperature=29, illuminance=4, illuminance_lux=4, linkquality=47, occupancy=False, power_outage_count=142, temperature=25, voltage=3075, device_class=motion, friendly_name=Powder Room, templates=icon=if (state == "on") return "mdi:motion-sensor"; return "mdi:human-male";, icon_color=if (state == "on") return "yellow"; return "grey"; @ 2023-08-13T17:36:34.624303-07:00>)>, <template TemplateState(<state binary_sensor.garage_1_occupancy=off; battery=100, device_temperature=26, illuminance=8, illuminance_lux=8, linkquality=112, occupancy=False, power_outage_count=25, temperature=15, voltage=3035, device_class=motion, friendly_name=Garage 1, templates=icon=if (state == "on") return "mdi:motion-sensor"; return "mdi:human-male";, icon_color=if (state == "on") return "yellow"; return "grey"; @ 2023-08-13T18:11:35.575073-07:00>)>, <template TemplateState(<state binary_sensor.office_motion=off; battery=100, device_temperature=29, illuminance=6, illuminance_lux=6, linkquality=116, occupancy=False, power_outage_count=59, temperature=27, voltage=3035, device_class=motion, friendly_name=Office, templates=icon=if (state == "on") return "mdi:motion-sensor"; return "mdi:human-male";, icon_color=if (state == "on") return "yellow"; return "grey"; @ 2023-08-13T20:11:43.248377-07:00>)>, <template TemplateState(<state binary_sensor.phillips_hue_switch_update_available=off; action=, battery=100, linkquality=65, update=installed_version=33565954, latest_version=33565954, state=idle, update_available=None, device_class=update, friendly_name=Phillips Hue Switch update available @ 2023-08-13T16:10:41.462656-07:00>)>, <template TemplateState(<state binary_sensor.bathroom_occupancy=off; battery=100, device_temperature=24, illuminance=6, illuminance_lux=6, linkquality=18, occupancy=False, power_outage_count=58, temperature=17, voltage=3015, device_class=motion, friendly_name=Bathroom, templates=icon=if (state == "on") return "mdi:motion-sensor"; return "mdi:human-male";, icon_color=if (state == "on") return "yellow"; return "grey"; @ 2023-08-13T16:10:41.408341-07:00>)>, <template TemplateState(<state binary_sensor.inside_motion=off; battery=100, linkquality=69, occupancy=False, power_outage_count=35, voltage=3035, device_class=motion, friendly_name=Inside Motion, templates=icon=if (state == "on") return "mdi:motion-sensor"; return "mdi:human-male";, icon_color=if (state == "on") return "yellow"; return "grey"; @ 2023-08-13T18:11:12.837016-07:00>)>, <template TemplateState(<state binary_sensor.kitchen_battery_low=off; battery=100, battery_low=False, linkquality=51, occupancy=False, tamper=False, voltage=3200, device_class=battery, friendly_name=Kitchen battery low @ 2023-08-13T16:05:14.839894-07:00>)>, <template TemplateState(<state binary_sensor.kitchen_occupancy=off; battery=100, battery_low=False, linkquality=51, occupancy=False, tamper=False, voltage=3200, device_class=motion, friendly_name=Kitchen, templates=icon=if (state == "on") return "mdi:motion-sensor"; return "mdi:human-male";, icon_color=if (state == "on") return "yellow"; return "grey"; @ 2023-08-13T19:23:43.475283-07:00>)>, <template TemplateState(<state binary_sensor.vehicle_real_vibration=unavailable; device_class=vibration, friendly_name=Vehicle Real vibration @ 2023-08-13T16:05:03.149581-07:00>)>, <template TemplateState(<state binary_sensor.vehicle_real_occupancy=unavailable; device_class=motion, friendly_name=Vehicle Real occupancy, templates=icon=if (state == "on") return "mdi:motion-sensor"; return "mdi:human-male";, icon_color=if (state == "on") return "yellow"; return "grey"; @ 2023-08-13T16:05:03.148507-07:00>)>, <template TemplateState(<state binary_sensor.vehicle_real_power_state=unavailable; friendly_name=Vehicle Real power state @ 2023-08-13T16:05:03.147418-07:00>)>, <template TemplateState(<state binary_sensor.vehicle_real_presence=unavailable; device_class=presence, friendly_name=Vehicle Real presence @ 2023-08-13T16:05:03.146476-07:00>)>, <template TemplateState(<state binary_sensor.bedroom_lamp_2_update_available=off; brightness=251, color=h=41, hue=41, s=47, saturation=47, x=0.3757, y=0.3737, color_mode=color_temp, color_temp=243, energy=3.26, level_config=on_level=previous, linkquality=18, power=3, update=installed_version=46, latest_version=46, state=idle, update_available=False, device_class=update, friendly_name=Bedroom Lamp 2 update available @ 2023-08-13T16:10:41.347151-07:00>)>, <template TemplateState(<state binary_sensor.landing_2_motion=off; battery=100, device_temperature=33, illuminance=96, illuminance_lux=96, linkquality=112, occupancy=False, power_outage_count=73, temperature=18, voltage=3085, device_class=motion, friendly_name=Landing 2, templates=icon=if (state == "on") return "mdi:motion-sensor"; return "mdi:human-male";, icon_color=if (state == "on") return "yellow"; return "grey"; @ 2023-08-13T16:10:41.342446-07:00>)>, <template TemplateState(<state binary_sensor.bedroom_lamp_1_update_available=off; brightness=251, color=h=41, hue=41, s=47, saturation=47, x=0.3757, y=0.3737, color_mode=color_temp, color_temp=243, energy=3.22, level_config=on_level=previous, linkquality=14, power=1.3, update=installed_version=46, latest_version=46, state=idle, update_available=False, device_class=update, friendly_name=Bedroom Lamp 1 update available @ 2023-08-13T16:10:41.336244-07:00>)>, <template TemplateState(<state binary_sensor.living_room_occupancy=off; battery=100, device_temperature=28, illuminance=18, illuminance_lux=18, linkquality=116, occupancy=False, power_outage_count=63, temperature=18, voltage=3005, device_class=motion, friendly_name=Living Room, templates=icon=if (state == "on") return "mdi:motion-sensor"; return "mdi:human-male";, icon_color=if (state == "on") return "yellow"; return "grey"; @ 2023-08-13T18:14:18.641710-07:00>)>, <template TemplateState(<state binary_sensor.office_light_3_update_available=off; brightness=203, color=h=41, hue=41, s=43, saturation=43, x=0.3708, y=0.3705, color_mode=color_temp, color_temp=236, energy=7.16, level_config=on_level=previous, linkquality=69, power=1.3, update=installed_version=46, latest_version=46, state=idle, update_available=None, device_class=update, friendly_name=Office Light 3 update available @ 2023-08-13T16:10:41.300947-07:00>)>, <template TemplateState(<state binary_sensor.office_light_2_update_available=off; brightness=203, color=h=41, hue=41, s=43, saturation=43, x=0.3708, y=0.3705, color_mode=color_temp, color_temp=236, energy=6.78, level_config=on_level=previous, linkquality=65, power=2.1, update=installed_version=46, latest_version=46, state=idle, update_available=None, device_class=update, friendly_name=Office Light 2 update available @ 2023-08-13T16:10:41.293836-07:00>)>, <template TemplateState(<state binary_sensor.office_light_1_update_available=off; brightness=203, color=h=41, hue=41, s=43, saturation=43, x=0.3708, y=0.3705, color_mode=color_temp, color_temp=236, energy=6.46, level_config=on_level=previous, linkquality=69, power=2.9, update=installed_version=46, latest_version=46, state=idle, update_available=False, device_class=update, friendly_name=Office Light 1 update available @ 2023-08-13T16:10:41.241592-07:00>)>, <template TemplateState(<state light.powder_room_mirror=off; effect_list=['blink', 'breathe', 'okay', 'channel_change', 'finish_effect', 'stop_effect'], supported_color_modes=[<ColorMode.BRIGHTNESS: 'brightness'>], do_not_disturb=None, linkquality=120, icon=mdi:mirror, friendly_name=Powder Room Mirror, supported_features=44 @ 2023-08-13T16:10:41.583905-07:00>)>, <template TemplateState(<state light.patio_stair_lighting=off; effect_list=['blink', 'breathe', 'okay', 'channel_change', 'finish_effect', 'stop_effect'], supported_color_modes=[<ColorMode.BRIGHTNESS: 'brightness'>], do_not_disturb=None, linkquality=72, friendly_name=Patio Stair Lighting, supported_features=44 @ 2023-08-13T16:10:41.578543-07:00>)>, <template TemplateState(<state light.patio_lighting=off; supported_color_modes=[<ColorMode.BRIGHTNESS: 'brightness'>], countdown=None, light_type=led, linkquality=69, max_brightness=255, min_brightness=1, power_on_behavior=previous, friendly_name=Main Light, supported_features=40 @ 2023-08-13T16:10:41.577765-07:00>)>, <template TemplateState(<state light.bnl_l=off; min_color_temp_kelvin=2000, max_color_temp_kelvin=6535, min_mireds=153, max_mireds=500, effect_list=['blink', 'breathe', 'okay', 'channel_change', 'finish_effect', 'stop_effect'], supported_color_modes=['color_temp'], do_not_disturb=None, linkquality=18, icon=kuf:light_wall_2, friendly_name=Bed Night Light L, supported_features=44 @ 2023-08-13T19:25:06.484874-07:00>)>, <template TemplateState(<state light.himalayan_salt_lamp=off; effect_list=['blink', 'breathe', 'okay', 'channel_change', 'finish_effect', 'stop_effect'], supported_color_modes=[<ColorMode.BRIGHTNESS: 'brightness'>], linkquality=72, icon=mdi:alarm-light, friendly_name=Himalayan Salt Lamp, supported_features=44 @ 2023-08-13T16:10:41.521106-07:00>)>, <template TemplateState(<state light.bnl_r=
<remainder truncated>
]

The integrations is “mosquito broker” instead of MQTT

Thanks but I get nothing with that…

image

{{
  expand(integration_entities('mosquito broker') )
  | list
}}

Also, HA documentation says to look at the integration name under entities…

After a lot more googling, I found this link which helped a lot, and I nailed it!

Count: {{
  expand(integration_entities('mqtt') )
  | selectattr('state', 'in', 'unavailable')
  | list
  | length
}}
Entities:
{{
  expand(integration_entities('mqtt') )
  | selectattr('state', 'in', 'unavailable')
  | map(attribute='name') 
  | list
}}

The map(attribute='name') could be [entity_id | object_id] (both the same) depending on what you’d like to see.

It’s not perfect as I would like only the devices that are unavailable rather than all of their entities but…

Now if I could only find that post in a dead zwave nodes topic where they guy wanted something similar for zigbee2mqtt…

Ha…daggum auto-correct.

I swear ‘mqtt’ did not work for me last night, but ‘Mosquitto broker’ did.

Tried again this morning and either will work.

Count: {{
  expand(integration_entities('mqtt') )
  | selectattr('state', 'in', 'unavailable')
  | list
  | length
}}

Entities:{% set devices = expand(integration_entities('mqtt') )
  | selectattr('state', 'in', 'unavailable')
  | map(attribute='entity_id')%}
{% for device in devices %}
{{ device_attr(device, 'name') }}
{%- endfor %}

Haven’t figured out how to dedup the list yet.

That I think is gonna be tough!!

So you typed 'mosquito broker` and your system said “Oh you mean MQTT? Here you go…”?? LOL