Get temperature from Fibaro Home Center 2 sensor with nested JSON

Hi,

Im trying to extract temperature value from a JSON generated on my Fibaro Home Center 2. The temperature is shown as “value” under “properties” (20.70) when calling my Fibaro motion sensor with;

http://192.168.x.xxx/api/devices?id=xxx

JSON;

{  
   "id":361,
   "name":"Temperatur",
   "roomID":416,
   "type":"com.fibaro.temperatureSensor",
   "baseType":"com.fibaro.multilevelSensor",
   "enabled":true,
   "visible":true,
   "isPlugin":false,
   "parentId":359,
   "remoteGatewayId":0,
   "interfaces":[  ],
   "properties":{  
  "parameters":[  ],
  "pollingTimeSec":0,
  "wakeUpTime":18000,
  "zwaveCompany":"Fibargroup",
  "zwaveInfo":"3,3,67",
  "zwaveVersion":"2.8",
  "batteryLevel":"100",
  "batteryLowNotification":"true",
  "configured":true,
  "dead":"false",
  "defInterval":"0",
  "deviceControlType":"0",
  "deviceIcon":"30",
  "emailNotificationID":"0",
  "emailNotificationType":"0",
  "endPointId":"0",
  "liliOffCommand":"",
  "liliOnCommand":"",
  "log":"",
  "logTemp":"",
  "manufacturer":"",
  "markAsDead":"true",
  "maxInterval":"0",
  "minInterval":"0",
  "model":"",
  "nodeId":"65",
  "offset":"0.00",
  "parametersTemplate":"270",
  "productInfo":"1,15,8,0,16,1,2,8",
  "pushNotificationID":"0",
  "pushNotificationType":"0",
  "remoteGatewayId":"0",
  "saveLogs":"true",
  "serialNumber":"",
  "showFireAlarm":"true",
  "showFreezeAlarm":"false",
  "smsNotificationID":"0",
  "smsNotificationType":"0",
  "stepInterval":"0",
  "unit":"C",
  "useTemplate":"true",
  "userDescription":"",
  "value":"20.70"
   },
   "actions":{  
  "reconfigure":0,
  "setInterval":1
   },
   "created":1516638096,
   "modified":1516638096,
   "sortOrder":99
}

configuration.yaml;

sensor:
 - platform: rest
    resource: http://192.168.0.150/api/devices?id=361
    name: Arbetsrum
    value_template: '{{ value_json.properties.value }}'
    unit_of_measurement: "°C"

Im getting a parser error in the log. I have looked at similar posts but I cant figure this out, I guess I need to set the value_json correctly but the above principle works with other JSONs.

LOG:
Error parsing value: ‘value_json’ is undefined (value: , template: {{ value_json.properties.value }})

Can someone help me out? This could be useful for others moving or using Home Center 2 with HA.

Thanks!

The }, immediately affter value is the clue, it’s nested, so…

Nested JSON in a response is supported as well

{
  "sensor": {
    "type": "air",
    "id": "12345"
  },
  "values": {
    "temp": 26.09,
    "hum": 56.73,
  }
}
Just use the “Square bracket notation” to get the value.

'{{ value_json["values"]["temp"] }}'

You need this sort of thing. Yours would be [“properties”][“value”] i believe

Thanks for the quick answer! So thats how you do the nested JSON, great!

The yaml is now;

sensor:
- platform: rest
   resource: http://192.168.x.xxx/api/devices?id=xxx
   name: Arbetsrum
   value_template: '{{ value_json["properties"]["value"] }}'

However, I still get an error saying that the “value_json” is undefined? I must have missed something?

Error parsing value: ‘value_json’ is undefined (value: , template: {{ value_json[“properties”][“value”] }})

Regards

Check your indentation, it doesn’t look right to me.

Well the indentation is ok, I just missed the space between sensor and platform when I pasted the config.

Anyway, your suggestion works beautiful when I paste the whole JSON in the template tool and use the brackets just like in the config. It does not work “live” in Home Assistant… I will test test to store the result locally in a file to locate where the error occurs.

Something is fishy.

Regards

Well. For all of you that is stuck just like I was;

You need authentication! And I needed to reboot the Home Assistant server before I got it to work!

Code to extract the temp value from a Fibaro Motion Sensor;

  • platform: rest
    resource: http://192.168.x.xxx/api/devices?id=xxx
    username: name
    password: password
    authentication: basic
    name: Fibaro Sensor
    unit_of_measurement: “°C”
    value_template: ‘{{ value_json[“properties”][“value”] }}’
    headers:
    User-Agent: HomeAssistant
    Content-Type: application/json