Basically i have “12:00:00” and something like “1 hour 19 mins”.
I would like to automatically get the result: 10:41:00
Tried looking at the jinja2 webpage, but yeah… different time format and also accounting the 60mins=1hour silliness.
Basically i have “12:00:00” and something like “1 hour 19 mins”.
I would like to automatically get the result: 10:41:00
Tried looking at the jinja2 webpage, but yeah… different time format and also accounting the 60mins=1hour silliness.
You’ll wanna convert to timestamps. Those are like Unix seconds-since-epoch times. Here’s a snippet where I look at the end time of a calendar event for use in a template sensor. 900 seconds = 15 minutes
{% if states.calendar.nathan_tech_shop %}
{% if is_state("calendar.nathan_tech_shop", "on") and as_timestamp(states.calendar.nathan_tech_shop.attributes.end_time) - as_timestamp(now()) < 900 %}on{% else %}off{% endif %}
{% else %}off{% endif %}
I already tried that I will still try to make your snippet usefull!
I forgot to add that the 1hour 19mins is actually a travel duration. So thats how i need to get to 10:41:00
I cant convert 1hour19mins to timestamps
Or do math.
1:19 = 60 sec * 79min = 4740sec
(instead of the 900 in my example)
Easiest way would be something like this:
{{ as_timestamp(states.calendar.nathan_tech_shop.attributes.end_time) - as_timestamp(now()) | timestamp_local }}
timestamp_local is a filter for jinja that will take a unix timestamp and covert it to a regular datetime. In your timezone.
Thank you both! Im kinda in the right direction.
{{ as_timestamp(states.calendar.work.attributes.start_time) -4740 | timestamp_local}}
This shows nothing (blank) in the template editor. I will thinker with this more!
Got it working!
{{ (as_timestamp(states.calendar.work.attributes.start_time) - states.sensor.google_travel_time__transit.attributes.duration.split(" ")[0] | int *3600 + states.sensor.google_travel_time__transit.attributes.duration.split(" ")[2] | int *60 | int) | timestamp_local}}
This converts and calculates 1 hour 21 mins into seconds and into unix, etc.
Thank both of you!!
Cool! This will help me. How hard would it be to generate a TTS at the time of the appointment - travel time? This is basically what I’m trying to achieve at this thread here…
Thanks for sharing!
Aren’t you supposed to use “duration_in_traffic” instead of “duration” for a more accurate calculation?
Does this script also work for durations less than 1 hour?
That’s rather easy actualy, this is part of my script for a “morning Briefing TTS”:
commute_time_to_work:
alias: Commute time to work
sequence:
- alias: Speech Output
service: tts.google_say
entity_id: media_player.cc_audio
data_template:
message: 'Goodmorning. The outside temperature is now {{ states.sensor.dark_sky_temperature.state }} degrees celcius. The current commute time to work is {{ states.sensor.google_travel_time__driving.state }} minutes by car.'
cache: false
Next step is to give me a heads-up when I need to leave to arrive on time, based on traffic conditions.
I’m having trouble comparing 2 timestamps.
Goal: I have set up a wake-up light automation and included a sensor in the group which shows the current alarm time set (adjustable by minutes and hour sliders).
I want to add a sensor (?) which shows to time left untill my next alarm. The problem is that it looks like that the alarmtime is seen as a string of characters instead of a timestamp. Therefore I can’t use them in calculations.
In my automation I use the following data_template as trigger for my alarm (20 minutes before wakeup time):
'{{ ((now().strftime("%s") | int + 1200) | timestamp_custom("%H:%M")) == states.sensor.alarmtime.state }}'
alarmtime is a state:
states.sensor.alarmtime.state
And must be compared with:
((now().strftime("%s") | int ) | timestamp_custom("%H:%M"))
Can someone point me in the right direction?
My template is calculating the optimal time to leave your house. Sadly it only works for >1hour driving times.
@GMFalka @ih8gates
Hey, I was able to make this work for < 1hour driving times! Just added a couple if statements to check if the string starts with hour(s), otherwise use only the minutes for the calculation.
{% if is_state("states.sensor.home_commute.attributes.duration_in_traffic.split(" ")[1]", "hour") or is_state("states.sensor.home_commute.attributes.duration_in_traffic.split(" ")[1]", "hours") -%}
{{ (as_timestamp(now()) + states.sensor.home_commute.attributes.duration_in_traffic.split(" ")[0] | int *3600 + states.sensor.home_commute.attributes.duration_in_traffic.split(" ")[2] | int *60 | int) | timestamp_custom("%I:%M %p")}}
{%- else -%}
{{ (as_timestamp(now()) + states.sensor.home_commute.attributes.duration_in_traffic.split(" ")[0] | int *60) | timestamp_custom("%I:%M %p")}}
{%- endif %}
I’ve been unsuccessfully trying to find the solution to this for what feels like an eternity, and this thread finally got me the results I was looking for, so THANK YOU ALL!
I am trying to do something similar, but have not found a solution so far …
I want to use a calculated delay in an automation, but have no idea how to do it …
- delay:
minutes: 120
works perfectly fine.
But how to use a calculated value instead?
- delay:
minutes: {{((((as_timestamp(states.sun.sun.attributes.next_dawn)) - (as_timestamp(states.sun.sun.attributes.next_dusk))) / 60 ) ) | int }}
does work when pasting it on the Templates page and returns 423.
@tmeringer minutes doesn’t take a template just the delay part and it looks for something like this: ‘HH:MM:SS’. Try this:
- delay: >-
{% set next_dawn = (as_timestamp(states.sun.sun.attributes.next_dawn)) | int %}
{% set next_dusk = (as_timestamp(states.sun.sun.attributes.next_dusk)) | int %}
{% set duration = next_dawn - next_dusk %}
{% set seconds = duration % 60 %}
{% set minutes = (duration / 60)|int % 60 %}
{% set hours = (duration / 3600)|int %}
{{ [hours, minutes, seconds]|join(':') }}
Wonder if anyone can help me with the following I want to have a count down timer to display in (Hours : Min ) a leaving time eg: " that it looks at the traffic conditions and alters the time "sensor.google_travel_time__driving - the current time " so it outputs time to leave in 20 min,19min,18min 17min ect down to 0 min " being struggling for days if anyone has any suggestions
This is the closest i have come {{strptime(“07:15”, “%H:%M”) - strptime(states.sensor.total_travel_time_now.state, “%M”) }} that outputs 06:57:00 not the desired 20min,19min 18min ect every way i have trided just gives me no results
Thank you for the solution. But I got the results in ‘H:M:S’ format.
I used this for results in ‘HH:MM:SS’ format:
{{ '%0.02d:%0.02d:%0.02d' | format(hours, minutes, seconds)}}
Hey all,
need some help with time please.
im trying to work out the “total time” (so end - start)
however I keep getting an error when i try and subtract the values?
maybe because the “time” is only in “hours:min”?
{{ states.sensor.dishwasher_g7310_status }}
finish time: {{ states.sensor.dishwasher_g7310_status.attributes.finishTime}}
kickoff time: {{states.sensor.dishwasher_g7310_status.attributes.kickoffTime}}
remaining time: {{ states.sensor.dishwasher_g7310_remaining_time.state }}
results:
<template TemplateState(<state sensor.dishwasher_g7310_status=In use; programType=Operation mode, rawProgramType=0, programPhase=Main Wash, rawProgramPhase=1795, dryingStep=, rawDryingStep=None, spinningSpeed=None, rawSpinningSpeed=None, ventilationStep=, rawVentilationStep=None, progress=0.0, finishTime=13:08, kickoffTime=10:49, friendly_name=Dishwasher G7310 Status @ 2020-10-08T09:43:16.002482+01:00>)>
finish time: 13:08
kickoff time: 10:49
remaining time: 02:19
You need to convert the time to “timestamp” first and do your calculation. You can convert the result back to hours and minutes.
{{ ( as_timestamp (states.sensor.dishwasher_g7310_status.attributes.finishTime) | int ) }}