Cant get data from rest api call thats in an array within another array

Hi,

I created a sensor that makes a REST api call, the data returned is JSON formatted.
I can access all data fine, and the sensor reports it accurately, except the data that’s contained inside an array inside another array.

Configuration:

rest:
  - resource: !secret living_room_tablet_rest
    scan_interval: 5
    sensor:
      - name: "living_room_tablet_light_sensor_1"
        value_template: "{{ value_json.sensorInfo[1].values[0] }}"
        device_class: illuminance
        unit_of_measurement: ""

This is the sample json response:

{
   "deviceName":"Galaxy Tab A8",
   "batteryLevel":67,
   "sensorInfo":[
      {
         "type":5,
         "name":"dual_light",
         "vendor":"Sprd Group Ltd.",
         "version":1,
         "accuracy":3,
         "values":[
            18
         ],
         "lastValuesTime":1686869112093,
         "lastAccuracyTime":1686864453866
      },
      {
         "type":29,
         "name":"Stationary Detect",
         "vendor":"Sprd Group Ltd.",
         "version":1,
         "accuracy":-1
      }
   ]
}

I can access the battery level with: {{ value_json.batteryLevel }}
I can even access the lastValuesTime with: {{ value_json.sensorInfo[0].lastValuesTime }}
But I cannot access sensorInfo > values data.
Tried both of these but didnt work:
{{ value_json.sensorInfo[0].values[0] }}
{{ value_json.sensorInfo[0].values }}

That one should have worked.

Maybe “values” is a reserved word like “value” and “json_value”. If so try this:

{{ value_json.sensorInfo[0]['values'][0] }}
1 Like

Thank you so much, that appears to have been the case, fixed with your solution. I really appreciate you helping me out.

1 Like