This was a pet project for me that I am quite happy with how it turned out. With A LOT of help from @petro, here is my “Time to get going Alert”
In my example, I want to be to work by 8:30am.
The uses the following components
Zone, workday, uptime (prevents false alerts after restart), time_date, google travel time, and a notification service of your choice (I use pushbullet)
I actually have a couple of different work locations set up as zones.
configuration.yaml
zone:
- name: WORK
latitude: 36.123456
longitude: -76.123456
radius: 300
icon: mdi:domain
binary_sensors.yaml
- platform: workday
country: US
workdays: [mon, tue, wed, thu, fri]
sensors.yaml
- platform: time_date
- platform: uptime
name: "HA runtime in minutes"
unit_of_measurement: minutes
# Commute to work
- platform: google_travel_time
name: Time to work
api_key: !secret google_maps_api_key
origin: device_tracker.my_iphone
destination: zone.work
# in the time below 8.33 is time in an integer format, so 8.33 is 8:20am. 8.5 would be 8:30am, etc
time_to_leave_for_work:
value_template: >
{% set t = strptime(states.sensor.time.state,'%H:%M') %}
{{ ( t.hour + t.minute / 60 ) > ( 8.33 - states.sensor.time_to_work.state | int / 60) }}
friendly_name: 'Time to Leave for work'
automations.yaml
- alias: "Notify Me when its time to leave for Work"
trigger:
- platform: state
entity_id: sensor.time_to_leave_for_work
to: 'True'
from: 'False'
condition:
- condition: numeric_state
entity_id: sensor.ha_runtime_in_minutes
above: 1
- condition: state
entity_id: binary_sensor.workday_sensor
state: 'on'
# use this if you only want the alerts for specific days, tuesday is the example below
- condition: time
weekday:
- tue
action:
- service: notify.pushbullet_notifications
data:
message: "Based on current traffic, you have 10 minutes to leave if you want to be to the Work by 8:30am"
target:
- email/[email protected]
title: "Time to Leave for Work"
This has been working well for me. I’ve found that 95% of the time traffic is normal, so I get this alert the same time every morning. Which meant that over time I kinda did not pay much attention to it. I’ve added this sensor
me_traffic_density_to_work:
friendly_name: Traffic to work
value_template: >-
{% if states("sensor.time_to_work_me") | float >= 21 %}
Heavy
{% elif states("sensor.time_to_work_me") | float < 21 and states("sensor.time_to_work_me") | float >= 16 %}
Moderate
{% elif states("sensor.time_to_work_me") | float < 16 and states("sensor.time_to_work_me") | float >= 8 %}
Normal
{% else %}
unknown
{% endif %}
I now only send alerts if the traffic is moderate or heavy. I plan on doing other stuff, just have not gotten around to it, but you could do TTS, or turn a colored bulb red, etc
Here is the update I use based on the traffic density
- alias: "Notify me when its time to leave for work"
trigger:
- platform: state
entity_id: sensor.time_to_leave_for_work
to: 'True'
from: 'False'
condition:
- condition: numeric_state
entity_id: sensor.ha_runtime_in_minutes
above: 1
- condition: state
entity_id: binary_sensor.workday_sensor
state: 'on'
- condition: or
conditions:
- condition: state
entity_id: sensor.me_traffic_density_to_work
state: 'Moderate'
- condition: state
entity_id: sensor.me_traffic_density_to_work
state: 'Heavy'
action:
- service: notify.pushbullet_notifications
data:
message: "Based on current traffic, you have about 10 minutes to leave if you want to be to work by 8:30am"
target:
- email/[email protected]
title: "Traffic is heavier than usual!"
Basically, if you use this automation you’ll only get alerts when traffic is heavy or moderate
As far as time being an integer, I copied the idea from some other automation in the community. I think it’s always an integer when you want to do math, maybe?
I am trying to recreate this via Node-Red and everything is working except checking for the traffic density sensor. It only shows unknown. Can you tell why?
Sensors
- platform: waze_travel_time
name: Morning Commute
origin: HOME ZONE
destination: WORK ZONE
region: 'US'
- platform: template
sensors:
time_to_leave_for_work:
friendly_name: 'Time To Leave For Work'
value_template: >
{% set t = strptime(states.sensor.time.state,'%H:%M') %}
{{ ( t.hour + t.minute / 60 ) > ( 7 - states.sensor.morning_commute.state | int / 60)
traffic_density_to_work:
friendly_name: 'Traffic To Work'
value_template: >-
{% if states("sensor.time_to_leave_for_work") | float >= 26 %}
Heavy
{% elif states("sensor.time_to_leave_for_work") | float < 26 and states("sensor.time_to_work_me") | float >= 22 %}
Moderate
{% elif states("sensor.time_to_leave_for_work") | float < 22 and states("sensor.time_to_work_me") | float >= 10%}
Normal
{% else %}
unknown
{% endif %}