Device tracker Show city

Hi ! I’m trying to convert my latitude/longitude position for my devices tracker into city. so I would like to know in witch city my device is.

is it possible ?

I saw that : https://github.com/gregoryduckworth/GoogleGeocode-HASS

Do I need to pay for a google api ?

Thanks

Have a look at Nominatim from OpenStreetMap.

https://nominatim.org/

Use the lat/lan from a device tracker to create a REST sensor.

Assuming your device tracker is named device_tracker.myphone you can use this code:

sensor
  - platform: rest
    name: My Phone City
    resource_template: >-
      {%- set lat = state_attr('device_tracker.myphone', 'latitude') -%}
      {%- set lon = state_attr('device_tracker.myphone', 'longitude') -%}
      https://nominatim.openstreetmap.org/reverse?lat={{lat}}&lon={{lon}}&format=json
    value_template: '{{ value_json.address.city }}'

This will periodically (SCAN_INTERVAL) update the sensor. You need a more elaborate solution (template sensor or automation) to update the city for every state update of the device tracker. Depending on your needs.

2 Likes

For those in need reading this thread, you could also (better) use an out of the box solution: Places

I’ve tried this and it never returns a city name. City is not even listed in attributes

Have you tried the template in the developer tools?

You could also try to re-create a url and open in in the browser like so:

https://nominatim.openstreetmap.org/reverse?lat=51.9&lon=4.6&format=json

Please show your results (mind your privacy).