Offline detection for Z2M devices with last_seen

Does not work for me. When I try to create the automation it says “Message malformed. Missing input action”

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 → Preview → Import → Create Automation → Fill all required fields → Save

You must have lqi sensors and last_seen option from Zigbee2MQTT enabled (see first post).

2 Likes

Hi,
Importation works, it’s the automation based on the blueprint that does not. When I save it it gives the error I mentioned.

Successfully used other blueprints.
Lqi are present on all sensors, last_seen is enabled
Thanks for your help

Can you give more details? Maybe something is logged to HA log, please give also screenshot or yaml what automation you are trying to create…

Ok I’ve tried again and apparently, when creating the automation using the blueprint, we need to define an action, otherwise it gives the error I mentioned. I’ve added a simple notification and now it saves without error.
Then I opened the blueprint’s code to see that the notification needed to refer to {{sensors}} to show the unresponsive devices. So that’s what I put in the notification, and it works like a charm.
I guess the blueprint cannot include a predefined notification since it depends on the user.
Maybe just add somewhere the steps needed for those who’ll encounter the same problem :wink:
Thanks a lot, it’s a useful automation !
Have a great day

2 Likes

Hello!

I enabled “last_seen” option in Z2M configuration. I checked the log, I saw the last_seen time.
I imported your automation from blueprint.
I made the rules, i did set 1 hour, all day (0), didnt make excloude sensors.
I did set notification in action section:

service: notify.push_uzenetkuldes
data:
title: A Zigbee eszköz nem elérhető
message: ‘{{sensors}}’

(I did set Pushover service before, it, I tired it, it work)

I saved it and enabled. But it do not work. I dissassembly one Xiaomi thermo sensor, it is not online 26 hours ago, but your script do it nothing.

Step result:
Result:

params:
domain: notify
service: push_uzenetkuldes
service_data:
title: A Zigbee eszköz nem elérhető
message: ‘’
target: {}
running_script: false
limit: 10

Step config:
service: notify.push_uzenetkuldes
data:
title: A Zigbee eszköz nem elérhető
message: ‘{{sensors}}’

I tired Low battery level detection & notification for all battery sensors and work it perfictly…

What is wrong? Please help me :slight_smile:

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

1 Like

Thank you! Its works! I needed to enable linkquality on my all devices.

This automation can run once per day? It cant check my sensors every 6 or 12 hours?

Interesting. I can’t seem to see any Entities in Home Assistant showing “linkquality”.

Please can you let me know how you enabled linkquality for all of your devices? I’ve looked through the docs and can’t figure it out.

Thanks

Some recent z2m upgrade introduced hiding linkquality entities by default. You must enable each linkquality entity manually in devices

I agree. Is it possible to specify multiple times for automation to run or set an intervall like every three hours?

Is anyone else seeing this error? (I’m on 2021.12.9)

2022-01-29 18:56:48 WARNING (MainThread) [homeassistant.helpers.template] Template warning: 'strptime' got invalid input '2022-01-29T17:30:49.295Z' when rendering 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(', ') }}' but no default was specified. Currently 'strptime' will return '2022-01-29T17:30:49.295Z', however this template will fail to render in Home Assistant core 2022.1

Notification does not show the sensor name


If you have the blueprint and other YAML code in the main message, the myBlueprints tool does not work.
I would suggest putting the other 2 yaml code segments onto a reply, or using a screenshot of those, or adding them as just text. Otherwise people will have problems importing the blueprint. (That or remove the myBlueprints thing completely.)
Before I moved my blueprint code to GitHub, I would load the main message with the blueprint code, then comment on it immediately if there were ‘helper’ yaml code segments that were needed.

The warning part, read this…
{{ result.sensors | join(', ') }}' but no default was specified. Currently 'strptime' will return '2022-01-29T17:30:49.295Z', however this template will fail to render in Home Assistant core 2022.1

@Mr_Groch

It’s a deprecated thing that will, I assume, become a breaking change…

Yeah, this blueprint no longer works for me on 2022.3.x

I’ve been trying to figure out where the issue is without much knowledge of templating, so hopefully the info below is correct.
At the moment I think the fault seems to be with this part of the template:

{{ strptime(state_attr('binary_sensor.door_front_contact', 'last_seen'), '%Y-%m-%dT%H:%M:%S%z', 0) }}

Running this in the template editor returns a 0 due to it failing. Remove the “,0” (as it is in the blueprint template) returns the timestamp string, e.g. 2022-04-14T17:56:31.029Z. This is why relative_time doesn’t work because strptime can’t convert the string to a datetime object.

# returns time @ 10 am, if fails returns 0
{{ strptime("10:00", "%H:%M", 0) }}

strptime(string, format) 
parses a string based on a format and returns a datetime object. 
If that fails, returns the default value, or if omitted the unprocessed input value.

What I can’t figure out is why strptime isn’t able to parse the date string. I suspect its something to do with the seconds and UTC offset that it can’t convert:

:%S%z
:31.029Z

According to this page, it says the following:

In fact, if I test the following template by removing “.029Z” from the timestamp, it successfully converts to a datetime object:

{{ strptime("2022-04-14T19:08:50", '%Y-%m-%dT%H:%M:%S', 0) }}
Result:
2022-04-14 19:08:50

My zigbee2mqtt configuration.yaml has last_seen set to ISO_8601 which might be related. An alternative is ISO_8601_local which I might try as well.

For reference, the full template (line wraps removed from the blueprint) is below:

{% 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(', ') }}

doh, I really should RTFM. Changing to ISO_8601_local did the trick (as it tells me to do in the first post in this thread!!!). The last_seen timestamp is now 2022-04-14T20:37:02+01:00 which of course works… :expressionless:

1 Like

Hi all!

Lot happens in Zigbee2MQTT and Home Assistant, so I’ve updated this blueprint! Device attributes are now legacy in Z2M and ‘last seen’ sesnors are now exposed as sensors with ‘timestamp’ device_class in Home Assstant by Z2M - so I’ve decided to drop checking for ‘lqi’ sensors - I’m checking all ‘last seen’ sensors with ‘timestamp’ device_class instead…

So all you need is to enable ‘last seen’ sensors in device page if it is disabled, and also modify excluded sensors if you are using it…

That is breaking change…

PS. I’m also checking now for ‘unavailable’ state :wink:

1 Like

Hmm, I’ve been trying to figure out how an easier way to do this:

The problem is I have 125 Z2M devices, so enabling the last_seen entity for each device within HA will take far too long! I’ve not been able to find an easy way to enable this from within Z2M either, with comments near the end of this issue describing the same problem: last seen not showing up · Issue #9398 · Koenkk/zigbee2mqtt · GitHub