Google Geocode Custom Component - GPS to Street Address

This may need to be states.sensor.G_Aidan.attributes.formatted_address, tho you shouldn’t have caps in entity id’s :stuck_out_tongue:

1 Like

Thank you - I’ll get rid of the capitalization. I was just trying to get all of my geocode sensor badges to display next to each other. Is there some trick to make that happen without naming them with a common prefix?

Of course it needs “states” in front of it. Stupid mistake. Would that cause the automation to not trigger though?

You didn’t say it didn’t trigger you said it didn’t work that’s not the same thing, in my eyes at least, I assumed the action didn’t work after you added something. Didn’t look at the trigger :stuck_out_tongue: If you trigger it manually after editing for errors, does it work (click the automation then click the trigger)? As for the trigger i can’t see anything wrong with it. Well if the action is up the spout it might cause it not to trigger I suppose but more likely it would cause an error in the logs saying it’s in valid or some such and it may trigger but nothing would happen.

So, actually - I didn’t say that “it didn’t work” - I said the “automations are not firing”.
In my part of the world, that would have been interpreted as “not being triggered” - but I suppose it could be ambiguous in other circles. You’re right that I could have been more clear. I do appreciate your help and certainly don’t want to argue about anything!

I’m not sure if fixing the errors we’ve already spotted have restored the triggering functionality. They have never worked when I try to trigger them through the HA interface - and I assumed that was because the trigger object did not exist with the “from_state” and “to_state” values that I was referencing - but they did always work when people were on the move. I’ll be able to test this better tomorrow when people are actually moving around.

But for now - I’ve found the problem ( but not the solution ) for my new addition to the text message. I did some testing using a notification that displays a message to my AndroidTV to try and find a syntax that worked and I’m having a problem coding it because the “Formatted Address” attribute contains capital letters and a space. None of the other attributes contain a space. They are either a single word - or they are separated by underscores.

If I use “message”: “{{ states.sensor.aidan.attributes.friendly_name }}” in my notification, it correctly displays the friendly_name attribute data. But I tried replacing “friendly_name” with “formatted_address”, “Formatted_Address”, “Formatted Address” and “[Formatted Address]” but I can’t find a syntax that works.

Can anyone help me out with the correct syntax for getting the value of Formatted Address? It’s curious to me that all of the other multi-word attribute names contain underscores instead of spaces - but not this one.

{{ states.sensor.keith.attributes["Formatted Address"] }}

The above works for me in the template menu so should work for you in the message template. You are correct to say that manually triggering it won’t work due to trigger.to_states. Why this component would break your phone trigger state is a mystery to me :frowning:

Thank you for your help. They syntax you provided works properly for referencing the attribute with the space in its name. My automations are now firing/triggering properly and I’m receiving the data I want in the text message.

The only thing missing now is a reverse geocode that will give me a place name.

It looks like it would only be a small additional task to extend this component to optionally call this URL as well as/instead of the google URL - and merge the below attributes into the state object…

But since I don’t know exactly how to do it myself - I’ll assume that its more difficult than I think and that the author has already considered it and decided against it for now. Still - if anyone can point me to any documentation that I could refer to in Home Assistant that would instruct me on how to call the below URL from a script, and then parse the results and put them in variables that I could use in notification messages - I’d greatly appreciate it.

Thanks again @keithh666 for your earlier help.

https://nominatim.openstreetmap.org/reverse?format=xml&lat=43.638164114&lon=-79.386831786&addressdetails=1

…which returns nicely delimited output like this…

<reversegeocode timestamp="Fri, 26 Jan 18 07:38:53 +0000" attribution="Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright" querystring="format=xml&lat=43.638164114&lon=-79.386831786&addressdetails=1">
  <result place_id="61887051" osm_type="node" osm_id="5069378525" ref="Queen's Quay Disabled Sailing Program" lat="43.6381056" lon="-79.3865986" boundingbox="43.6380056,43.6382056,-79.3866986,-79.3864986">
    Queen's Quay Disabled Sailing Program, 283, Queens Quay West, Harbourfront, Old Toronto, Toronto, Ontario, M5J 1B5, Canada
    </result>
      <addressparts>
        <attraction>Queen's Quay Disabled Sailing Program</attraction>
        <house_number>283</house_number>
        <road>Queens Quay West</road>
        <neighbourhood>Harbourfront</neighbourhood>
        <city_district>Old Toronto</city_district>
        <city>Toronto</city>
        <state>Ontario</state>
        <postcode>M5J 1B5</postcode>
        <country>Canada</country>
        <country_code>ca</country_code>
      </addressparts>
    </reversegeocode>

Hmm I don’t know of any way to get at the data in HA but maybe something could be done in python using this library…

http://docs.python-guide.org/en/latest/scenarios/xml/

Hi @keithh666,

That nominatim url I posted has a “format=xml” in it that can be changed to “format=json” - so I didn’t need the link to xml parsing library you provided - but thanks anyways.

I actually took the original code from the google_geocode.py and added an additional call to openstreetmap to retrieve the place_type and place_name as additional attributes for the sensor. This makes my notifications infinitely more useful (to me) when someone is away from home. Prior to this, I was sending the address - but most of the time - the address meant nothing to me. Now I get a message that says they are at Walmart or Red Lobster or something else which is often much more informative than the street address alone.

I uploaded my modified file to the comment thread on the author’s GitHub page (linked higher up in this thread). I’m not a developer and can’t guarantee that I did everything properly in my mods - but it works for me - and until @michaelmcarthur is able to get some time away from the new baby and include “place names” in his “official” custom component - my mods may also be useful to others.

Once more, I have to thank the original author (@michaelmcarthur) for writing this code in such a clear, organized and useful manner. I learned quite a lot about custom component design and Python simply by trying to extend his code - and thanks again of course to @keithh666 for his help in this thread.

2 Likes

For everyone that wanted a place name I merged openstreetmap data with google geocode. This file in the dev branch has all the features of the google version but it uses openstreetmap for the data. This means if you put places as an option it will display the place name. Give it a go if you want a place name. There might be a few bugs but from my initial testing it seams ok. you can add the file to your custom components folder and add this to your sensor.yaml.

- platform: openstreetmap_geocode
  name: Michael
  origin: device_tracker.mobile_phone
  options: place

@acdown87 @tenly @keithh666

4 Likes

@michaelmcarthur do we install the file above in the same place as the Google_geocode.py file? i.e. custom components -> sensor?

Yeah same place.

1 Like

@michaelmcarthur its working fine for me thank you.

On a different note I implemented the template sensor you gave and the results are very patchy in the sense that it does not select the last changed sensor.

Could we use binary sensor instead? as template sensors have a performance impact I suppose? or do you have any other idea on how I could tackle this?

Thanks.

@michaelmcarthur if i want to pay for more requests of api, what is the price? did you known? and where is the correct link for i see it.
Thanks

Scan Interval……

I see in the thread above that other people have noticed that the sensor is not always in sync with the device_tracker co-ordinates and asked about how to increase the scan interval (which has been provided)…

But I have an automation that triggers off of a Device_Tracker location change. As an action, it sends me a text message using data from the matching sensor - which hasn’t been updated yet. Is there any way at all to force the sensor to update “on demand” instead of (or as well as) on a specific interval?

Ideally - when my automation fires off of the change to the Device_Tracker entity - I’d like the first action it takes to be to call “sensor.update()” (or whatever the function is to cause the sensor to update) for my reverse geocode sensor - and then after the sensor has been updated - the automation can proceed to send me the notification with the correct address information.

I expect that the advice I receive will be to trigger my automation off of the sensor change instead of device_tracker - but I’d rather not do that as that would introduce a delay - and also, periodically, I receive ‘name resolution’ errors that prevent the successful reverse lookup.

I have to believe that a system as powerful as Home Assistant is contains some mechanism for initiating a sensor update from something other than a recurring schedule? But how?

Hi,
Can we get the data from google in different languages? i would like to get it in hebrew

thx

i can find this:

https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&language=iw&region=IL">

i got it to work in hebrew…
in the google_geocode.py file
you need to change:
url = “https://maps.googleapis.com/maps/api/geocode/json?latlng=” + lat + “&key=” + self._api_key

to

url = “https://maps.googleapis.com/maps/api/geocode/json?latlng=” + lat + “&key=” + self._api_key + “&language=iw&region=IL”

And you will get the address in Hebrew.
you can select whice language and region by changing the parmeters

2 Likes

I used your component and it worked like a charm.

1 Like

Component set up for two devices. It will update one or the other but not both, one will always be awaiting update, no errors showing.

Using Owntracks http on both phones and both reporting correctly to hass.io running on a rpi.

Any suggestions appreciated?

Edit: seems to be updating the two now so will keep an eye on it.

noobie question, but do you still put the the google api key or do you have to get an openstreetmap api to use this feature?

There is no need to use the api field as it’s not implemented in the openstreetmap version.