RESTful Sensor with json result as array values?

Trying to get some air pollution data using this sensor and json.
The documentation from the data provider https://api.nilu.no/docs/
Testing the url in https://jsonlint.com/ gets me this result:
[{
“zone”: “Øst og Sørlandet”,
“municipality”: “Lillehammer”,
“area”: “Lillehammer”,
“station”: “Bankplassen”,
“eoi”: “NO0074A”,
“component”: “PM10”,
“fromTime”: “2018-08-02T10:00:00+01:00”,
“toTime”: “2018-08-02T11:00:00+01:00”,
“value”: 14.85,
“unit”: “µg/m³”,
“index”: 1,
“color”: “6ee86e”,
“latitude”: 61.112880,
“longitude”: 10.464970
}, {
“zone”: “Øst og Sørlandet”,
“municipality”: “Lillehammer”,
“area”: “Lillehammer”,
“station”: “Bankplassen”,
“eoi”: “NO0074A”,
“component”: “PM2.5”,
“fromTime”: “2018-08-02T10:00:00+01:00”,
“toTime”: “2018-08-02T11:00:00+01:00”,
“value”: 6.70,
“unit”: “µg/m³”,
“index”: 1,
“color”: “6ee86e”,
“latitude”: 61.112880,
“longitude”: 10.464970
}, {
“zone”: “Øst og Sørlandet”,
“municipality”: “Lillehammer”,
“area”: “Lillehammer”,
“station”: “Bankplassen”,
“eoi”: “NO0074A”,
“component”: “NO2”,
“fromTime”: “2018-08-02T10:00:00+01:00”,
“toTime”: “2018-08-02T11:00:00+01:00”,
“value”: 7.5991443850,
“unit”: “µg/m³”,
“index”: 1,
“color”: “6ee86e”,
“latitude”: 61.112880,
“longitude”: 10.464970
}]

How do I set up my RESTful sensor, and how do I pick up the values to another sensor, the value for PM10 into sensor.pm10?

The rest sensor here not working:

  - platform: rest
    resource: https://api.nilu.no/aq/utd.json?areas=Lillehammer&stations=Bankplassen
    name: "API Nilu"
    value_template: "{{ value_json.status }}"
    json_attributes:
        - results

The top level of the json is an array, which is accessed by an index, such as

    value_template: "{{ value_json[0].status }}"

but to get the version you want, you have to rely on the order of the elements remaining the same. If it doesn’t, you need to check the other contents, and that is beyond my templating ability.

1 Like

Thanks!

But I cant get the data with my codeing.Think I have tried everything but the corect code. I have no clue what my “value_template” should look like and it does not work leaving it out?

You’ve got a few things working against you here. The first is the fact that the top-most result is an array, not a dictionary. I was just looking at the RESTful Sensor code yesterday with just this in mind. Because the result is an array, you can’t use the json_attributes parameter, which is the best way to get at pieces of the result.

The next issue is a state string is limited to 255 characters. So often you can’t just put the entire result in the sensor’s state. I didn’t count the characters in the above reply, but if it’s 255 or less, then you can just put the whole response in the sensor’s state, then you can use a template sensor and find the piece you want with the regex_findall_index filter.

If that won’t work (too many characters) you might want to grab just some of the result into the state. But, as @gpbenton points out, that depends on the order of the elements in the array. If the website always puts things in the same order, then you can do as was suggested.

With this website you might be better off 1) using a Command Line Sensor, which uses curl and jq (which you can install with sudo apt-get install jq.)

EDIT: I just did a quick test, and you could use jq the following way to extract the value of the “value” field for the array element whose “component” field is “PM10”:

curl ... | jq '.[] | select(.component == "PM10") | .value'
2 Likes

At the very least you need to remove:

json_attributes:
  - results

For the reason I mention in my other reply.

hey

i see you are working with the JQ command

can you help me with my first question here.? i think i need the same, just dont know how i need to put the JQ in my command line

command: (echo ‘{“cmd”:“listactions”}’; sleep 1) | nc XXXX 8000 | jq ‘.id .value1’

something like that i need? to get .id and .value1 from the data array