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?
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 ) }}
Ok, not sure how those attributes are formatted in the first place.
If you remove the “| int” will that give you another non 0 value?
sorry, that just gives “none”
i think the “time” values are maybe just a string? thehy show as just “13:39” for example?
I found this in another thread.
{% set time = "12:45" %}
{{ (((as_timestamp(strptime(time, "%H:%M"))) - as_timestamp( strptime( now().strftime("%H:%M"), "%H:%M") )) /60) | int}}
It seems to work to calculate a time in string format.
Maybe you can replace “time” and “now” with your sensor attributes?
This will give you the time in minutes (143) between kickofftime and finishtime.
{% set kickofftime = "10:45" %}
{% set finishtime = "13:08" %}
{{ (((as_timestamp(strptime(finishtime, "%H:%M"))) - as_timestamp(strptime(kickofftime, "%H:%M") )) /60) | int}}
Hi I’ve just stumbled upon this thread as I’m trying to do something similar to what OP was doing with working out a departure time. I’ve got the added complication of a TTS automation though.
This thread has been fantastic with figuring out the template for what I want, and I’ve got it to generate a departure time that’s definitely correct, but when I put it into TTS, it will say “…you should set off before x hours, y minutes, zero seconds”.
Here’s the function I use to calculate the departure time:
{{ strptime('08:00', '%H:%M') - strptime(states.sensor.time_to_work.state, '%M') }}
Now how do I separate out the hour and minute values so I can then insert them into the TTS string and make it say “…you should set off before y minutes past x”?
Just tacking onto this thread with a quick question.
The above solutions all use attributes, but I need a state (hours between now and forecast), but below does not resolve
{{as_timestamp(states('sensor.openweathermap_forecast_time')) - as_timestamp(now()) | timestamp_local }}
gives
TypeError: unsupported operand type(s) for -: 'float' and 'str'
However
{{as_timestamp(states('sensor.openweathermap_forecast_time')) | timestamp_custom("%H") }}
Results in the hour, expected because forecast_time is a timestamp…
{{states('sensor.openweathermap_forecast_time') | as_datetime - now() }}
results in 0:31:59.997861
Even tried using strptime
, no joy
What about
{% set n = as_timestamp(now()) %}
{% set x = as_timestamp( states('sensor.openweathermap_forecast_time') ) %}
{% set t = (n-x) |timestamp_custom('%R') %}
{{ t }}