REST sensor json_attribute confusion

I apologize if this is a FAQ or something - I’ve been poking around a bit, and haven’t nailed down an answer, though my poking has gotten me to a point where I can ask a coherent question. :slight_smile: I’m running HA 0.72.1, which I just updated to yesterday.

I’m adding a REST sensor, and the API I’m calling returns JSON - but it returns an array as a top-level object, and so I’m having trouble using json_attributes, which I think I need to do to achieve what I want, which is to make one call to the API and then retrieve all the different info from the returned value.

Here’s an example return from the API:

[
{
"macAddress": "2C:3A:E8:FF:FF:FF",
"lastData": {
"dateutc": 1530724440000,
"winddir": 194,
"windspeedmph": 2.5,
"windgustmph": 4.5,
"maxdailygust": 5.8,
"tempf": 63.1,
"hourlyrainin": 0,
"dailyrainin": 0,
"weeklyrainin": 0,
"monthlyrainin": 0,
"totalrainin": 14.68,
"baromrelin": 29.7,
"baromabsin": 29.86,
"humidity": 67,
"tempinf": 68.5,
"humidityin": 54,
"uv": 2,
"solarradiation": 200.75,
"feelsLike": 63.1,
"dewPoint": 51.96056810981581,
"lastRain": "2018-05-23T05:59:00.000Z",
"date": "2018-07-04T17:14:00.000Z"
},
"info": {
"name": "MyStation",
"location": "Backyard"
}
}
]

I think it returns an array because I could have multiple weather stations, and [0] would be the first, [1] the second, etc. What I’d like is to be able to get lastData into a json_attribute so I can parse that in other sensors. I’ve tried this:

  - platform: rest
    resource: https://api.ambientweather.net/v1/devices?applicationKey=KEY_HERE&apiKey=OTHER_KEY_HERE
    name: weather_station_report
    json_attributes:
      - [0]
    scan_interval: 300
    value_template: '{{ value_json[0].info.location }}'

… this doesn’t work; I get “JSON result was not a dictionary”. I’ve tried just

  - platform: rest
    resource: https://api.ambientweather.net/v1/devices?applicationKey=KEY_HERE&apiKey=OTHER_KEY_HERE
    name: weather_station_report
    json_attributes:
    scan_interval: 300
    value_template: '{{ value_json[0].info.location }}'

as well, but in this case, no attributes show up. :frowning:

Hm. I’ve been banging my head against this for a few days now and I’d like some pointers - once I get the JSON into an attribute (in whatever form), I feel like the rest of what I want to do (not detailed here) is straightforward. (Also, in case it’s not obvious, it doesn’t work to have the JSON in the state, because it’s more than 255 chars).

Thanks in advance for any help.

Replying to myself!
It looks like there’s at least one open issue that’s exactly my problem (#14478), as well as one which is related (#12235).

I also found someone else trying to do exactly the same thing I am. :slight_smile:

Now to figure out how to work around this issue. Sigh.

and here Help with REST that are contained within brackets

try https://gist.github.com/sti0/23f47d8aec03a0981f1c4aa2d6f9c258

That was useful, thanks! - jsonrest seems to be doing the trick as far as putting the json into attributes. Now I need to figure out why the template sensors I have pulling data out of that json don’t seem to be updating as frequently as the jsonrest sensor itself…

replying to myself again:

In order to get updates working properly, I had to implement force_update in jsonrest.py. I suspect without the force happening, it only ever updated when I restarted home assistant. :slight_smile:

@riz94107, can you share how you implemented force_update in jsonrest.py?