Can this config be simplified?

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 %}`

since ls is < or >= this should work. Also other questions below.

- 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) %}  <----  the sqrt of x^2 = x

        {% if (y) < 300 %}  <--- This seems superfluous (n-ls) seems the only thing that matters
          {% if (n - ls) > 86400 %}
            Offline
          {% elif 43200 < (n - ls) < 86400 %}
            Demnächst prüfen
          {% elif (n - ls) < 43200 %}
            Online
          {% endif %}      

        {% else %}
          {% if (n - ls) > 86400 %}
            Offline
          {% elif 43200 < (n - ls) < 86400 %}
            Demnächst prüfen
          {% elif (n - ls) < 43200 %}
            Online
          {% endif %}

        {% endif %}`

Not advanced as your code, but this works for notifying me of ZigBee sensors that go offline.

card:
  title: Things that are broken
  type: entities
filter:
  include:
    - domain: sensor
      state: unavailable
    - domain: sensor
      state: unknown
    - domain: sensor
      state: offline
    - domain: binary_sensor
      state: unavailable
    - domain: binary_sensor
      state: unknown
    - domain: binary_sensor
      state: offline
  exclude:
    - entity_id: '*echo*'
  unique: true
type: custom:auto-entities

That was a little help, thanks! Took it from some previous code and overlooked it

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!