How to mitigate Rest resource that keeps changing its path

using resource https://data.buienradar.nl/2.0/feed/json, I am looking for the station Woensdrecht, and find it at 49:

rest:

  - resource: https://data.buienradar.nl/2.0/feed/json
    scan_interval: 86400
    sensor:
      - unique_id: rest_sensor_buienradar_woensdrecht
        name: Buienradar Woensdrecht
        value_template: >
          {{value_json.actual.stationmeasurements.49.regio}}
        json_attributes_path: '$.actual.stationmeasurements.49'
        json_attributes:
          - timestamp
          - weatherdescription
          - etc

however, the station number changes almost daily, so my sensors go un available, or show data for another station altogether…
fwiw, this is the full json for the station:

{"$id":"53","stationid":6340,
 "stationname":"Meetstation Woensdrecht",
 "lat":51.45,"lon":4.33,
 "regio":"Woensdrecht","timestamp":"2025-02-18T07:30:00",
 "weatherdescription":"Zwaar bewolkt",
 "iconurl":"https://www.buienradar.nl/resources/images/icons/weather/30x30/cc.png",
 "fullIconUrl":"https://www.buienradar.nl/resources/images/icons/weather/96x96/CC.png",
 "graphUrl":"https://www.buienradar.nl/nederland/weerbericht/weergrafieken/cc",
 "winddirection":"O","airpressure":1024.7,"temperature":-5.0,
 "groundtemperature":-6.7,"feeltemperature":-9.5,
 "visibility":12300.0,"windgusts":3.8,
 "windspeed":3.0,"windspeedBft":2,
 "humidity":86.0,"precipitation":0.0,
 "rainFallLast24Hour":0.0,"rainFallLastHour":0.0,
 "winddirectiondegrees":80}

that was yesterday, check today, it’s back to 38 …

Cant I somehow search for the correct stationid/stationname in that json attributes path and be immune to that frequent change, and find the correct endpoint automatically?

Have you at all looked at the documentation RESTful Sensor documentation? It links to an explanation of how to write a JSONPath, though I have used neither before it can’t have taken me more than two minutes to figure out that the json_attributes_path probably should be something like this:

$.actual.stationmeasurements[?(@.stationid == 6340)]

Edited to remove trailing [0] which somehow wasn’t correct.

Tbh, I didnt see that no. thanks for the pointer.

However, it does not yet work as hoped for (I’ve moved the value_template for the time being to now(), as that requires even more syntax checking)

      - unique_id: rest_sensor_buienradar_woensdrecht
        name: Buienradar Woensdrecht
        value_template: >
          {{now()}}
#           {{value_json.actual.stationmeasurements[?(@.stationid == 6340)].regio}}
        json_attributes_path: '$.actual.stationmeasurements[?(@.stationid == 6340)][0]'
        json_attributes:
          - timestamp
          - weatherdescription
          - winddirection
          - $id
          - stationid
          - stationname
          - winddirection

results in the following:

so the path is not set correctly, and none of the attributes is listed because of that.
nothing in the logs.

edit
this works though:

        json_attributes_path: '$.actual.stationmeasurements[?(@.stationid == 6340)]'
        json_attributes:
          - timestamp
          - weatherdescription
          - winddirection

sweet!
now get that station name back in the value_template, because this is returned:

Invalid config for 'rest' at packages/package_weer.yaml, line 993: invalid template (TemplateSyntaxError: unexpected char '?' at 40) for dictionary value 'rest->8->sensor->8->value_template', got '{{value_json.actual.stationmeasurements[?(@.stationid == 6340)].regio}}\n', please check the docs at https://www.home-assistant.io/integrations/rest

using the same string

        value_template: >
          {{value_json.actual.stationmeasurements[?(@.stationid == 6340)].regio}}

Well as I said I’ve never used JSONPath before. Using an online evaluator just now I came to the same conclusion, but I really don’t understand why. As I expected from the documentation, the result is displayed as a list, so it makes no sense why one wouldn’t also need to actually get the first item of this list with a [0] or .0 at the end… Ohh well, at least it works!

1 Like

yes, at least for the most part of it.

I cant seem to find the correct syntax using that same path in the value_template however.
Somehow it doesnt like the ? but I can not escape it correctly

The value_template is not a JSONPath, it is a Jinja template. Entirely different language/syntax. Should be something like this:

(value_json.actual.stationmeasurements | selectattr('stationid', '==', 6340) | first)['regio']
1 Like

of course… I kept thinking the wrong way…

result is great now:
thank you so much