Reading from JSON data source

Hi all,

I’m a bit lost on how to separate and read from a JSON value I’m getting from MQTT.

I have the blow JASON string;

{“Energy”:[{“pvPower”:“0.00”,{“pvCurrent”:“0.0”},{“houseLoad”:“0.29”},{“soc”:“88.0”},{“batteryVoltage”:“50.3”},{“batteryCurrent”:“5.8”},{“temperature”:“16.9”},{“batteryPower”:“-0.29”}]}

I’ve tried breaking down the content and showing it in a sensor using the below config:

  • platform: mqtt
    name: “Battery Temperature”
    state_topic: “energy/grid”
    unit_of_measurement: ‘°C’
    value_template: “{{ value_json[‘Energy’].temperature }}”

But I’m getting an “Unknown” result. I tried various different JSON structures and can’t work it out. Can anyone give me an example of how to get one of the values from the string?

Thanks,
Cheese

That JSON is incorrect - there is a missing curly bracket, for example after {"pvPower":"0.00" }.
Is that an exact copy&paste of the payload? If so, then the source is already incorrect, and there is no way you can extract data.
If that curly bracket is in the original source, then the following template would work {{ value_json['Energy'][6]['temperature'] }} - if the order of those entries is always the same.

Thanks so much! Yes the missing } was my mistake. Not sure how it went missing when I pasted the value into the thread, but the template you provided works. Thanks again.

1 Like