Open TawfikDaim opened 1 month ago
This Last Seen value is actually brought over from Zigbee2MQTT into Home Assistant, but it’s part of a diagnostic entity that is disabled by default. To make use of it you need to enable it.
to do this you need to add the following parts to your Zigbee2MQTT configuration.yaml file.
device_options: homeassistant: last_seen: enabled_by_default: true
https://www.homeautomationguy.io/blog/managing-offline-devices-in-zigbee2mqtt
To create a template sensor go to Home Assistants settings page, and Helpers. Create a Helper, which is a Template sensor. Give it a name and paste this code into the state template area:
{% set result = namespace(sensors=[]) %} {% for state in states.sensor | rejectattr('attributes.device_class', 'undefined') | selectattr('attributes.device_class', '==', 'timestamp') %} {% if 'last_seen' in state.entity_id and (states(state.entity_id) == 'unavailable' or ((as_timestamp(now()) - as_timestamp(states(state.entity_id))) > ((24 | int) 60 60))) %} {% set result.sensors = result.sensors + [state.name | regex_replace(find=' last seen', replace='') ~ ' (' ~ relative_time(strptime(states(state.entity_id), '%Y-%m-%dT%H:%M:%S%z', 'unavailable')) ~ ')'] %} {% endif %} {% endfor %} {{ result.sensors | join(', ') }}
You will see a preview of the sensor at the bottom of the dialog box listing out any Offline devices, or ones that have not been seen for over 24 hours.
Paste this into dev tools, template:
{% set result = namespace(sensors=[]) %} {% for state in states.sensor | selectattr('attributes.unit_of_measurement', '==', 'lqi') %} {% if state_attr(state.entity_id, 'last_seen') != None and (as_timestamp(now()) - as_timestamp(state_attr(state.entity_id, 'last_seen'))) > ((1 | int) 60 60) %} {% set result.sensors = result.sensors + [state.name | regex_replace(find=' linkquality', replace='') ~ ' (' ~ relative_time(strptime(state_attr(state.entity_id, 'last_seen'), '%Y-%m-%dT%H:%M:%S%z')) ~ ')'] %} {% endif %} {% endfor %} {{ result.sensors | join(', ') }}
If you see no result, you maybe have lqi sensors disabled (z2m update some time ago disabled those sensors by default…). If so you need to go to configuration, entities, check show disabled entities and search for linkquality - you should then enable all linkquality sensors for mqtt integration
https://community.home-assistant.io/t/offline-detection-for-z2m-devices-with-last-seen/310138/5
create an automation based on this blueprint
Do you have succesfully imported and used any other blueprint? I’ve just deleted this blueprint on my local HA setup and imported it again succesfully.
Configuration → Blueprints → Import → Paste URL https://gist.github.com/Mr-Groch/bf073b142b507e3b6f8154223f81803b 27 → Preview → Import → Create Automation → Fill all required fields → Save
https://gist.github.com/Mr-Groch/bf073b142b507e3b6f8154223f81803b
You must have lqi sensors and last_seen option from Zigbee2MQTT enabled (see first post).
Image
actions:
Image
here couple of enhancements needed for z2mqtt.
17 Oct: Instead of the value state I have now a dashboard for maintenance, in a later phase an alert to be sent if a device is offline for extened time or if lqi value is below certain threshold (say 90) by whatsapp or notification