I have been trying to get this going now for way too long. What I would like is a sensor that holds a value like “1 day, 10 minutes” (or similar) which is populated by the time difference between now and what an “input_datetime” sensor has for its value.
In other words, If the “input_datetime” sensor has 2022-08-17 14:19:37, and now is 2022-08-17 15:19:37, my new sensor should say 1 hour.
I was testing in templates to answer @smolandia question. My template is this:
{{ states.sensor.sunrise.last_changed }}
{{ now() }}
{% set x1 = as_timestamp(states.sensor.sunrise.last_changed) %}
{% set x2 = as_timestamp(utcnow()) %}
{{ x2, x1 }}
{{ x2 - x1 }}
{{ (as_local(now()) - as_local(states.sensor.sunrise.last_changed)) }}
{{ (as_timestamp(as_local(utcnow())) - as_timestamp(as_local(states.sensor.sunrise.last_changed))) }}
{% set time = (as_timestamp(now()) - as_timestamp(states.sensor.sunrise.last_updated)) | int(0) %}
{{ time }}
{% set time1 = (time - 6*3600) %}
{{time1}}
The sun rose {{ (time1 / 3600) | int }} hours {{ (((time1 / 3600) % 1) * 60) |int }} minutes ago.
This is what I get. I am forced to correct for time zone to get a correct answer. I am america/denver time zone. So with daylight savings I am 6 hrs behind UTC
I always got the time from utc to sunrise. I have tried all permutations of utcnow, as_local and as_timestamp and nothing made a difference. It was always +6 on the time. Does anyone have an explanation, do I just not understand something, or is this a bug?
It would have been much shorter but I was testing trying to find the right combo. It was my understanding that I didn’t have to correct for UTC to my timezone. I should have gotten the correct number of seconds from this
Is the 6 hour difference attributable to your timezone?
Something is odd because when I try the same thing with an entity’s last_changed attribute, relative_time reports the correct time interval (i.e. timezone is handled correctly).
Yes it is related to timezone. Let me try your template editor and see if that is correct. Working on it.
I see what it is now. The value of my sunrise today was 06:08:00 but the variable was changed at midnight. The time is 16 hours from being changed but only 10 hours from sunrise.
The contents of the variable may not be when it was changed. I am so dumb sometimes.