Z2m last seen glance lovelace UI

Hi all!

I am sure this is some basic stuff for many of you, but also for many others it can be a help.
I constantly had problems with not so family friendly zigbee bulbs. They were turned off constantly the dumb way but other battery powered devices (like covers, motion sensors) lost their connectivity along that which frustrated the family even more so they would not rely on the devices and keep on doing it the dumb way. A ‘dumb’-spiral. So i spent a little time and figured out a way to have my z2m devices and their time of last ‘sign of life’ at one glance, which is perfect for me. Now I can easily counteract quicker.
First you need to add

  last_seen: 'ISO_8601_local'

under ‘advanced’ in the z2m configuration.yaml file. (Note: I do not use ZHA, but if it has the ‘last_seen’ option, it will work with that too.)
The lovelace UI card config:

type: markdown
title: Rollo last seen
content: >-
  {% if as_timestamp(state_attr('sensor.DEVICE_linkquality', 'last_seen')) |
  int < as_timestamp(states.sensor.DEVICE_linkquality.last_changed) | int %}
  DEVICE: {{ ((as_timestamp(now()) -
  as_timestamp(states.sensor.DEVICE_linkquality.last_changed))|
  timestamp_custom('%j')) | int - 1 }}{{ ( as_timestamp(now()) -
  as_timestamp(states.sensor.DEVICE_linkquality.last_changed)) |
  timestamp_custom('d %Hh %Mm', utc) }}
  {% elif as_timestamp(state_attr('sensor.DEVICE_linkquality', 'last_seen')) |
  int > as_timestamp(states.sensor.DEVICE_linkquality.last_changed) | int %}
  DEVICE: {{ ((as_timestamp(now()) -
  as_timestamp(state_attr('sensor.DEVICE_linkquality', 'last_seen')))|
  timestamp_custom('%j')) | int - 1 }}{{ ( as_timestamp(now()) -
  as_timestamp(state_attr('sensor.DEVICE_linkquality', 'last_seen'))) |
  timestamp_custom('d %Hh %Mm', utc) }}
  {% endif %}

What it does basically:
Takes a look at when the ‘linkquality’ last changed (which is most frequent for me) and looks at the ‘last_seen’ attribute. It compares both and displays the latest one. It looks a bit complicated because I specifically wanted the format to be days-hours-minutes.

Copy-paste, change device name before the colon, change the entity and that’s it. Every device in z2m has ‘linkquality’ and ‘last_seen’ and ‘last_changed’ comes from HA.

This is the result:

Rollo Schlafzimmer: 0d 00h 38m

greetings!

thank you for sharing. I copy pasted your markdown in lovelace and gives this error:

while parsing a block mapping in "/config/lovelace/01-dash.yaml", line 3503, column 7 expected <block end>, but found '<block mapping start>' in "/config/lovelace/01-dash.yaml", line 3516, column 8

The second DEVICE entry seems incorrect.

@juan11perez try again with the edited code. There was wrong spacing

The code is for only one device! If you want multiple devices, copy paste the code underneath content:

And let me know, if it works for you @juan11perez

thank you very much for your help. just want to confirm Im it doing correctly.
So I have a contact sensor called office_door
I added your template as such:

    - type: markdown
      title: Last Seen Zigbee
      content: >-
        {% if as_timestamp(state_attr('sensor.office_door_linkquality', 'last_seen')) |
        int < as_timestamp(states.sensor.office_door_linkquality.last_changed) | int %}
        office_door: {{ ((as_timestamp(now()) -
        as_timestamp(states.sensor.office_door_linkquality.last_changed))|
        timestamp_custom('%j')) | int - 1 }}{{ ( as_timestamp(now()) -
        as_timestamp(states.sensor.office_door_linkquality.last_changed)) |
        timestamp_custom('d %Hh %Mm', utc) }}
        {% elif as_timestamp(state_attr('sensor.office_door_linkquality', 'last_seen')) |
        int > as_timestamp(states.sensor.office_door_linkquality.last_changed) | int %}
        office_door: {{ ((as_timestamp(now()) - 
        as_timestamp(state_attr('sensor.office_door_linkquality', 'last_seen')))|
        timestamp_custom('%j')) | int - 1 }}{{ ( as_timestamp(now()) -
        as_timestamp(state_attr('sensor.office_door_linkquality', 'last_seen'))) |
        timestamp_custom('d %Hh %Mm', utc) }}
        {% endif %}

it shows no errors.

Does it show up in the lovelace UI?

yes it does, i got this

Office Door: 0d 00h 15m

thank you for your help