Custom Component: open_route_service travel_time

Someone found openrouteservice.org in this post.

I added it as a custom component for you to use.

To make things easier you can install it as a custom repo using HACS

You can find all the information and the code itself here:

GitHub Release
GitHub Activity
hacs_badge
License

Project Maintenance
BuyMeCoffee

Community Forum

Homeassistant Custom Component sensor provides travel time from openrouteservices.org.

This component will set up the following platforms.

Platform Description
sensor Show travel time between two places.

Setup

You need to register for an API key here.

Openroute Services offers a Free Plan which includes 1.000 free requests (For reverse geocoding) per day. More information can be found here

Configuration

To enable the sensor, add the following lines to your configuration.yaml file:

# Example entry for configuration.yaml
sensor:
  - platform: open_route_service
    api_key: "YOUR_API_KEY"
    origin_latitude: "51.222975"
    origin_longitude: "9.267577"
    destination_latitude: "51.257430"
    destination_longitude: "9.335892"

Configuration options

Key Type Required Description
api_key string true Your application’s API key (get one by following the instructions above).
origin_latitude string true The starting latitude for calculating travel distance and time. Must be used in combination with origin_longitude. Cannot be used in combination with origin_entity_id
origin_longitude string true The starting longitude for calculating travel distance and time. Must be used in combination with origin_latitude. Cannot be used in combination with origin_entity_id
destination_latitude string true The finishing latitude for calculating travel distance and time. Must be used in combination with destination_longitude. Cannot be used in combination with destination_entity_id
destination_longitude string true The finishing longitude for calculating travel distance and time. Must be used in combination with destination_latitude. Cannot be used in combination with destination_entity_id
origin_entity_id string true The entity_id holding the starting point for calculating travel distance and time. Cannot be used in combination with origin_latitude / origin_longitude
destination_entity_id string true The entity_id holding the finishing point for calculating travel distance and time. Cannot be used in combination with destination_latitude / destination_longitude
name string false A name to display on the sensor. The default is “HERE Travel Time”.
mode string false You can choose between: cycling-regular, driving-car or foot-walking. The default is driving-car.
route_mode string false You can choose between: fastest, or shortest. The default is fastest
unit_system string false You can choose between metric or imperial. Defaults to metric or imperial based on the Home Assistant configuration.
scan_interval integer false “Defines the update interval of the sensor in seconds. Defaults to 300 (5 minutes).”

Dynamic Configuration

Tracking can be set up to track entities of type device_tracker, zone, sensor and person. If an entity is placed in the origin or destination then every 5 minutes when the platform updates it will use the latest location of that entity.

# Example entry for configuration.yaml
sensor:
  # Tracking entity to entity
  - platform: open_route_service
    api_key: "YOUR_API_KEY"
    name: Phone To Home
    origin_entity_id: device_tracker.mobile_phone
    destination_entity_id: zone.home

Entity Tracking

  • device_tracker
    • If the state is a zone, then the zone location will be used
    • If the state is not a zone, it will look for the longitude and latitude attributes
  • zone
    • Uses the longitude and latitude attributes
  • sensor
    • If the state is a zone, then will use the zone location
    • All other states will be passed directly into the HERE API
      • This includes all valid locations listed in the Configuration Variables

Updating sensors on-demand using Automation

You can also use the homeassistant.update_entity service to update the sensor on-demand. For example, if you want to update sensor.morning_commute every 2 minutes on weekday mornings, you can use the following automation:

automation:
- id: update_morning_commute_sensor
  alias: "Commute - Update morning commute sensor"
  initial_state: 'on'
  trigger:
    - platform: time_pattern
      minutes: '/2'
  condition:
    - condition: time
      after: '08:00:00'
      before: '11:00:00'
    - condition: time
      weekday:
        - mon
        - tue
        - wed
        - thu
        - fri
  action:
    - service: homeassistant.update_entity
      entity_id: sensor.morning_commute

Buy Me A Coffee

5 Likes

The PR to enable this repo as a default repo for HACS is still open. You can always add it as a custom repo if you don’t want to wait =)

Thanks for this, works well.

1 Like

Wow, how has this project only one reply after a year. Glad the guy commented, otherwise I would never have seen this component. This looks great!

1 Like

@eifinger Is there anyway to avoid error on startup since “device_tracker.mobile_phone” is not available until HA is fully loaded?

Is this the error you are seeing?`

Yes, it’s complaining about the entity no being available.

2020-09-21 07:50:01 ERROR (MainThread) [custom_components.open_route_service.sensor] Unable to find entity device_tracker.mobile_phone

I should be able to fix that in the next few days. And than try to overcome the shame of forgetting about that open issue :slightly_frowning_face:

Not a major issue, just doing some deep house cleaning since 0.115 is bugging down my system

I released a new version v1.0.2 which should fix the issue.

Just tried it, but it’s giving the following error.

2020-09-22 06:22:22 ERROR (MainThread) [homeassistant.components.sensor] Error while setting up open_route_service platform for sensor
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/entity_platform.py", line 201, in _async_setup_platform
    await asyncio.gather(*pending)
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/entity_platform.py", line 310, in async_add_entities
    await asyncio.gather(*tasks)
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/entity_platform.py", line 481, in _async_add_entity
    await entity.add_to_platform_finish()
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/entity.py", line 521, in add_to_platform_finish
    await self.async_added_to_hass()
  File "/home/homeassistant/.homeassistant/custom_components/open_route_service/sensor.py", line 181, in async_added_to_hass
    @callback
NameError: name 'callback' is not defined

I just released v1.0.3, I forgot to import the callback

2020-09-22 07:18:22 ERROR (MainThread) [custom_components.open_route_service.sensor] Unable to find entity device_tracker.mobile_phone

It is working with a test sensor from zone to zone, it’s just throwing the error because it can’t find the device on startup.

Couple errors on update, not sure if it’s because I’m at home.

Logger: homeassistant.helpers.entity
Source: custom_components/open_route_service/sensor.py:337
First occurred: 7:48:33 PM (4 occurrences)
Last logged: 8:03:33 PM

Update for sensor.phone_time_to_home fails
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/entity.py", line 278, in async_update_ha_state
    await self.async_device_update()
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/entity.py", line 469, in async_device_update
    await self.async_update()  # type: ignore
  File "/home/homeassistant/.homeassistant/custom_components/open_route_service/sensor.py", line 252, in async_update
    await self._hass.async_add_executor_job(self._open_route_data.update)
  File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/home/homeassistant/.homeassistant/custom_components/open_route_service/sensor.py", line 337, in update
    self.duration = summary["duration"]
KeyError: 'duration'

It is indeed an error when destination and origin are the same coordinates. Thank you!
I will fix this soon: https://github.com/eifinger/open_route_service/issues/4

1 Like

This is now fixed with release v1.0.4 available via HACS.

Special thanks to @MatthewFlamm for the awesome pytest-homeassistant-custom-component!

1 Like

Awesome, thank you and @MatthewFlamm as well, seems to be working great.

Hey, seeing the following repeated error for each sensor, not sure what it pertains to.

Logger: homeassistant.helpers.entity
Source: custom_components/open_route_service/sensor.py:355
First occurred: 11:00:24 AM (13 occurrences)
Last logged: 1:10:17 PM

Update for sensor.wife_to_home fails
Update for sensor.phone_time_to_home fails
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/entity.py", line 278, in async_update_ha_state
    await self.async_device_update()
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/entity.py", line 469, in async_device_update
    await self.async_update()  # type: ignore
  File "/home/homeassistant/.homeassistant/custom_components/open_route_service/sensor.py", line 252, in async_update
    await self._hass.async_add_executor_job(self._open_route_data.update)
  File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/home/homeassistant/.homeassistant/custom_components/open_route_service/sensor.py", line 355, in update
    reverse_geocode_destination
  File "/srv/homeassistant/lib/python3.7/site-packages/openrouteservice/client.py", line 293, in wrapper
    result = func(*args, **kwargs)
  File "/srv/homeassistant/lib/python3.7/site-packages/openrouteservice/geocode.py", line 381, in pelias_reverse
    return client.request("/geocode/reverse", params, dry_run=dry_run)
  File "/srv/homeassistant/lib/python3.7/site-packages/openrouteservice/client.py", line 204, in request
    result = self._get_body(response)
  File "/srv/homeassistant/lib/python3.7/site-packages/openrouteservice/client.py", line 228, in _get_body
    body = response.json()
  File "/srv/homeassistant/lib/python3.7/site-packages/requests/models.py", line 898, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/local/lib/python3.7/json/__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "/usr/local/lib/python3.7/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/local/lib/python3.7/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Could you please enable debug logs for the component and send me the error logs?
These will contain the exact coordinates and request being made so feel free to send them to me via personal message.

Thank you!

I mainly shows the following with the coordinates of my phone which is home with the same coordinates for origin and destination, and my wife phone which is away with different coordinates for origin and destination, she has been at the same location for the past 6 hours.

2020-10-21 14:25:02 DEBUG (SyncWorker_59) [custom_components.open_route_service. sensor] Requesting route for origin: