I want to share my setup. I’m using Z2M and first I tried availability_timeout
config. I didn’t like it because it generates additional network traffic and I have mostly battery powered devices which are not pinged. Also when Z2M mark device as unavailable then you are unable to control it any more via HA.
For me that was no go and I went for binary sensor which checks last seen field. For that I enabled last_seen
in Z2M config:
advanced:
last_seen: ISO_8601_local
and then I created binary_sensor template like this:
binary_sensor:
- platform: template
sensors:
z2m_health:
friendly_name: 'Z2M Mesh health'
value_template: >-
{%- macro CheckDroppedZigbee() -%}
{% for state in states.sensor -%}
{%- if ("linkquality" in state.name and state_attr(state.entity_id, "last_seen") != None and (as_timestamp(now()) - as_timestamp(state_attr(state.entity_id, "last_seen")) > (24 * 60 * 60))) -%}
{{ state.name }}
{%- endif -%}
{%- endfor %}
{%- endmacro -%}
{% set output = CheckDroppedZigbee() %}
{% if output | trim == "" %}
false
{%- else -%}
true
{%- endif -%}
attribute_templates:
data: >-
{%- macro GetDroppedZigbee() -%}
{% for state in states.sensor -%}
{%- if ("linkquality" in state.name and state_attr(state.entity_id, "last_seen") != None and (as_timestamp(now()) - as_timestamp(state_attr(state.entity_id, "last_seen")) > (24 * 60 * 60))) -%}
{{ state.name | regex_replace(find=' linkquality', replace='', ignorecase=False) }} - {{ relative_time(strptime(state_attr(state.entity_id, "last_seen"), '%Y-%m-%dT%H:%M:%S%z')) }} ago {{- '\n' -}}
{%- endif -%}
{%- endfor %}
{%- endmacro -%}
{{ GetDroppedZigbee() }}
I wanted to store message into attributes so that I can use it in Lovelace also. I’m using only linkquality
entities.
After that you have everything and you can use this binary sensor in automations like this:
alias: Zigbee unhealthy alert
description: ''
trigger:
- platform: state
entity_id: binary_sensor.z2m_health
to: 'on'
condition: []
action:
- data:
message: |-
❌ Zigbee last seen ❌️:
{{state_attr('binary_sensor.z2m_health', 'data')}}
service: notify.telegrambotme
mode: single
Thanks to @Skye and @Tinkerer for code which gave me idea and direction.