Get Value from JSON Array Rest Sensor

Hi. I’m trying to extract the value from some JSON at a URL. I am able to get some of the contents to display in Home Assistant, but the values from the array in the JSON (which is what I am really after) displays as unknown.

For instance at this URL:

https://waterservices.usgs.gov/nwis/iv/?sites=05514705&period=P7D&format=json

I want to retrieve the latest river gage level.

This path returns the correct value in this JSONTesterr:

.value.timeSeries[0].values[0].value[667].value

So I would think that {{ 'value_json.value.timeSeries[0].values[0].value[667].value' }} would return the same value, but nope. I have been working on this for a few days with no luck. I would really like to do this natively in Home Assistant without invoking BeautifulSoup if possible. Any suggestions?

Here is my REST sensor setup:

  - platform: rest
    name: quivre
    resource: https://waterservices.usgs.gov/nwis/iv/?sites=05514705&period=P7D&format=json
    value_template: '{{ value_json.value.timeSeries[0].values[0].value[667].value }}'
    verify_ssl: false

Your resource isn’t right. Try opening a cmd terminal and doing:

curl -X GET https://waterservices.usgs.gov/nwis/iv/?sites=05514705&period=P7D&format=json

You’ll see it returns some HTML.

The correct curl command to get that json is:

curl -X POST https://waterservices.usgs.gov/nwis/iv -d sites=05514705 -d period=P7D -d format=json

So setup your restful sensor as follows:

  - platform: rest
    name: quivre
    method: POST
    resource: https://waterservices.usgs.gov/nwis/iv/
    payload: 'sites=05514705&period=P7D&format=json'
    value_template: '{{ value_json.value.timeSeries[0].values[0].value[667].value }}'
    verify_ssl: false

EDIT: Fixed payload…still wrong. nvm

Now your value_json should contain real json.

1 Like

Did that work for you? Because it didn’t work for me:

Screenshot from 2020-02-05 19-31-15

Get the number of items in the json key and then access the last one. Try this which will be a bit slow I think:

  - platform: rest
    name: quivre
    resource: https://waterservices.usgs.gov/nwis/iv/?sites=05514705&period=P7D&format=json
    value_template: >-
        {% set j = value_json.value.timeSeries[0]['values'][0].value %}
        {% set n = (j | length) -1 %}
        {{j[n]['value']}}
    verify_ssl: false

Update : Just tried it and it works:

This is exactly what I was after. Thanks!

1 Like

If you want to get the last item in the list, this is the ‘pythonic’ way to do it:

  - platform: rest
    name: quivre
    resource: https://waterservices.usgs.gov/nwis/iv/?sites=05514705&period=P7D&format=json
    value_template: >-
      {{ (value_json.value.timeSeries[0]['values'][0].value)[-1].value }}
    verify_ssl: false

Here’s the answer to your original question. The reason why your template failed is due to how it referenced the values list.

If it’s referenced this way, using bracket notation, it works (it returns 17.71).

If it’s referenced this way, using dot notation, it fails (produces an error message).

In your case, the key called values is, as the error message indicates, the name of a built-in function. To differentiate it from the built-in function, don’t reference it using dot notation, use bracket notation.

Bracket notation can also handle keys containing spaces (such as ['time series'][0]). However, it is less compact than dot notation. Here is your template in full bracket notation:

value_json['value']['timeSeries'][0]['values'][0]['value'][667]['value']

and it works:


but it’s rather long so that’s why dot notation is popular (except when it causes you to ‘work for days’ to fix it). :wink:

Hope this helps avoid similar problems in the future. Good luck!

5 Likes

Geez. Cant believe it was so simple. Thanks for the explanation!

1 Like

I’m having a problrm trying to parse some json:
{
“range”: “HomeAssistant!A1”,
“majorDimension”: “ROWS”,
“values”: [
[
“£0.00”
]
]
}

I’m using the REST function to get this - and try to just get the value into a sensor:

Get value from Google Sheets

Developer Tools - when I don’t add the value_template, gves me the whole JSON structure - and using https://jsonpathfinder.com/

I get the value to be at: x.values[0][0]

so, in theory, this should work - but I get an undefined value (unknown in State). Can anyone help?

The word “values” might be a reserved word (meaning it is used for other internal purposes) and therefore becomes ambiguous for your application. If that’s the case then I suggest you use bracket notation, instead of dot notation, to clearly indicate you want to use that word to represent the name of a key in the JSON payload (and not its other internal meaning).

value_template: "{{value_json['values'][0][0]}}"

If that fails to fix the problem, then it’s not a reserved word and the cause is something else.

Wow, I really didn’t think about reserved words! that was it - thanks!