After watching a bit of the upcoming features for the Google Home, I wanted to develop a way for home assistant to send proactive notifications in the event traffic is heavy between my current location and next calendar event.
It works by first grabbing the location from your calendar, then using the google travel time component to get the time both with and without traffic. The automation itself checks to see if the time with traffic is at least 10% higher (the number 10 in the automation) than the time without traffic, and then it checks to see if the time to the event plus a comfort modifier (mine is set to 30 minutes, this is usually how early I like to be) is greater than then time from now to the event. If both of these conditions are met it sends a notification
I’d appreciate anyone who uses this to help me test it, and those of you who are interested in maybe helping me clean it up a bit it would be awesome!
Thanks and enjoy!
Here is the code:
#########################################################
## Packages / Proactive Travel Notifications
#########################################################
################################################
## Customize
################################################
homeassistant:
customize:
packages.travel_notifications: &customize
package: 'travel_notifications'
################################################
## Senors
################################################
sensor:
- platform: time_date
display_options:
- 'date'
- 'time'
- 'date_time'
- platform: template
sensors:
calendar_location:
friendly_name: 'Calendar Location'
value_template: '{{ states.calendar.YOURCALENDARNAME.attributes.location }}'
- platform: template
sensors:
location_in_traffic_percent_increase:
friendly_name: "Location in Traffic Percent Increase"
value_template: "{{ (( states.sensor.current_location_to_next_calendar_event.state | int - states.sensor.current_location_to_next_calendar_event.attributes.duration.split(' ')[0] | int) / states.sensor.current_location_to_next_calendar_event.attributes.duration.split(' ')[0] | int) * 100 }}"
- platform: google_travel_time
name: Current Location to Next Calendar Event
api_key: !secret google_maps_api_key
origin: device_tracker.YOUDEVICETRACKER
destination: sensor.calendar_location
################################################
## Automation
################################################
automation:
- alias: Notify on excessive travel time
trigger:
platform: template
value_template: >-
{% if (states.sensor.location_in_traffic_percent_increase.state | int > 10) and
(( states.sensor.current_location_to_next_calendar_event.state | int + 30) >
((( as_timestamp(states.calendar.YOURCALENDARNAME.attributes.start_time) -
as_timestamp(strptime(states.sensor.date__time.state, '%Y-%m-%d, %I:%M'))) / 60) | int ) | int) %}true{% endif %}
action:
service: notify.YOURNOTIFTICATIONNAME
data_template:
message: "Traffic update, you need to leave in the next 30 minutes to get to {{states.calendar.YOURCALENDARNAME.attributes.message }} on time."