ZHA Zigbee Monitor

This script checks if all zigbee devices are online (available).

  zigbee_device_check:
    sequence:
    - variables:
        ignore_ieees:
        - "00:0d:ff:00:12:34:56:78"
        - "00:1a:7d:00:87:65:43:21"
        offline_devices: >
          {% set result = namespace(list=[]) %}
          {% for device in integration_entities('zha') | map('device_id') | unique %}
            {% set first_entity = device_entities(device) | first %}
            {% if states(first_entity) == 'unavailable' %}
              {% set ieee = (device_attr(first_entity, 'identifiers') | list)[0][1] %}
              {% if ieee not in ignore_ieees %}
                {% set user = device_attr(first_entity, 'name_by_user') or '—' %}
                {% set name = device_attr(first_entity, 'name') or '—' %}
                {% set entry = "🔴 " ~ user ~ " (" ~ name ~ ") – " ~ ieee %}
                {% set result.list = result.list + [entry] %}
              {% endif %}
            {% endif %}
          {% endfor %}
          {{ result.list }}
    - condition: template
      value_template: "{{ offline_devices | count > 0 }}"
    - service: persistent_notification.create
      data:
        title: "Zigbee: offline devices"
        message: >
          {{ offline_devices | join('\n') }}
1 Like

Interesting idea. One piece of feedback - right now this will send a notification even if there are no devices offline because the result list includes the line “These devices are offline:” Because of that, the count of offline_devices will always be greater than zero. You could change the title of the notification to “Zigbee Devices Offline” and then change the setup of result to:

{% set result = namespace(list=[]) %}

or change the test to:

    - condition: template
      value_template: "{{ offline_devices | count > 1 }}"
1 Like

Thank you very much, I changed the code.