[SOLVED] Help with vacuum relative time

Hi, as Xiaomi vacuum changed to have information as separate sensors now instead of attributes, I’m unable to get human readable (relative time) readout of the Last Clean End sensor. I’m not a programmer so I struggle with templating, but I thought this should work:

Vacuum last cleaned {{ relative_time(states.sensor.robot_vacuum_last_clean_end.state) }}

But it returns 2021-11-06T16:30:13, not “yesterday” or “18 hours ago” (both would be fine). Problem is, the timestamp is the state of the sensor itself, so it does not have the .last_changed attribute, and I just don’t know how to pass that to relative_time.

{{ relative_time(states('sensor.robot_vacuum_last_clean_end')) }} returns the same thing (2021-11-06T16:30:13). AFAIK that’s NOT relative time.

Basically how the Devices page shows it, that’s what I want:
Screen Shot 2021-11-07 at 09.28.05

Please advise, thanks.

SOLUTION: I have to first make it a datetime object and only then convert it into relative_time:

{{ relative_time(as_datetime(states('sensor.robot_vacuum_last_clean_end'))) }}

6 Likes