WTH - Why isn't there a template extension for simple relative age?

I often find myself using a template similar to this:

{{ as_timestamp(now()) - as_timestamp(states.sensor.temperature.last_changed) }}

to get the seconds since something changed. It would be much cleaner to have a single function replacement (similar to the relative_time function, but computer friendly instead of human friendly)

example:
{{ relative_age(states.sensor.temperature.last_changed) }}

If you do this:

{{ now() - states.sensor.temperature.last_changed }}

You will get a time delta object that can be used in templates.

Sure, but then you still have to write the function for returning a relative time string. Would be nice if there was a template which would return what we see in HA now: 5 seconds ago, 3 days ago, …

?

It is a time delta object relative from now.

You specifically said you don’t want that:

Sorry, missed the part that he did NOT want a human readable format

Ha! I missed that it wasn’t the original poster replying. Sorry.

1 Like

Ah, I guess that does give me a fairly concise way of getting what I want if I do that and grab the seconds attribute. Thanks

{{ (now() - states.sensor.temperature.last_changed).seconds }}

@devastator You can get what you’re looking for (human readable string) like this:

{{ states.sensor.temperature.last_changed|relative_time }}
1 Like