Help with rest platform - state_attr

I found a free reverse-geocoding service and obtained an API key. When I enter the api into my browser:

https://api.geoapify.com/v1/geocode/reverse?lat=44.3559783935546875&lon=-73.449300413753&format=json&apiKey=abcdefghijklmnopqrstuvwxya123456

(With my API), I get a lot of data in JSON format, like this:

{
    "results": [
        {
            "datasource": {
                "sourcename": "openstreetmap",
                "attribution": "© OpenStreetMap contributors",
                "license": "Open Database License",
                "url": "https://www.openstreetmap.org/copyright"
            },
            "country": "United States",
            "country_code": "us",
            "state": "Massachusetts",
            "county": "Middlesex County",
            "city": "Westford",
            "postcode": "01460",
            "street": "Main Street",
            "housenumber": "1",
            "lon": -72.44927424177823,
            "lat": 43.5598025,
            "state_code": "MA",
            "distance": 0,
            "result_type": "building",
            "formatted": "1 Main Street, Westford, MA 01460, United States of America",
            "address_line1": "1 Main Street",
            "address_line2": "Westford, MA 01460, United States of America",
            "category": "building",
            "timezone": {
                "name": "America/New_York",
                "offset_STD": "-05:00",
                "offset_STD_seconds": -18000,
                "offset_DST": "-04:00",
                "offset_DST_seconds": -14400,
                "abbreviation_STD": "EST",
                "abbreviation_DST": "EDT"
            },
            "plus_code": "87JCHH52+W7",
            "plus_code_short": "HH52+W7 Westford, Middlesex County, United States",
            "rank": {
                "importance": 0.00000999999999995449,
                "popularity": 3.1833793643476254
            },
            "place_id": "51dad7bfe8c0dc51c0590adcba9ba7474540f00102f901107dc10c00000000c00203",
            "bbox": {
                "lon1": -72.4493678,
                "lat1": 43.5596913,
                "lon2": -72.4491988,
                "lat2": 43.559874
            }
        }
    ],
    "query": {
        "lat": 43.559783935546875,
        "lon": -72.449300413753,
        "plus_code": "87JCHH52+W7"
    }
}

Not bad for free…

However, I tried to make a sensor with some of the data and I get nothing:

sensor:
  - platform: rest
    name: Geoapify Reverse Geocoding
    resource: https://api.geoapify.com/v1/geocode/reverse?lat=42.559783935546875&lon=-71.449300413753&format=json&apiKey=abcdefghijklmnopqrstuvwxya123456
    value_template: '{{ value_json.properties.city }} {{ value_json.properties.street }} {{ value_json.properties.housenumber }}'
    scan_interval:
      minutes: 10

Anyone see what I am doing wrong?

“.properties.” ???

try

{{ value_json.results.city }}

the results are an array, so for the first element that would be:

{{ value_json.results[0].city }}
1 Like

Thanks, that works. I am slowly learning JSON notation and this helps.

Right [0], I hadn’t seen it, you can see it from firefox

Continuing this project…

sensor:
  - platform: rest
    name: Geoapify Reverse Geocoding
    resource: https://api.geoapify.com/v1/geocode/reverse?lat=42.559783935546875&lon=-71.449300413753&format=json&apiKey=abcdefghijklmnopqrstuvwxya123456
    value_template: '{{ value_json.properties.city }} {{ value_json.properties.street }} {{ value_json.properties.housenumber }}'
    scan_interval:
      minutes: 10

This is working fine, but I want to insert the latitude and longitude data from another entity:

This doesn’t work. Any ideas what I did wrong inserting the lat-lon from the device_tracker entity?

- platform: rest
  name: Geoapify Reverse Geocoding
  resource: https://api.geoapify.com/v1/geocode/reverse?lat={{state_attr('device_tracker.iphone','latitude') }}&lon={{state_attr('device_tracker.iphone','longitude') }}&format=json&apiKey=abcdefghijklmnopqwertyetc
  value_template: '{{ value_json.results[0].housenumber }} {{ value_json.results[0].street }}, {{ value_json.results[0].city }}'
  scan_interval:
    minutes: 1