Convert Int (minutes) to datetime(minutes)?

I am trying to take an input_datetime, and subtract some minutes from it. Could someone please point me toward converting minutes (an integer) to a datetime?
Thank you!

{% set work_start = states('input_datetime.input_work_start_time') %}
{% set travel_time = states('sensor.travel_time_to_work') | int  %}

 {{ (work_start - travel_time) }}

something like this?

{{ (today_at(work_start) - timedelta(minutes=travel_time)) }}

iā€™m assuming your input_work_start_time is time only (so i put today_at). which gets a datetime(). if you want the time only, then do

{{ (today_at(work_start) - timedelta(minutes=travel_time)).time()}}

iā€™m making a few assumptions here. if this isnā€™t working or giving you what you want, please give the sample values of the in those inputs and sensors and also then an example of what you want out.

1 Like

Perfect, thank you!

I would also like to do a countdown (HH:MM). This gives the correct result, but includes decimal seconds (eg 0:09:40.480895). I am not sure how to implement | timestamp_custom(ā€œ%HH.%MMā€)? I would like eg 00:10.

  - sensor:
        name: "Countdown to Work Departure time"
        state: >-
          {% set departure_time = states('sensor.work_departure_time') %}
          {% set time_now = now() %}
          {{ (today_at(departure_time) - time_now)  }}

not positive I understand what you are askingā€¦
this ? timestamp_custom('%H:%M:%S')

note that this formats from a timestamp value, not a datetime value

You got the wrong output because you asked for hours and minutes (thatā€™s what the H and M are. I added S for seconds

This might be really helpful to you:

2 Likes

Thanks - was not quite what I was after, but it gave me some clues :slight_smile:

The below template gives this:
-1 day, 12:29:29.808038
00:29

The top result is ā€˜correctā€™ā€¦ But I would like this result displayed in HH:MM. When I try and do that I get the bottom result - which should read 12:29 (ie 12hrs, 29min). Any idea why it incorrect by 12hrs after this conversion? My temporary fix is to add 43200 - but would like to understand why this is required?

  - sensor:
        name: "Countdown to Work Departure time"
        state: >-
          {% set departure_time = states('sensor.work_departure_time') %}
          {% set time_now = now() %}
          {{ (today_at(departure_time) - time_now)  }}
          {{ (as_timestamp(today_at(departure_time)) - as_timestamp(time_now) ) | timestamp_custom('%H:%M') }}

sorry i misunderstood your question.

i think itā€™s converting to your timezone. try adding local=false

- sensor:
        name: "Countdown to Work Departure time"
        state: >-
          {% set departure_time = states('sensor.work_departure_time') %}
          {% set time_now = now() %}
          {{ (today_at(departure_time) - time_now)  }}
          {{ (as_timestamp(today_at(departure_time)) - as_timestamp(time_now) ) | timestamp_custom('%H:%M', local=false) }}

at the risk of breaking something, but for code beauty points, consider this:

- sensor:
        name: "Countdown to Work Departure time"
        state: >-
          {% set departure_time = states('sensor.work_departure_time') %}
          {% set time_now = now() %}
          {% set travel_time = (today_at(departure_time) - time_now)  %}
          {{ travel_time }}
          {{ travel_time.seconds | timestamp_custom('%H:%M', local=false) }}
1 Like

Thats perfect thank you!

One last related questionā€¦ am I able to use this new sensor sensor.countdown_to_work_departure_time as a trigger for an automation?

Something like using a value template:
{{sensor.countdown_to_work_departure_time == 01:00}}
to trigger if it is an hour before departure time? BTW, my example does not actually work :slight_smile:

yes you can use the sensor for triggering.

right, that specific example wonā€™t work , but Iā€™ll leave you to figure it out and ask if you need helpā€¦ itā€™s good to work things out to learnā€¦

one hint I would give you that isnā€™t about the error but about resiliency, is that id encourage you to use <= or >= as appropriate instead of ==ā€¦ if you think about it, itā€™s a more resilient criteria

1 Like

I am afraid I am defeated! Any other help (or solution) much appreciated! This is the sort of thing I have tried, but does not work. No doubt I am comparing apples with orangesā€¦

{{ as_timestamp(states('sensor.countdown_to_work_departure_time')) <= 01:00 }}

to make sure I have your format right, could you put this in dev tools ā†’ template
and post what you get as a result?

{{ states('sensor.countdown_to_work_departure_time') }}

you have verified that this sensor does what you want and counts down properlyā€¦ changing every minute?

in the examples above, the sensors returned 2 linesā€¦ I presume that was for testing only. The final one should only print 1 lineā€¦ the actual remaining time

if you donā€™t mind, please also post your final sensor code verbatim

1 Like

The result is
09:08 (which is correct)
Yes, the sensor is working perfectly thank you :slight_smile: And yes, the two lines were for testing only. This is the final sensor code:

template:
  - sensor:
        name: "Countdown to Work Departure time"
        state: >-
          {% set departure_time = states('sensor.work_departure_time') %}
          {% set time_now = now() %}
          {{ (as_timestamp(today_at(departure_time)) - as_timestamp(time_now) ) | timestamp_custom('%H:%M', local = false) }}

ok, so Iā€™ll give you an answer, but Iā€™ll also try to convince you not to use itā€¦ knowing Iā€™m likely to fail to do soā€¦

I think this should trigger for you.

{{  strptime(states('sensor.countdown_to_work_departure_time'),'%H:%M') <= 
strptime('01:00','%H:%M')
}}

the reason why yours didnā€™t work is the reason I think you shouldnā€™t use itā€¦ you tried to do as_timestamp but the object you gave it isnā€™t any time objectā€¦ it was a string.

The wacky thing here is that you had a valid time object in the sensor that you converted to a string. Then here you are converting it back. this would be like if excel stored all itā€™s data as strings and when you did any math, it converted the string to a number then did the math, converted it back to a string and stored the string again.

the ā€œrightā€ way is for a model/view separationā€¦ this means that in the data model be pure and nativeā€¦ ie, store the native data object. In this case a timedelta is probably right. then in the places where you want to view it, convert that to the view that you want (e.g. h:m without seconds). do the view manipulation in the UI code. Keep the data good and clean.

This means your sensor should be

template:
  - sensor:
        name: "Countdown to Work Departure time"
        state: >-
          {% set departure_time = states('sensor.work_departure_time') %}
          {% set time_now = now() %}
          {{ today_at(departure_time) - time_now) }}

Then this trigger becomes something like this:

{{ states('sensor.countdown_to_work_departure_time') <= timedelta(hours=1) }}

once again I am doing this on mobile so itā€™s free coded ā€¦ not testedā€¦ no refunds :slight_smile:

But hopefully you get the concept and that unblocks you

Thank you very much for taking the time to not only give me a solution, but explain that there is a better solution. So you have convinced me to use the ā€˜betterā€™ way :slight_smile: Thanks again!

coolā€¦ so if you go down that path, then sensor.countdown_to_work_departure_time becomes a proper timedelta object. so if you want to display that in hour:minute format you would then do this:

{{ states('sensor.countdown_to_work_departure_time').total_seconds() | timestamp_custom('%H:%M', local=false) }}

1 Like