ssajohnson
(Ssajohnson)
December 5, 2023, 2:23am
1
Hey,
I’m looking to build a sensor that’ll tell me when I need to leave for work to get there by 7am. I want to start at 7am substract the waze travel time and give myself 10 minutes warning. I’m sure it has something to do with the time formatting.
Thanks!
- name: "Time_to_Leave"
state: >
{{ ( states('sensor.StartWork') - states('sensor.waze_travel_time') - timedelta(minutes=10) | timestamp_custom('%H:%M')) }}
tom_l
December 5, 2023, 2:46am
2
States are strings and entity ids do not contain capital letters. Assuming your sensors have the required format:
{{ ( states('sensor.startwork')|as_datetime - states('sensor.waze_travel_time')|as_datetime - timedelta(minutes=10) | timestamp_custom('%H:%M')) }}
If they don’t have the required format it will require a bit more work. Post the results from the dev tools template editor for this:
{{ states('sensor.startwork') }}
{{ states('sensor.waze_travel_time') }}
ssajohnson
(Ssajohnson)
December 5, 2023, 3:21am
3
Might help if I get the name right, I set up startwork as a helper:
{{ states('input_datetime.startwork') }}
{{ states('sensor.waze_travel_time') }}
This is what it gives:
07:00:00
28
tom_l
December 5, 2023, 3:49am
4
Try this:
{{ ( today_at(states('input_datetime.startwork')) - timedelta(minutes=states('sensor.waze_travel_time')) - timedelta(minutes=10) ).strftime('%H:%M') }}
ssajohnson
(Ssajohnson)
December 5, 2023, 4:21am
5
This got it, changed the waze to a float, thank you so much!
{{ ( today_at(states('input_datetime.startwork')) - timedelta(minutes=states('sensor.waze_travel_time')| float(0)) - timedelta(minutes=10) ).strftime ('%H:%M')}}
Last question, to get it to trigger an automation do I need to remove the strftime (‘%H:%M’) since it’s now passing the time as a string?
tom_l
December 5, 2023, 4:34am
6
Yeah if you get rid of that you can use a time trigger:
trigger:
- platform: time
at: sensor.time_to_leave
FYI you can use spaces in your sensor name:
- name: "Time to Leave"
This will still result in the entity id shown in the trigger.
EDIT you will also have to add this to your sensor:
- name: "Time to Leave"
device_class: timestamp # add this
state: > ...etc
https://www.home-assistant.io/docs/automation/trigger/#sensors-of-datetime-device-class
ssajohnson
(Ssajohnson)
December 5, 2023, 4:41am
7
Amazing, thank you so much.
1 Like