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).


I want to get, current.p2, current.p1 & 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
      - current

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.


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

  - platform: file
    name: AirVisualProTemp
    file_path: /config/misc_scripts/airvisualpro.json
    value_template: '{{ }}'
    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: '{{ }}'
    unit_of_measurement: '%'

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

import urllib.request
import json
url = ''
req = urllib.request.Request(url)

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

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

Will work for now I guess.