REST template and json_attributes_path

Hey, I’, trying to parse some XML from a REST sensor and I’m stuck on the json_attributes_path.

My current sensor looks like:

rest:
  - resource: https://api.met.no/weatherapi/textforecast/2.0/?forecast=landoverview
    sensor:
      - name: met_test
        unique_id: met_test
        value_template: "MET test"
        json_attributes_path: '$.textforecast.time[0].forecasttype.location[?(@["\x40id"]=="0503_0608")]'
        json_attributes:
          - '@name'
          - '#text'

And a snippet of the XML converted to JSON (from log) is as follows:

{
  "textforecast": {
    "@xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
    "@xsi:noNamespaceSchemaLocation": "https://schema.api.met.no/schemas/textforecast-0.3.xsd",
    "meta": {
      "@licenseurl": "https://www.met.no/en/free-meteorological-data/Licensing-and-crediting"
    },
    "time": [
      {
        "@from": "2023-01-21T00:00:00",
        "@to": "2023-01-22T00:00:00",
        "forecasttype": {
          "@name": "normal",
          "location": [
            {
              "@name": "Agder, Østlandet",
              "@id": "0503_0608_0609_0610",
              "#text": "Nordaust eller skiftende bris. Lokal frostrøyk rundt åpent vann, ellers pent vær."
            },

As you can see the attribute contains a ‘@’ which results in adressing it as @["\x40id"] in my working example above.

But, since the value of @id varies and I want all entries containing 0503 (my location) I did a test in pure python with jsonpath to simplify debugging. This does a pythonish filter test and works nicely.

import json
from jsonpath import jsonpath

with open('met.json') as fp:
   json_string = fp.read()
json_data = json.loads(json_string)

jsonpath_expression = '$.textforecast.time[*].forecasttype.location[?("0503" in @["\\x40id"])]'

result = jsonpath(json_data, jsonpath_expression, debug=True)
print(json.dumps(result, indent=3))

Resulting in:

[
   {
      "@name": "Agder, \u00d8stlandet",
      "@id": "0503_0608_0609_0610",
      "#text": "Nordaust eller skiftende bris. Lokal frostr\u00f8yk rundt \u00e5pent vann, ellers pent v\u00e6r."
   },
   {
      "@name": "\u00d8stlandet",
      "@id": "0503_0608",
      "#text": "Skiftande bris, nordaust bris p\u00e5 kysten. Stort sett pent ver. Kan hende lokal t\u00e5ke rundt Oslofjorden."
   },
   {
      "@name": "\u00d8stlandet",
      "@id": "0503_0608",
      "#text": "Skiftende bris. Delvis skyet oppholdsv\u00e6r. Fra om ettermiddagen s\u00f8rlig bris, p\u00e5 kysten s\u00f8rvestlig liten kuling. Tilskyende, etter hvert sn\u00f8."
   }
]

Now, the big question is how to put this into the YAML? I’ve tried the so many variants with no luck, the sample below is what I would expect would work. I guess, as I’ve studied the sensor python code, that this is an escaping thing but I can’t seem to get to the bottom of it. Any ideas?

rest:
  - resource: https://api.met.no/weatherapi/textforecast/2.0/?forecast=landoverview
    sensor:
      - name: met_test
        unique_id: met_test
        value_template: "MET test"
        json_attributes_path:  >
          $.textforecast.time[0].forecasttype.location[?('0503' in @['\x40id')]
        json_attributes:
          - '@name'
          - '#text'

I’ve really need a pair of fresh eyes on it :slight_smile:

Best regards

And writing this got me on the right track, so for documentation, my YAML was mostly correct - in my sleep deprieved state I got some parentesis matching wrong.

The working sensor with the closing ([])put correctly:

rest:
  - resource: https://api.met.no/weatherapi/textforecast/2.0/?forecast=landoverview
    sensor:
      - name: met_test
        unique_id: met_test
        value_template: "MET test"
        json_attributes_path:  '$.textforecast.time[0].forecasttype.location[?("0503" in @["\x40id"])]'
        json_attributes:                                                                                                      
          - '@id'
          - '@name'
          - '#text'