Receive a notification when its time to leave for Work based on google travel time sensor

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"
8 Likes

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

1 Like

Just what I need! This shall keep the mrs happy!

Would you share the whole automation please? and… how do you set time in an integer format?

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?

1 Like

Sorry to ask you, but i dont understand when you said "8.33 is 8:20 and 8.5 would be 8:30.
Can you explain the formule?

Example; 8:00 would be what? 8:10 etc 8.15 etc…

thanks you

Break up time in to a decimal.
8am - 8.0
8:15 - 8.25
8:30 = 8.5
8:45 = 8.75
9am = 9.0

ok now i understad,
60 min = 100

on your example:

a )8:15 ---- 15 / 60 = 0,25
b) 8:30 — 30 / 60 = 0,50
c) 8:45 — 45 /60 = 0.75

Result:

a) 8:25
b) 8:50
c) 8:75

Thanks for attention

1 Like

If anyone can help me with the following, i have a stop off on the way before getting to my work (school drop off) how can i include that

eg: {% set t = strptime(states.sensor.time.state,’%H:%M’) %}
{{ ( t.hour + t.minute / 60 ) > ( 8.33 - states.sensor.time_to_work.state | int / 60) + ( 8.33 - states.sensor.time_to_SCHOOL.state | int / 60) }}

If anyone knows please

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 %}

NODE-RED Flow