Google Inspired Proactive Traffic Notifications

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."
12 Likes

What does the stuff in the Customize section do?

Can you also hardcode this to base this off of everyweek day at 5pm. I have to pick up my child at daycare before 5pm and I have tried to create an easy template but can’t get it working. This is what I’m trying:

{{ strptime("17:00", "%H:%M") }} 

That’s not working though…

Without creating and setting up a calendar event, I don’t know how to go about doing that correctly. It can’t be hard right?!?!

Maby a stupide question but how do you get the travel time sensors for “no traffic” ?

Easiest way might just be to replace this code:

as_timestamp(states.calendar.YOURCALENDARNAME.attributes.start_time)

With this:

as_timestamp(strptime('17:00', '%H:%M'))

I guess I’m not following, what do you mean by “no traffic”?

If I understand you right you are comparing the travel time between A and B without any traffic delays with with the estimated time in the. how do you get the travel time from A to B without any traffic delays?

The google_travel_time platform provides both a travel time and travel time with traffic for supported routes.

Appreciate the assistance!

It didn’t work unfortunately, but I’m going to keep trying.

I’ve tried all sorts of things, but I keep failing on creating a true timestamp due to formatting that I am unfamiliar with. This gets me the closest to what I want, but then fails when trying to subtract time in traffic.
{{ strptime("17:00", "%H:%M:%S") }}

This is when it fails:
{{ as_timestamp(strptime("17:00", "%H:%M:%S")) }}

That returns 'None' in the template editor.

I’ll keep trying, but thanks for a cool example!

I only have one sensor and that is with traffic for supported routs, that way I am asking how you get the sensor for only travel time without traffic included.

Just guessing here, but since you define the format with seconds, try providing the seconds as well. “17:00:00”

Yeah sorry, I forgot that was included when I pasted it in there. I tried it both ways.

In the template editor I can get this to work with and without seconds, but I’m formatting it incorrectly somehow. I think the seconds are messing it up, but I don’t know how to cut them off or format them out.