Extracting values from JSON

Hello,

I’m trying to extract a value from some JSON output. The output looks like this - I have removed some of the result part as its quite long:

{
   "ActTime" : 1475479301,
   "ServerTime" : "2016-10-03 08:21:41",
   "Sunrise" : "07:13",
   "Sunset" : "18:39",
   "result" : [
      {
         "Humidity" : 98,
         "HumidityStatus" : "Wet",
         "Temp" : 4.80,
         "Type" : "Temp + Humidity",
         "TypeImg" : "temperature",
         "idx" : "3698"
      }
   ],
   "status" : "OK",
   "title" : "Devices"
}

If I have a sensor like this I get the sunrise returned correctly:

  - platform: rest
    resource: http://192.168.1.110:8080/json.htm?type=devices&rid=3698
    value_template: '{{ value_json.Sunrise }}'
    method: GET
    name: Test
    unit_of_measurement: "°C"

However I want to get the Temp out of the result section, if I try this I get nothing:

  - platform: rest
    resource: http://192.168.1.110:8080/json.htm?type=devices&rid=3698
    value_template: '{{ value_json.result.Temp }}'
    method: GET
    name: Outside
    unit_of_measurement: "°C"

If I change the value_template to the following I get everything in result:

value_template: '{{ value_json.result }}'

How do I extract just the Temp value?

Thanks.

You need to extract Temp from result.

Yes but how?

1 Like

Interested to find out the answer as this looked correct to me.

Try value_template: '{{ value_json.result[0].Temp }}'

1 Like

That has worked - thanks for the help

Dave, why did that work? What does the [0] do here? I’m trying to understand this better.

In the JSON, results is an array. results[0] would get the first element in that array.

1 Like

Thank you!