Restful json_attributes help

Hiya - I’m looking for a bit of help with a rest sensor. I’ve configured it like this:

      - name: "REST Tv Input Source"
        json_attributes_path: "$.components.main['samsungvd.mediaInputSource'].inputSource"
        value_template: "{{value_json.components.main['samsungvd.mediaInputSource'].inputSource.value}}"
        json_attributes:
          - "timestamp"

The value pulls out correctly but the timestamp does not get put into an attribute. The log error I get is that JSON result was not a dictionary or list with 0th element a dictionary - but I can see that’s not the case when using jsonpath. The problem seems to be that the dictionary key has a . (period) in it, and this is breaking something on the json attributes path.

Another sensor against the same API, that works almost the same way, but just doesn’t have a dictionary key with a . works exactly as expected:

      - name: "REST Tv Channel Name"
        json_attributes_path: "$.components.main.tvChannel.tvChannelName"
        value_template: "{{value_json.components.main.tvChannel.tvChannelName.value}}"
        json_attributes:
          - "timestamp"

Am I missing something on how this should be written? Grateful of any help or guidance!

Show us the JSON.

Yep, sure:

{
   "components":{
      "main":{
         "samsungvd.mediaInputSource":{
            "supportedInputSourcesMap":{
               "value":[
                  {
                     "id":"dtv",
                     "name":"TV"
                  },
                  {
                     "id":"HDMI2",
                     "name":"PlayStation 5"
                  }
               ],
               "timestamp":"2023-09-20T17:03:19.696Z"
            },
            "inputSource":{
               "value":"dtv",
               "timestamp":"2024-05-14T21:10:47.874Z"
            }
         },
         "tvChannel":{
            "tvChannel":{
               "value":"",
               "timestamp":"2024-05-14T18:03:40.774Z"
            },
            "tvChannelName":{
               "value":"",
               "timestamp":"2024-05-14T19:28:09.669Z"
            }
         }
      }
   }
}

It’s a bit of a bodge workaround, but:

json_attributes_path: "$.components.main.*.inputSource"

Thanks! That does work - but annoyingly, there may well be another inputSource present under a different key under main - It’s just not in the sample one I provided.

Am I looking at a bit of a bug in how this works then?

OK, try this:

json_attributes_path: "$.components.main[?('samsungvd.mediaInputSource')].inputSource"

That works perfectly - thank you very much!

Just out of curiosity, any idea why my original didn’t work?

1 Like

No, it’ll be to do with the HA implementation of JSONPath. Your original works fine in the online tester.

Feel free to submit an issue on Github, or better still, fix it and submit a PR :slight_smile: .