Trying to convert the last_seen value of Zigbee device to days

I have an Aqara T1 valve controller that sometimes becomes “Unavailable”. So I created an automation that triggers on that state. I also found that the “Identify” entity of these devices contains the last_seen value:

I’m trying to take that long date string and extract only the days, so I can use it in an automation to determine the number of days has elapsed. Below is one of my unsuccessful attempts. Can someone please show me how to convert this date stamp to days?

-Thanks

Last Seen status is: {{ states('button.aqara_t1_valve_controller_identify') "{{ ((as_timestamp(now()) - as_timestamp(last)) / 86400) | int }}"

I am not sure if all devices have a last_seen value so I would use last_updated instead:

{{ ((now() - states.light.sengled_1.last_updated).total_seconds() / 86400 ) | round(2) }}

The Aqara T1 doesn’t have a last_updated entity. Not many entities at all. So I’m trying to use your statement with the last_seen which is displayed by the identify entity shown below.

The conversion is not working but it looks close!

-Thanks

Last Seen status is: "{{ ((now() - states('button.aqara_t1_valve_controller_identify') / 86400 ) | round(2) }}"

it’s not an entity, its an attribute on each entity but hidden.

I don’t have any buttons with states.
But most entities at least have the last_changed state.

{{ (now() - as_datetime(states.binary_sensor.balkongdorr.last_changed)).days }}

I have done some more digging, last_seen is an attribute of the integration.

Zigbee2MQTT supports it, but its not enabled by default.
In order to enabled it it is necessary to edit the Zigbee2MQTT configuration.yaml

Adding the following:

advanced:
  last_seen: 'ISO_8601'

Once thats done / Z2M has to be restarted, then there is a disabled attribute of each Z2M device: Last Seen
Which can be enabled (on a per device basis).

After enabling then it is possible to access it and calculate the number of days:

{{ ((as_timestamp(now()) - as_timestamp(states("sensor.living_entry_scene_last_seen"))) / 86400 ) | round(2)}}

As noted previously I don’t know which integrations support last_seen / how to enable them - I have tested the above (with Zigbee2MQTT) if your using something else you will need to play.