Google travel time in Home Assistant

Google Now is pretty clever at showing you the information you need when you need it, but it’s far from perfect. Sometimes you don’t want a clever system that tries to guess what you need to see. You want a bit more control.

One example is travel. Google’s algorithms figure out your routine, coupled with where you live and work, and present you with Google Now cards to show you how long it will take to get to work or get home. The trouble is they’re not always consistent. Sometimes you get an alert if the traffic’s bad, sometimes you don’t. Sometimes you don’t get a travel card at all. Then there are the times that you’re not following your normal routine – you might be leaving earlier or later than usual.

Of course you could go into the actual Maps application and manually check your commute time, but that’s a bit of a pain. Once again, Home Assistant offers a solution, using the Google Maps Travel Time component.

First of all set up zones for home and work:

zone:
  - name: home
    latitude: XXXXXX
    longitude: XXXXXX
    radius: 100

  - name: work
    latitude: XXXXXX
    longitude: XXXXXX
    radius: 100

You can then create two tracking entities:

sensor:
  - platform: google_travel_time 
    name: Home to work
    api_key: XXXX_XXXX_XXXX
    origin: zone.home
    destination: zone.work

sensor:
  - platform: google_travel_time 
    name: Work to home
    api_key: XXXX_XXXX_XXXX
    origin: zone.work
    destination: zone.home

Once these are added to your front end, you’ll get badges showing the commute times each way.

What if you only want to show the relevant one? You only need to know the time to get home when you’re actually at work, and you only need to know the time to get to work when you’re at home.

For that, you’ll obviously need a device tracker that can tell which zone you’re in. I’m using the GPSLogger component.

As a quick aside, the GPSLogger setup suggests configuring the app with the URL:

http://<ha_server>/api/gpslogger?latitude=%LAT&longitude=%LON&device=%SER&accuracy=%ACC&battery=%BATT&speed=%SPD&direction=%DIR&altitude=%ALT&provider=%PROV&activity=%ACT

This will give you an entity based on the serial number of your phone – i.e. device_tracker.XXXXXXXX where XXXXXXX is your serial number.

You don’t have to use the serial number, though. I changed it to be:

http://<ha_server>/api/gpslogger?latitude=%LAT&longitude=%LON&device=seansphone&accuracy=%ACC&battery=%BATT&speed=%SPD&direction=%DIR&altitude=%ALT&provider=%PROV&activity=%ACT

This gives me an entity_id of device_tracker.seansphone, which is easier to remember and use in automations. I can also keep it the same if I change my phone.

Then you can use a template sensor to pick the right value according to the state of the device tracker:

- platform: template
  sensors:
    commute:
      friendly_name: Commute
      unit_of_measurement: Min
      value_template: >-
        {%- if is_state("device_tracker.seansphone", "home") 
            {{ states.sensor.home_to_work.state }}
        {% elif is_state("device_tracker.seansphone", "work") %}
            {{ states.sensor.work_to_home.state }}
        {% else %}
            0
        {%- endif %}

You can make it more sophisticated. You might, for example, only want to show your commute to work time on a weekday and in your morning commute times:

- platform: template
  sensors:
    commute:
      friendly_name: Commute
      unit_of_measurement: Min
      value_template: >-
        {%- if is_state("device_tracker.seansphone", "home") 
               and (0 < now().strftime("%w")|int < 6) 
               and (7 < now().strftime("%H")|int < 10) %}
            {{ states.sensor.home_to_work.state }}
        {% elif is_state("device_tracker.seansphone", "work") %}
            {{ states.sensor.work_to_home.state }}
        {% else %}
            0
        {%- endif %}

This will give you a dedicated commute sensor showing the right value:


Obviously once it’s working to your satisfaction you can hide the other two sensors.

You could of course replace the work to home sensor with one that has as its origin your device location and just show that whenever you’re away, regardless of whether you’re at work or not, so that you get the time to home wherever you are.

I see two advantages in having a fixed sensor for work to home though.

First of all you could create an automation based on the sensor value to send you an alert when your commute time goes above a threshold. If the sensor showed time to home from other locations you’d get false alarms whenever you went further afield. Using the commute sensor and having it set to zero when you’re not at work and at the weekend also means you won’t get alerts when you don’t need them.

The other one? Well, why wouldn’t you want to draw a graph of your commute time? You can only do that if you have a sensor continuously monitoring the same journey…

in Home Automation

8 Comments

  1. Julien 20th October 2017
  2. seanbAuthor 20th October 2017
  3. Julien 20th October 2017
  4. seanbAuthor 20th October 2017
  5. Brandon 14th February 2018
  6. Darrell Millar 12th October 2021
  7. seanbAuthor 12th October 2021
  8. James 24th November 2021

Add a Comment

Your email address will not be published. All comments will be reviewed.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts