Time between now and value of input_datetime

Hey all,

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.

Any input most appreciated. :slight_smile:

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?

Wow I expected something much shorter and less involved :frowning:

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

{{ (as_timestamp(now()) - as_timestamp(states.sensor.sunrise.last_updated)) | int(0) }}

Did you try

{{ relative_time(states.sensor.sunrise.last_changed) }}

yes and it returns 16 hours, when it is only 10 hours

Edit: If I do this I can get the correct answer.

{{states('sensor.sunrise') }}
{{ now() }}

{% set x1 = as_timestamp(states('sensor.sunrise')) %}
{% set x2 = as_timestamp(now()) %}
{{ x2, x1 }}
{{ x2 - x1 }}


{% set time = x2 - x1 | int(0) %}
{{ time }}
The sun rose {{ (time / 3600) | int }} hours {{ (((time / 3600) % 1) * 60) |int }} minutes ago.

I noticed in the last_changed that it does not carry the -06 in the offset but it does when you use states(). I don’t understand why.

Bug in sun2 integration???

I did this in the developer,

{{states('sensor.sunrise') }}
{{ now() }}
{% set x1 = as_timestamp(states('sensor.sunrise')) %}
{% set x2 = as_timestamp(now()) %}
{% set time = x2 - x1 | int(0) %}
The sun rose {{ (time/86400) | int }} days {{ (time / 3600 % 24) | int }} hours {{ (((time / 3600) % 1) * 60) |int }} minutes ago.

and I get this:

You will need to change sensor.sunrise to input_datetime.your_helper. There is probably a more elegant way to display the result.

the template to hold the variable would be

template:
  - sensor:
    - name: "Time Since Change"
      unique_id: "Time Since Change"
      state: >-
         {% set x1 = as_timestamp(states('sensor.sunrise')) %}
         {% set x2 = as_timestamp(now()) %}
         {% set time = x2 - x1 | int(0) %}
         {% set days = (time/86400) | int %}
         {% set hours = (time / 3600 % 24) | int %}
         {% set minutes = (((time / 3600) % 1) * 60) | int %}
         {{ days ~ 'd:' ~ hours ~ 'h:' ~ minutes ~ 'm' }}

The template will update once a minute.

and here is the screenshot from the development tools

1 Like

Is the 6 hour difference attributable to your timezone? :thinking:

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.