Hi there!
I have a rather complicated way of displaying when a Zigbee device is offline. I hope you can recognize what I wrote there.
I first had this code in a markdown card, but now wanted to switch to templates. The problem I have: I have about 120+ devices and having such a huge list in the config is very complicated to take care off, plus I don’t like the idea of manually and repetitively typing everything down.
So: is there some way of using variables or some form of iterating to simplify this for 120+ entities?
`- sensor:
- name: "xy sensor"
state: >
{% set entity = 'xy_linkquality' %}
{% set z = (state_attr("input_datetime.zigbee_restart", "timestamp")) | int %}
{% set lc = as_timestamp(states.sensor[entity].last_changed) | int %}
{% set ls = as_timestamp(state_attr('sensor.' ~ entity, 'last_seen')) | int %}
{% set n = as_timestamp(now()) | int %}
{% set x = (lc - z) %}
{% set y = (x**2)**(1/2) %}
{% if (y) < 300 %}
{% if (n - ls) > 86400 %}
Offline
{% elif 43200 < (n - ls) < 86400 %}
Demnächst prüfen
{% elif (n - ls) < 43200 %}
Online
{% endif %}
{% elif ls < lc %}
{% if (n - ls) > 86400 %}
Offline
{% elif 43200 < (n - ls) < 86400 %}
Demnächst prüfen
{% elif (n - ls) < 43200 %}
Online
{% endif %}
{% elif ls >= lc %}
{% if (n - ls) > 86400 %}
Offline
{% elif 43200 < (n - ls) < 86400 %}
Demnächst prüfen
{% elif (n - ls) < 43200 %}
Online
{% endif %}
{% endif %}`
I’m not sure in which cases your Zigbee devices would have the state “unknown” or “offline”. Could you please explain?
And yes… the available feature in z2m… I just hate it. It would be extremely useful, but there is a major down: yet I couldn’t figure out how to run the availability check manually. This means that a device is uncontrollable from Lovelace until the next check is run, which could be hours (depending on your settings), even if it is working and was just not responding in time.
If you know how to run a check manually, please let me know. Otherwise this won’t work for me.
greetings!