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
Hello,
Did you experience any issue regarding insertion of google_travel_time sensor in influxdb?
I also have these 2 sensors on my HA but nothing in infludb.
Julien
I didn’t have any issues that I recall.
Are you taking all measurements into InfluxDB or are you filtering them? I assume you’ve got InfluxDB working with other sensors?
Measurements are named by default after the unit, so in this case it will be “min” for minutes. The Grafana query that generated the graph above was:
SELECT “value” FROM “min” WHERE (“entity_id” = ‘work_to_home’) AND $timeFilter
Thank you so much, i was looking for a name like “sensor.work_to_home”…
I can see it in influxdb and grafana 🙂
Thanks again!
No problem, glad I could help 🙂
I have another post where I talk about InfluxDB and how HomeAssistant measurements are named:
https://seanb.co.uk/2017/07/influxdb-with-home-assistant/
There are some override options, but the entity_id is always going to be a tag – and therefore part of your where clause – rather than the measurement name.
Awesome tutorial, thanks for outlining everything, made setup super smooth for me!
It’s been a few years since you did this, but I stumbled across your post in trying to do it myself and running in to issues.
My setup is just like yours, but my entities only show one zone, showing nothing. It also gives me an integration, but again, just the one zone.
Did yoiu run in to any similar issue when first doing this? I would love to get these badges to show up.
I don’t recall those issues – have you got the zones set up? I think it’s done through the user interface now, as can the Google Travel Time integration.
I don’t actually use the integration any more, and didn’t set it up when I last rebuilt my HA instance. That’s what comes from working from home now – my commute is from my bedroom to the spare room!
Hello! Thanks for the info, so far I think I’ve got about half way. I had to reboot my server to get the devices to appears. however i’m a little confused as to how I get them to display on my home screen.
I get an “unknown” when I look at the sensor data. How do I narrow down if I’d done something wrong with adding my zones or if there’s an issue somewhere else?