RESTful sensor json parsing issue

I’m having trouble with a RESTful sensor, which is complicated by the shitty form of the json data I’m fetching, but which unfortunately is out of my control. It’s a nearby weather station that I can fetch data from. The data looks like this:

I’m also a little confused by the fact here seems to be two types of RESTful sensor, and they seem very similar.

{
    "type": "FeatureCollection",
    "features": [
        {
            "geometry": {
                "coordinates": [
                    12.1841,
                    54.879
                ],
                "type": "Point"
            },
            "id": "0485f855-b702-c7e1-e1ef-061511480664",
            "type": "Feature",
            "properties": {
                "created": "2024-01-27T12:00:05.876679Z",
                "observed": "2024-01-27T12:00:00Z",
                "parameterId": "humidity_past1h",
                "stationId": "06147",
                "value": 92.0
            }
        },
        {
            "geometry": {
                "coordinates": [
                    12.1841,
                    54.879
                ],
                "type": "Point"
            },
            "id": "0a992993-dfcb-3308-fb52-15b55f612744",
            "type": "Feature",
            "properties": {
                "created": "2024-01-27T12:00:05.583618Z",
                "observed": "2024-01-27T12:00:00Z",
                "parameterId": "precip_dur_past1h",
                "stationId": "06147",
                "value": 0.0
            }
        },
        {
            "geometry": {
                "coordinates": [
                    12.1841,
                    54.879
                ],
                "type": "Point"
            },
            "id": "1a796ee7-f13a-0188-e906-411237204ffc",
            "type": "Feature",
            "properties": {
                "created": "2024-01-27T12:00:05.660264Z",
                "observed": "2024-01-27T12:00:00Z",
                "parameterId": "wind_max",
                "stationId": "06147",
                "value": 12.3
            }
        }
    ],
    "timeStamp": "2024-01-27T12:02:14Z",
    "numberReturned": 5
}

(additional “features” array objects removed to simplify)

I want to grab specific values for my sensors, but the problem is that the values are in an array of “features” where I can’t be sure of the order. I have tried to use jsonPath to lookup the correct values, and it seems to work fine on the jsonPath explorers online, but not in my HA instance.

My original attempt looked like this:

- scan_interval: 600
  resource: https://dmigw.govcloud.dk/v2/metObs/collections/observation/items?api-key=<api-key>&stationId=06147&limit=20&period=latest
  sensor:
    - name: "DMI Wind Max 2"
      json_attributes_path: "$.features[?(@.properties.parameterId=='wind_max')].properties"
      json_attributes:
        - value

Logs showed an error that the length of the state exceeded 255.
Then I tried an alternative approach, using a value_template instead, like this:

  sensor:
    - name: "DMI Wind Max"
      value_template: >-
        {% for feature in value_json.features %}
          {% if feature.properties.parameterId == 'wind_max' %}
            {{ feature.properties.value }}
          {% endif %}
        {% endfor %}

This seemed to work to start with, but started giving duplicate values for the sensor for some reason, i.e. the sensor value will contain both the latest value, and also a lot of white space and then the previous value, like it’s concatenating in some strange way. It also feels like a bit of a hack.

Any ideas what could be wrong with either of these approaches? I’d prefer to get the first option working, since it seems like the more correct way to handle it.

Just having a quick play in the template editor:

{{ (value_json.features|selectattr('properties.parameterId','eq','wind_max')|list|first)['properties']['value'] }}

Will return 12.3 and only 12.3

1 Like

Much more elegant, and more importantly, it seems to work perfectly, thanks! :slightly_smiling_face: :+1:

1 Like