Elapsed time conversion in template

Hi,
I have sensor.truenas_uptime which returns time of last boot as 2022-04-11T19:59:15+01:00

I would like to convert this into a string that counts how much time elapsed since, i.e. something like 5 days, 16:58:34.

So far I managed to convert it to seconds (I think) with

{{ (as_timestamp(now()) | int) - ((as_timestamp(strptime(states('sensor.truenas_uptime'), '%Y-%m-%dT%H:%M:%S+00:00'))) | int) }}

but can’t figure out how to go from there.

Any advice welcome, thanks!

Copy-paste this into the template editor:

{{ now() - states('sensor.truenas_uptime') | as_datetime }}

It will report the time difference in a format you might find acceptable. If it’s not how you want to see it, let me know the exact way you want the time difference to be presented.

Thanks, that’s 99% of what I want, it gives 4 days, 21:42:58.134227 and all I’d like now is to get rid of the decimal point and everything after.

This converts the result into a string and then slices off the last 7 characters.


{{ ((now() - states('sensor.truenas_uptime') | as_datetime) | string)[:-7] }}

2 Likes