JSON parsing ... again

I’ve read a lot about parsing JSON here in the forums, but I am not clever enough to use it when it comes to a JSON that is returned from my solar inverter (it’s a SMA one and I know that there is a integration for it, but it does not help in my case because of (a) having two of them (which the integration cannot handle) and (b) not all values can be accessed using the integration, so I am trying the JSON thingy).

So this is the JSON object that is returned:

{"result":{"012A-B1234C12":{"6800_08822000":{"1":[{"validVals":[9366,9344,9345,9346],"val":[{"tag":9346}]}]},"6800_10821E00":{"1":[{"val":"STP6.0-3AV-40 364"}]},"6800_08811F00":{"1":[{"validVals":[1129,1130],"val":[{"tag":1129}]}]},"6180_08214800":{"1":[{"val":[{"tag":307}]}]},"6180_08414900":{"1":[{"val":[{"tag":886}]}]},"6180_08522F00":{"1":[{"val":[{"tag":308}]}]},"6800_088A2900":{"1":[{"validVals":[9327,9375,9376,9437],"val":[{"tag":9327}]}]},"6100_40463600":{"1":[{"val":3240}]},"6100_40463700":{"1":[{"val":0}]},"6100_40263F00":{"1":[{"val":3566}]},"6400_00260100":{"1":[{"val":8305376}]},"6800_00832A00":{"1":[{"low":6000,"high":6000,"val":6000}]},"6800_008AA200":{"1":[{"low":0,"high":null,"val":3004908683}]},"6400_00462500":{"1":[{"val":1277112}]},"6100_00418000":{"1":[{"val":null}]}}}}

Great. It’s even valid according to https://jsonlint.com/.

Now up to the next tool: https://jsonpath.curiousconcept.com/
I need to get the following values out of the JSON:

  • $.result[‘012A-B1234C12’][‘6100_40263F00’][‘1’][‘0’][‘val’]
  • $.result[‘012A-B1234C12’][‘6100_40463700’][‘1’][‘0’][‘val’]
  • $.result[‘012A-B1234C12’][‘6100_40463600’][‘1’][‘0’][‘val’]

This works perfectly fine, but I have no idea how to use all that magic in https://www.home-assistant.io/integrations/rest/ (value_template, json_attributes, json_attributes_path etc).

Using no templating at all results in:

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

Thanks in advance,
Torsten

I took some time but I was able to solve it myself :nerd_face:

This helped me: Rest sensor: State max length is 255 charactersusing a dummy sensor value and extracting the useful values via json attributes

So my sensor looks like this:

- platform: rest
  name: SMA SunnyBoy Storage
  resource: https://xx.xx.xx.xx/dyn/getDashValues.json
  value_template: 'SMA_SBS' 
  scan_interval: 35
  verify_ssl: false
  json_attributes:
    - result

and one of the corresponding template sensors to extract the data I need:

    sma_batt_level:
      friendly_name: 'SMA Batterie Level'
      value_template: "{{ states.sensor.sma_sunnyboy_storage.attributes['result']['012A-B1234C12']['6100_40263F00']['1'].0.val }}"

Cheers,
Tom

1 Like

Small addon to this topic that had me waste time and can prevent frustration for others. :slightly_smiling_face:

My json result from the rest call did not have a single top level “result” in the output. I had trimmed down the api call because the result was a huge output. So I ended up with multiple smaller top levels. In this case it is important that each top level is represented in the json_attributes list:

  json_attributes:
    - result_1
    - result_2
    - result_3

Then the value_template will look like this:

value_template: "{{ states.sensor.sma_sunnyboy_storage.attributes['result_1']
value_template: "{{ states.sensor.sma_sunnyboy_storage.attributes['result_2']
value_template: "{{ states.sensor.sma_sunnyboy_storage.attributes['result_3']

And yes the @tmeringer for pointing the undocumented fact that if you omit the dummy value_template, HA will try to send the rest output to the sensor state and it will most likely throw an error at you because you are exceeding 255 caracters.