Nested JSON in REST sensor

Hi guys,

I’m a bit new here so I struggle with some basics.
Hopefully you can help me out.
I have a AirVisual Pro that I would like to integrate into HA.
It uploads all data to an API and can be queried.
However it’s rate limited which means I cannot setup multiple REST sensors to pull all the data.
I’ve tried to use json_attributes but it doesn’t seem to work on nested json.
The json looks like this (truncated as there are a lot of data).

{"settings":{"node_name":"home"},"current":{"ts":"2018-05-29T23:50:24.988Z","tp":23.4,"hm":54,"p2":4,"p1":4,"co":1092},"historical":{"instant":[{"ts":"2018-05-29T23:50:24.988Z","tp":23.4,"hm":54,"p2":4,"p1":4,"co":1092},{"ts":"2018-05-29T23:45:23.273Z","tp":23.4,"hm":54,"p2":5,"p1":60,"co":1091},{"ts":"2018-05-29T23:40:24.875Z","tp":23.4,"hm":54,"p2":4,"p1":15,"co":1086},{"ts":"2018-05-29T23:35:25.378Z","tp":23.4,"hm":54,"p2":4,"p1":37,"co":1079},{"ts":"2018-05-29T23:29:22.592Z","tp":23.4,"hm":54,"p2":2,"p1":2,"co":1070},{"ts":"2018-05-29T23:28:22.570Z","tp":23.3,"hm":54,"p2":2,"p1":2,"co":1071},{"ts":"2018-05-29T23:27:22.567Z","tp":23.3,"hm":55,"p2":2,"p1":13,"co":1068},{"ts":"2018-05-29T23:26:22.567Z","tp":23.3,"hm":55,"p2":2,"p1":13,"co":1067},{"ts":"2018-05-29T23:25:22.565Z","tp":23.3,"hm":53,"p2":2,"p1":23,"co":1064},{"ts":"2018-05-29T23:24:22.574Z","tp":23.2,"hm":53,"p2":3,"p1":3,"co":1061},{"ts":"2018-05-29T23:23:22.563Z","tp":23.2,"hm":55,"p2":3,"p1":13,"co":1059},{"ts":"2018-05-29T23:22:22.551Z","tp":23.2,"hm":55,"p2":2,"p1":2,"co":1059},{"ts":"2018-05-29T23:21:23.402Z","tp":23.1,"hm":55,"p2":2,"p1":2,"co":1059}

I want to get current.hm, current.p2, current.p1 & current.co in one query as attributes to the sensor.
Can anyone give me some pointers?

I had to give up on my original idea of utilizing the REST sensor for this.

According to this conversation removing the value_template and just use json_attributes to populate the attributes should work.
Like this.

  - platform: rest
    name: airvisualproaqiold
    friendly_name: "Indoor AQI"
    unit_of_measurement: 'AQI'
    scan_interval: 300
    json_resources:
      - current
    resource: https://www.airvisual.com/api/v2/node/uniquekey

Normally this is supposed to work as I understand it but since the response is larger than the REST sensor accepts (HA perhaps) it’s a catch 22 moment for me.

homeassistant.exceptions.InvalidStateError: Invalid state encountered for entity id: sensor.airvisualproaqiold. State max length is 255 characters.

I have toyed around a lot trying to circumvent this but without luck so far.
So what I ended up doing was a combination of a small python script, command and file sensors.
It’s not very elegant but fixes my problem.

configuration.yaml

  - platform: command_line
    name: AirVisualProAQI
    scan_interval: 300
    unit_of_measurement: 'AQI'
    command: "python3 /config/misc_scripts/airvisualpro.py"

  - platform: file
    name: AirVisualProTemp
    file_path: /config/misc_scripts/airvisualpro.json
    value_template: '{{ value_json.tp }}'
    unit_of_measurement: '°C'
    
  - platform: file
    name: AirVisualProPM25
    file_path: /config/misc_scripts/airvisualpro.json
    value_template: '{{ value_json.p2 }}'
    unit_of_measurement: 'μg/m3'

  - platform: file
    name: AirVisualProHumidity
    file_path: /config/misc_scripts/airvisualpro.json
    value_template: '{{ value_json.hm }}'
    unit_of_measurement: '%'

  - platform: file
    name: AirVisualProCO2
    file_path: /config/misc_scripts/airvisualpro.json
    value_template: '{{ value_json.co }}'
    unit_of_measurement: 'ppm'

airvisualpro.py

import urllib.request
import json
url = 'https://www.airvisual.com/api/v2/node/uniquekey'
req = urllib.request.Request(url)

##parsing response
r = urllib.request.urlopen(req).read()
response = json.loads(r.decode('utf-8'))
print(response['current']['p1'])

with open('airvisualpro.json', 'w') as outfile:
  json.dump(response['current'], outfile)

Will work for now I guess.