Restful sensor - json attributes path / json attributes

I need some help in understanding what I am missing in a rest api call please. I finally thought I had a fairly good grasp of API:s and decided to make use of a publicly available api. That being said, I am struggling with the generated sensors which appear to be empty/unavailable. I have tried with two differerent approaches as visible in my configuration.yaml below.

rest:
  - scan_interval: 3600
    resource: https://service.karlskrona.se/FileStorageArea/Documents/bad/swimAreas.json
    sensor:
      - name: "Badtemp-Salto"
        value_template: "{{ value_json[0].Payload.swimAreas[0].temperatureWater }}"
        json_attributes_path: "$.Payload.swimAreas[?(@.nameArea=='SALTĂ–')].temperatureWater"
        json_attributes:
          - "temperatureWater"
      - name: "Badtemp-Tallholm"
        value_template: "{{ value_json[0].Payload.swimAreas[6].temperatureWater }}"
        device_class: temperature
        unit_of_measurement: "°C"</sup>

Any hints would be very appreciated here!

your file is not a valid json

1 Like

Assuming the parser can read through the potential Byte Order Mark suggested above, your problem is the first [0] in your templates. Remove them — should start value_json.Payload.

also on swimAreas[0] I think

Thanks for the quick reply! That’s strange. I don’t get an error like you do. At least not for the first rows. I did however saw some issues (null values?) further down the API code:

I will investigate further, thanks again!

use nodered and javascript for debugging

Thanks, I think I did try this in one of many earlier attempts without any luck. The usage of [number] is when I want to reference a particular row (?) in a table right? At least that was how I interpretated it from my reading today :slight_smile:

Nevertheless, I will give your suggestion a go for sure, thanks again!

Out of curiosity, is it sufficient to only reload all YAML files or do I need to restart HA every time i make adjustments to sensors like these? Would be great if I didn’t have to restart all the time. Though, I’m pretty sure I’ve had a few cases in HA where I had to actually restart HA in order to get a few sensors up and running…

for the rest you have to restart, but with node red you can see in real time and test everything.

you should edit the “debug” node and try to see if it finds the value.
swimAreas[0] first block, swimAreas[1] second block…

you can see example AqaraPOST-Homeassistant/Aqara_G3_without_nodered.txt at 6bdb029e3d9b6499da3c754d7e7e4fb8d20ffbb7 · sdavides/AqaraPOST-Homeassistant · GitHub

You’re not going to get far guessing like that. [0] is indeed for accessing the first element in a list, but that JSON isn’t a list at the top level.

The problem is indeed with the response. I set up the same sensors and get this in the log:

REST result could not be parsed as JSON

This validator also cannot read it as JSON:

https://jsonformatter.curiousconcept.com/#

1 Like

This is currently beyond my level of understanding I’m afraid. Node-RED appear to be very useful but still something I haven’t had the pleasure to dig into yet.

Does it matter or, perhaps I should say is it possible to exclude the first part in the API? I’m thinking of the part you have in this printscreen (providerPayload). I was hoping to skip that part and move straight to the “Payload” part underneath?

That being said, if the API that I’m connecting to is broken then it’s quite unfortunate as it’s out of my control then. Fortunately I have another API for flights that I am planning to make use of but that was a bit more complex with credentials etc. TBC :slight_smile:

I really appreciate you both took your time here to support. I’ve learned a lot and will continue to learn thanks to the awesome community members of HA such as yourselves. Many thanks!

node red is very simple, you just need to get started.
swim Areas[0].timeStamp first timestamp value
swimAreas[1].timeStamp second timestamp value.

First of all though you must have a valid json, �� they are not valid

Couldn’t let this one go :sunglasses:

command_line:
  - sensor:
      command: "curl -s --output - https://service.karlskrona.se/FileStorageArea/Documents/bad/swimAreas.json | iconv -f UTF-16 -t UTF-8 | sed 's/\r//'"
      name: Salto test
      scan_interval: 86400
      value_template: "{{ now() }}"
      json_attributes:
        - providerPayload
        - Payload

Then use template sensors to pull out what you need from the attributes.

The JSON being published wasn’t invalid, but it was UTF-16 encoded.

@velodromen — if this is your first command_line sensor, you’ll need to do a full restart after adding the above code to your configuration.yaml.

1 Like

Wow, many thanks for looking into this one Troon! :+1:I completely missed your reply above since I thought my issue was more or less a dead end. Instead I found a different API serving the temperatures I was looking for and this time I got it to work after some additional reading.

1 Like