Calculating minutes until a time?

HI all,

I need some help, I’m totally stuck on what I thought would be simple.

I have an input for a time where I need some automations to run by. I want to look up how many minutes from now until then. I cannot for the life of me work out how to calculate the minutes as the input_datetime doesn’t have a date, so I get scrambled answers.

My failed attempts are below. Any suggestions?


This is the input I’m working with (currently set to “9:00pm”)

input_datetime:
  need_to_be_ready:
    name: Hot Tub Auto Time
    has_date: false
    has_time: true

Not working sensor:

- template:
  - sensor:
      name: "minutes_until_needed"
      state: >
        {{ (((states.input_datetime.need_to_be_ready | int)) - ((as_timestamp (now()) | int ))) }}

And another sensor attempt which also didn’t work:

- template:
  - sensor:
      name: "minutes_until_needed"
      #state: >
      #  {{ (((states.input_datetime.need_to_be_ready | int)) ))) | int }}
      value_template: >
        {% set d = now().strftime("%Y-%m-%d ") %}
        {% set t = now().timestamp() %}
        {{ d + states('input_datetime.need_to_be_ready'), '%Y-%m-%d %H:%M:%S').timestamp() - t }}

You could compare the times only using “%H”.

Pseudo;

If now() "%h" > input "%h" {
Set variable = tomorrow's date ~ input time
}else{
Set variable = today's date ~ input time
}

This will show wrong time if the input has the time 09:01 and the time is 09:02.
But you could fix that by nesting and looking at the minutes also.

Time stuff is never as simple as you think it should be… but it has gotten easier in the past 8 months with the addition of today_at, timedelta, etc.

If it’s a time only input_datetime:

{{ ((today_at(states('input_datetime.time_only')) - now()).total_seconds() / 60) |int }}

If it’s a date and time input_datetime:

{{ ((states('input_datetime.date_and_time')|as_datetime|as_local - now()).total_seconds() / 60) |int }}

EDIT: Updated to use total_seconds() as suggested below.

4 Likes

This worked perfectly - thank you so much! Also unlike the other options I tried I can see the logic int his so can tweak and re-use elsewhere, and learning is always the aim on these things, so thank you!

1 Like

If the timespan goes over a day (difference between input_datetime.date_and_time and now is more than a day), the correct template is:

{{ ((states('input_datetime.date_and_time')|as_datetime|as_local - now()).total_seconds() / 60) |int }}

Note .total_seconds() instead of .seconds.

3 Likes