2 templates results differs in time


Ive got a result I don’t expect

I’ve 2 templates that look at the same sensor but reports 2 different results. How come?

1. {{ (as_timestamp(now()) - as_timestamp(states.sensor.value_ath.last_changed | default(0)) | int ) | timestamp_custom("%m minutes ago", false)}}

2. {{(as_timestamp(states.sensor.value_ath.last_changed) | timestamp_custom('%d %B - %H:%M'))}}

1: 01 minutes ago
2: 08 May - 04:03 PM

The first result is definitely not the same time as the second. It differs like 4 hours.

In the first template you are confusing a time duration with a moment in time. There are different things. For anything over 59 minutes this will produce wrong results.

Also, you are asking for the month %m, not the minutes %M.

As the other posted mentioned, applying the timestamp_custom() function to a difference in two times doesn’t make any sense.

You don’t even need to though, because the result of subtracting two timestamps is the number of seconds between them. To get minutes, just divide by 60.

{{ ((as_timestamp(now()) - as_timestamp(states.binary_sensor.pantry_fp2.last_changed | default(0))) / 60) | int  }} minutes ago

Your | int was also positioned such that it was only affecting the timestamp for last_updated, but not the one for now(), so I changed the parentheses around as well.

Thanks for for suggestion. It works but it’s not readable atm as the result is something like 6575 minutes ago.
I think it’s better readable when you see xxx hours and xxx minutes ago…
Do you have such template?

Provided you’re running a relatively-recent version:

{{ time_since(states['binary_sensor.pantry_fp2']['last_changed'], precision=2) }}

Documented here: