Help with REST that are contained within brackets

Trying to pull some json from ambientweather.net but the entire line is contained within square brackets and HA doesn’t like what I’m doing :slight_smile:

I created a simple copy of the json and removed the brackets, and everything processes fine. So, somehow I need to either remove the [ ] or change something in my configuration just not sure what.

Here’s what I get from ambientweather.net

[  
   {  
      "dateutc":1526125200000,
      "winddir":83,
      "windspeedmph":2.9,
      "windgustmph":4.5,
      "maxdailygust":12.5,
      "tempf":61.2,
      "hourlyrainin":0,
      "dailyrainin":0,
      "weeklyrainin":0,
      "monthlyrainin":0.05,
      "totalrainin":1.17,
      "baromrelin":29.74,
      "baromabsin":30.06,
      "humidity":84,
      "tempinf":73.2,
      "humidityin":54,
      "uv":2,
      "solarradiation":184.73,
      "feelsLike":61.2,
      "dewPoint":56.32332156578099,
      "lastRain":"2018-05-05T01:59:00.000Z",
      "date":"2018-05-12T11:40:00.000Z"
   }
]

Thanks!

Have you tried

[0].something

or

[0]['something']

I have, but the likelihood of me typing something incorrectly is very high.
What works on my version without brackets is (there’s a bunch more sensors, just showing wind direction for this example)

  - platform: rest
    name: weathertest
    json_attributes:
      - winddir
    resource: redacted
    value_template: '{{ value_json[0] }}'
  - platform: template
    sensors:
      winddir:
        friendly_name: 'Wind Direction'
        value_template: '{{ states.sensor.weathertest.attributes["winddir"] }}'

When I use the json with brackets, I get a bunch of errors. And the ‘sensor.weathertest’ also has no data

2018-05-12 13:07:51 WARNING (Thread-10) [homeassistant.components.sensor.rest] JSON result was not a dictionary
2018-05-12 13:07:51 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved

homeassistant.exceptions.InvalidStateError: Invalid state encountered for entity id: sensor.weathertest. State max length is 255 characters.
2018-05-12 13:08:08 WARNING (MainThread) [homeassistant.components.sensor.template] Could not render template Wind Direction, the state is unknown.

I did try to replace

value_template: '{{ states.sensor.weathertest.attributes["winddir"] }}'

with

value_template: '{{ states.sensor.weathertest.attributes[0].winddir }}'  (and)
value_template: '{{ states.sensor.weathertest.attributes[0]['winddir'] }}' 

but I had no luck.

I’m sure it’s a simple fix, but I just can’t seem to get it.

I never used that sensor, but according the warning message and the source code of that component, the json_attributes didn’t support JSON array as top-level. Therefore, states.sensor.weathertest.attributes is always None in your case.

Its workflow is first parse value, assign to attributes, then try to parse value_template, rewrite value. It looks buggy for me. The simple fix should be first parse value_template, then parse value, then assign attributes.

Would you please submit a issue?

That’s a no-no in python. You are trying to assign a string inside a string without using the proper punctuation. Notice how the editor is even messing it up by showing winddi as black where the rest is red?

    value_template: "{{ states.sensor.weathertest.attributes[0]['winddir'] }}"

or

    value_template: '{{ states.sensor.weathertest.attributes[0]["winddir"] }}'

Now on to your problem at hand… Have you tried

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

I did notice the errors after I posted, obviously not a programmer (it’s on my list of things to do)

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

results in a “expected a dictionary for dictionary value”

In talking with folks on the HA discord, it appears the issue is the json_attributes requires the original JSON object to be a dictionary.

{{ value_json[0][0] }} was a long shot, was just wondering if there was an extra layer that was being applied.

this is probably related to my efforts templating out the results of a rest sensor…

maybe we still need the trusted jsonrest Custom Component after all…where all this was possible.

I know this is over a year old, but here is how I got it working with json_attributes:

I’m using a command line sensor with curl and then I pipe everything to python and print the first item of the list (watch for the escape quotes in the command)

- platform: command_line
  command: 'curl -sb -H \"https://your_url.com\" | python -c \"import sys, json; print(json.dumps(json.load(sys.stdin)[0]))\"'

I know this is an old thread but I was working on something similar and thought I’d share: I just submitted a PR which implements the same json_attributes_template config option in rest that mqtt uses to let you manipulate a JSON REST response back into state attributes.

https://github.com/home-assistant/home-assistant/pull/24820

1 Like

@shbatm Is it possible to trial your PR changes as a custom component. I’ve been waiting for something like this. Thanks for taking the initiative.

@ronschaeffer You should be able to just copy all of the files from this folder: https://github.com/shbatm/home-assistant/tree/rest-json-attributes/homeassistant/components/rest into your config/custom_components/rest/ folder.