How to raise an alert when known zigbee device is no longer in the network

I have a situation where 2 of my Xiaomi devices had dropped from the network and it took a day or so before I noticed the temperature was not changing anymore.

I see with the Zigbeenetwork map component that there is a last seen datetimestamp for each device in the network topology graph. Is there a way to check these times for all zigbee devices and if now - last_seen is over a certain amount of hours, raise a notification.

Has anyone done such a thing before?

Sure, Iā€™ve got this sensor for doing exactly that. Itā€™s written for Z-Wave, but merging it with the logic from the follow template (for displaying the last seen time of my Zigbee2MQTT devices), should work:

    {% for state in states.sensor -%}
      {%- if state.attributes.linkquality %}
        {%- if "linkquality" in state.name %}
          {{ relative_time(state.last_updated) }} ago for {{ state.name | lower }}<BR>
        {%- endif -%}
      {%- endif -%}
    {%- endfor %}

Iā€™ve not, yet, extended the Z-Wave binary sensor to Zigbee, or made the notifications smarter, but this automation has the groundwork for a smarter notification.

3 Likes

Do these sensor not change the state to ā€œunavailabeā€? My ZigBee sensors act like this.

No, for example the temp/hum sensors just keep their last transmitted value in HA. Perhaps if Z2M or HA is restarted that these values become ā€˜Unavailableā€™ but nowadays I donā€™t have to reboot either a lot anymore.

Ah I see, think this is probably due to your sensors being integrated to MQTT but Iā€™m not sure on this.

Will have a look at your solution, meanwhile I wonder if using the relatively new Z2M config option

 last_seen: 'disable'
  # Optional: Add an elapsed attribute to MQTT messages, contains milliseconds since the previous msg (default: false)

could be used for this.

E.g. an automation triggering a script every 4 hours that checks all the last_seen timestamps and reports if a sensor hasnā€™t been seen for 2-3 or so hours.

Hmm, could you give me a hand for the following:

I would like to get a notification when a device has not been seen for 1+ days. So somehow I would need to have an automation that checks letā€™s say every few hours to see if the output of such a template contains ā€˜1 day agoā€™ for example and then fires a notification.

I was thinking of using timedelta first but HA doesnā€™t know about it it seems (when I tried in the Template tab).

You could just look to see if the state last updated 24 hours ago, in seconds:

    {% for state in states.sensor -%}
      {%- if (as_timestamp(now()) - as_timestamp(state.last_updated) > (24 * 60 * 60) ) %}
         {{ state.name | lower }} was last seen {{ relative_time(state.last_updated) }} ago
      {% endif -%}
    {%- endfor %}

You could probably use it in the kind of template condition I have here to allow you to only fire the notification if thereā€™s a problem.

1 Like

Hey why did you pick last_updated and not last_seen? For instance, I unplugged a zigbee router a week ago. Last_seen is a week ago. Last_updated is 12 hours ago.

Because it works for me :man_shrugging:

Ahah fair enough. I wanted to write it with last_seen but I am not able to use the relative time:
Last updated looks like 2020-11-05 05:25:22.336481+00:00
whereas last_seen looks like 2020-10-24 09:39:10

I tried using as_timestamp then timestamp_local but it still does not workā€¦
Any clue please?

Try in Developer tools -> Templates - as_timestamp() works for me with that format.

Yes it works for me too but my goal is to get the relative_time at the end.
I got blocked when trying to use it.

Then you need strptime as explained here.