I have an input_datetime sensor. I’m trying to create a template sensor that shows the relative time since the timestamp in the input_datetime sensor as “X hours and Y minutes ago”.
When using {{ relative_time(states.input_datetime.example.last_changed) }}, if it’s more than 1 hour since the timestamp, the output is simply 1 hour but I would like it to say how many minutes as well.
If you don’t mind could you test it for me to see if you get the correct results, please?
if it does work, TBH, I’m really not sure what’s going on with the first two attempts as to why they didn’t work. And I can’t explain what the reason is for why adding “false” to the custom timestamp formatting fixes the problem.
From my understanding and the testing I did in the template editor I am getting some strange results so I can’t explain it at all.
These shouldn’t matter. you can use as_timestamp(now()) or as_timestamp(utcnow()) and they both should return the same value. datetime objects have timezones associated with them and as_timestamp converts it into an integer that is based on UTC.
The issues lie in timestamp_custom('%H:%M', false). This assumes the input is a timestamp. When you subtract 2 timestamps, they are no longer timestamps. It’s now an int but it just so happens to work with timestamp_custom. But the problem with timestamp_custom is that it assumes Local so without the false it offsets the delta it by the timezone because timestamp_custom isn’t meant for time differences. It’s meant for moments in time, i.e. a timestamp int.