Extract with 'value_json'

I’m struggeling to extract a deep nested JSON value from a MQTT response.

Part of the MQTT message in JSON:

{
   "id":925,
   "need_ack":1,
   "sensorData":[
      {
         "battery":{
            "status":1,
            "value":100
         },
         "co2":{
            "status":0,
            "value":505
   ..
}

I want to get the value in ‘co2’. This is what I got so far as value_template, bit it does not work:

"{{ value_json[['sensorData']['co2']['value']] }}"

All examples I could find while looking for the answer were not going further than 2 nestings. Hopefully someone has the magic answer for me :slight_smile:

{{ value_json['sensorData'][0]['co2']['value'] }}

As @exxamalte mentioned, that index 0 is what you are missing. If you see a brace in JSON ( [ ) that is an array and you have to address the array numerically if there is no key (i.e., “record_1” : { ).

1 Like

Great, thank you! This worked!

And thank you too, CO_4x4! I have learned something new today :slight_smile:

1 Like