Reverse Geocode Sensor ("Places") using OpenStreetMap - custom component

Hi Jim

Your update solved the issue. I am however now facing another error:

2018-05-28 23:56:09 ERROR (MainThread) [homeassistant.helpers.entity] Update for sensor.geocode_location fails
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/entity.py", line 204, in async_update_ha_state
    yield from self.async_device_update()
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/entity.py", line 327, in async_device_update
    yield from self.hass.async_add_job(self.update)
  File "/usr/local/lib/python3.6/concurrent/futures/thread.py", line 56, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/util/__init__.py", line 319, in wrapper
    result = method(*args, **kwargs)
  File "/home/homeassistant/.homeassistant/custom_components/sensor/places.py", line 432, in update
    self.do_update("Scan Interval")
  File "/home/homeassistant/.homeassistant/custom_components/sensor/places.py", line 676, in do_update
    user_display.append(targetoption)
NameError: name 'targetoption' is not defined

When I have a look at line 676 I see the following:

if option in locals():
   user_display.append(targetoption)

Whereas line 674 uses target_option. When I change line 676 this does however throw another error.

Do you have any idea what might cause this?

Not sure what’s been wrong with Forum notifications - but i just saw your message today. Are you still having a problem with this?

Hi Jim

Now I receive in 0.72 the following message:

Update for sensor.geocode_location fails
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/entity.py", line 197, in async_update_ha_state
    yield from self.async_device_update()
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/entity.py", line 320, in async_device_update
    yield from self.hass.async_add_job(self.update)
  File "/usr/local/lib/python3.6/concurrent/futures/thread.py", line 56, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/util/__init__.py", line 319, in wrapper
    result = method(*args, **kwargs)
  File "/home/homeassistant/.homeassistant/custom_components/sensor/places.py", line 432, in update
    self.do_update("Scan Interval")
  File "/home/homeassistant/.homeassistant/custom_components/sensor/places.py", line 462, in do_update
    distance_m = distance(float(new_latitude), float(new_longitude), float(home_latitude), float(home_longitude))
ValueError: could not convert string to float: 'None'

Do you have idea what might cause this?

I’m having the same issue. HASSIO 0.71

For some reason, I’m not getting form notifications when @geoffrey posts - but I got yours today @ravensteijn .

I’ve been working on developing an iOS app and haven’t been doing much with HomeAssistant for the last two months so I haven’t been very active on the forums. I am still running on 0.65.3 (upgrading now) and I don’t see the error in my log files. Did this error begin occurring in conjunction with a version upgrade?

I looked at the code in question and I would ask you to check the values you have for the following:

        new_latitude    = str(self._hass.states.get(self._devicetracker_id).attributes.get('latitude'))
        new_longitude   = str(self._hass.states.get(self._devicetracker_id).attributes.get('longitude'))
        home_latitude   = str(self._home_latitude)
        home_longitude  = str(self._home_longitude)

Specifically, my guess is that your devicetracker (for some reason) doesn’t have a latitude and/or longitude. I’ll look at my log files again after my version upgrade completes to see if I have the error you guys are seeing. If I do, then it might be something version specific that I’ll need to track down.

Until then - please let me know if you did in fact have values for your devicetracker entity latitude and longitude (did the attribute names change?) and/or if there might have been an error with the places component prior to this one.

I added it again to the configurator.yaml file and it’s working now, after a few minutes waiting.

i’m feeling a bit ashamed because i changed 1 thing. I made an account at www.openstreetmap.org
I think it’s mandatory to get this API call working? Am i correct?

Thanxs!

Not unless they’ve changed their policy since April…
Their API allowed completely anonymous access, but they requested that you include your email address in the APIKey field so that they would be able to contact you if they needed to - or to explain that they’ve had to throttle your requests because they were above some “non-published threshold” level.

I am tracking 3 family members, and for each one, I configured it to use the family members email address as the apikey value. I have never had requests rejected nor received any emails from openstreetmap.org

To be honest, now that google requires an account and form of payment for their service, I’m not sure why more people aren’t using my component simply to avoid having to register with Google. Google does provide a $200 credit each month which is enough to cover most people’s usage - but it still seems like a hassle to have to sign up for the apikey.

Glad it’s working for you now. Let me know if you experience any other problems.

2 Likes

Very nice component and a perfect alternative for the google_geocode component.

I noticed that the component uses UTC within the timestamp. Is it possible to change this to the local timezone?

I’m trying to integrate this into HA however the log indicates that cannot find the sensor. Here is what I have done:

Created a folder custom_components/sensors in the folder in which my configuration.yaml resides.

Place a copy of places.py within the custom_components/sensors folder

Edited configuration.yaml to include the following:

sensor_places_alan:
  name: alan
  devicetrackerid: device_tracker.iphone_
  options: zone, street, city
  display_zone: show
  map_provider: google
  map_zoom: 18
  home_zone: zone.alan_home
  api_key: !secret email_alan

What am I doing wrong? I’m on 0.77.0.

Thanks

Hi Texangeek,

I have all my sensors in a sensors.yaml file which I include from configuration.yaml with the following line:
sensor: !include sensors.yaml

Inside my sensors.yaml file, I have the following configuration for the “places” component:

  - platform: places
    name: jim
    devicetracker_id: device_tracker.jim_iphone8plus
    options: zone,place
    display_zone: show
    map_provider: google
    map_zoom: 19
    home_zone: zone.jim_home
    api_key: !secret email_jim

There is nothing in your configuration that says to use the “places” component. It looks like you’ve tried to name it twice - once as “sensor_places_alan” and again as just “alan”.

If you’re configuring your sensors within your configuration.yaml, try something like this:

Sample Configuration.yaml configurations:

sensor places_jim:
  - platform: places
    name: jim
    devicetracker_id: device_tracker.jim_iphone8
    options: zone,place
    display_zone: show
    map_provider: google
    map_zoom: 19
    home_zone: zone.jim_home
    api_key: !secret email_jim

sensor places_sharon:
  - platform: places
    name: sharon
    devicetracker_id: device_tracker.sharon_iphone7
    options: zone, place
    map_provider: apple
    map_zoom: 18
    home_zone: zone.sharon_home
    api_key: !secret email_sharon

Hi Nelbs,

Sorry for the delayed reply - I didn’t see your post until just today.

Which timestamp are you referring to? I’m in EST and all of the times I see appear to be local time. Is it possible that you didn’t correctly set the timezone on the computer you are using to run HomeAssistant?

Hey @tenly ever thought about adding places to the custom updater ? so that users can update via it ?

just ping @ludeeus if you want to add it :slight_smile:

1 Like

You were right, I did not set the timezone of my pi right. I changed it and now it works fine!
thanks

Thanks for your response.

I updated my configuration.yaml to reflect your suggested code but still receive the following error in the log:

ERROR (MainThread) [homeassistant.loader] Unable to find component sensor.places

Looking at your suggested code I note that you do not have the “- platform: places” line indented beneath “sensor:” Is this a typo? My configuration looks like:

sensor:
  - platform: places
    name: places_alan
    devicetrackerid: device_tracker.iphone_
    options: zone, street, city
    display_zone: show
    map_provider: google
    map_zoom: 18
    home_zone: zone.alastair_home
    api_key: !secret email_alan

Hi Texangeek,

Yes - sorry. There was a typo in what I posted in my last reply and the missing indent is required. I’ve edited my previous reply with examples that should be correct. The examples come from the GitHub documentation I posted when I uploaded the component.

There are also a couple of examples on how to configure automations.yaml that send notifications based on this sensor. Here’s a link to the GitHub page:
Github “Places” Project

Hi Toast,

Thanks for your message/suggestion. I’m not currently actively updating the “Places” component because it seems to be pretty stable and I’m putting all my free time into trying to learn Swift programming for the iPhone - but if/when an update is required, or need to add new features - I’ll definitely look into the custom_updater integration.

OK, so this what I now have I configuration.yaml:

Sensor places_alan:
  - platform: places
    name: Alan
    devicetrackerid: device_tracker.iphone_
    options: zone, street, city
    display_zone: show
    map_provider: google
    map_zoom: 18
    home_zone: zone.alan_home
    api_key: !secret email_alan

The sensor place_alan line is aligned with column zero of the file, i.e. it is not indented under the sensor component. Is this correct?

I apologize for all the queries - I’m new to HA and still trying to get my head around the YAML syntax. Thanks for you feedback and the great component.

Hi Texangeek,

Your config looks okay to me - except that the word “Sensor” should not be capitalized.

The only other thing that looks weird is the name of your device_tracker entity.
Does it really end with an underscore as the last character? (“iphone_”)

What error message are you actually getting?

Have you double-checked to make sure that the places.py file exists in the correct location?

  • In the same directory as your configuration.yaml file, there should be a directory named “custom_components”.
  • Underneath the “custom_components” directory, there should be a directory named “sensor”.
  • Inside the “sensor” directory, you should have the “places.py” file that you downloaded from my GitHub repository.

All of the directory names and the file name should be all lowercase characters.

If you’re still having trouble after double-checking all of those things, please send your log file.

Tenly

Thank you so much for your continued feedback and assistance.

Based on your last note I amended my config slightly. Changed ‘devicetrackerid’ to ‘devicetracker_id’ - stupid typo but easy to overlook! :grinning: Also removed the capitalization on Sensor. So now my configuration looks like:

sensor places_alan:
  - platform: places
    name: alan
    devicetracker_id: device_tracker.alan_iphone_
    options: zone, street, city
    display_zone: show
    map_provider: google
    map_zoom: 18
    home_zone: zone.alan_home
    api_key: !secret email_alan

Regarding the device tracker name - this is the name that is allocated within the Known_devices file. I think I previously tried amending this by changing the first line of the definition, i.e:

_iphone_:

to

alan_phone

but it failed to work. I’ll try again and see if I can get a more sensible device tracker name into the system.

So what I now see in the log file is (note I actually have two device trackers defined):

2018-09-03 13:51:21 INFO (MainThread) [homeassistant.loader] Loaded sensor.places from custom_components.sensor.places
2018-09-03 13:51:21 WARNING (MainThread) [homeassistant.loader] You are using a custom component for sensor.places which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you do experience issues with Home Assistant.
2018-09-03 13:51:26 INFO (MainThread) [homeassistant.components.sensor] Setting up sensor.places
2018-09-03 13:51:26 INFO (MainThread) [homeassistant.components.sensor] Setting up sensor.places
2018-09-03 13:51:26 ERROR (MainThread) [homeassistant.components.sensor] Error while setting up platform places
  File "/config/custom_components/sensor/places.py", line 312, in setup_platform
    add_devices([Places(hass, devicetracker_id, name, api_key, options, home_zone, map_provider, map_zoom)])
  File "/config/custom_components/sensor/places.py", line 330, in __init__
2018-09-03 13:51:26 ERROR (MainThread) [homeassistant.components.sensor] Error while setting up platform places
  File "/config/custom_components/sensor/places.py", line 312, in setup_platform
    add_devices([Places(hass, devicetracker_id, name, api_key, options, home_zone, map_provider, map_zoom)])
  File "/config/custom_components/sensor/places.py", line 330, in __init__

I upgraded my Home Assistant to the latest version to try and replicate your problem. I renamed one of my devices so the name ended with an underscore, I pointed it at a DeviceTracker entity that did not contains lat/long and even configured it to look for a non-existent DeviceTracker entity - but I can’t reproduce the error you are seeing.

Are you still having the problem?
What does your logging setup look like?