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) }}
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.
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) }}
Thanks - was not quite what I was after, but it gave me some clues
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') }}
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
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
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…
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
The result is 09:08 (which is correct)
Yes, the sensor is working perfectly thank you 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) }}
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) }}
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 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: