Value_template Nested JSON

Hi Guy’s,

I’ve been working on extracting a JSON value from a REStful API for a few days now and I seem to be hitting a wall in regards to the nested JSON values and how to get these formatted into a value_template config.

The data is from a Fireboard BBQ thermometer, I have API access and get data back via curl.
I have created this rest sensor,

The Fireboard API seems to have evolved a lot since the previous threads on this topic. Resulting in a much deeper nesting.

  - platform: rest
    scan_interval: 60
    resource: "https://fireboard.io/api/v1/devices/UUID/temps.json" 
    headers:
      Authorization: "TOKEN"
    value_template: "{{ value_json['channels']['current_temp'] }}"
    name: Fireboard Temp
    json_attributes:
     - current_temp
     - channel_label

Sensor returns nothing,

My output of the API data is here,

 [
    {
       "uuid":"Private",
       "id":Private,
       "title":"FireBoard 2 Drive",
       "hardware_id":"Private",
       "created":"2021-04-12T13:28:33.278Z",
       "latest_temps":[
          {
             "degreetype":1,
             "channel":1,
             "temp":24.7,
             "created":"2021-07-12T20:05:44Z"
          }
       ],
       "device_log":{
          "internalIP":"192.168.56.128",
          "onboardTemp":25.7,
          "linkquality":"38/100",
          "bleClientMAC":"",
          "vBattPer":0.9770082315882354,
          "nightmode":false,
          "deviceID":"Private",
          "macNIC":"7c:9e:bd:28:df:fc",
          "uptime":"0:20",
          "vBattPerRaw":0.933,
          "publicIP":"Private",
          "frequency":"2.4 GHz",
          "cpuUsage":"20%",
          "date":"2021-07-12 20:02:15 UTC",
          "signallevel":-81,
          "boardID":"GHK4DKRRJ",
          "band":"802.11bgn",
          "mode":"Managed",
          "tempFilter":"true",
          "versionImage":"201703260554",
          "versionEspHal":"HAL: V1R2;MSP: 1.8.0;",
          "memUsage":"2.6M/4.2M",
          "txpower":78,
          "model":"FBX2D",
          "ssid":"Private",
          "contrast":"10",
          "commercialMode":"false",
          "macAP":"74:83:c2:97:ef:e8",
          "drivesettings":"{\"p\":4,\"s\":1,\"d\":5,\"ms\":100,\"f\":0,\"l\":1}",
          "auxPort":"",
          "version":"3.1.30",
          "bleSignalLevel":0,
          "versionNode":"5.0.0",
          "diskUsage":"0.7M/4.0M",
          "versionUtils":"6.1.3",
          "vBatt":4.097,
          "versionJava":"7.6.5"
       },
       "last_templog":"2021-07-12T20:05:19Z",
       "last_battery_reading":0.9770082315882354,
       "channels":[
          {
             "channel_label":"Pit",
             "current_temp":24.7,
             "alerts":[
                
             ],
             "range_average_temp":24.4,
             "id":14871974,
             "last_templog":{
                "degreetype":1,
                "channel":1,
                "temp":24.7,
                "created":"2021-07-12T20:05:44Z"
             },
             "channel":1,
             "created":"2021-07-12T19:42:13Z",
             "enabled":true,
             "range_min_temp":22.3,
             "sessionid":2627590,
             "range_max_temp":24.7
          },
          {
             "channel_label":"Meat",
             "channel":2,
             "created":"2021-07-12T19:42:13Z",
             "alerts":[
                {
                   "time_stop":"23:59:59",
                   "notify_email":false,
                   "time_start":"00:00:01",
                   "id":3145146,
                   "notify_sms":false,
                   "temp_max":80.0,
                   "minutes_buffer":0,
                   "channel":2,
                   "created":"2021-07-12T19:42:13Z",
                   "notify_app":true,
                   "enabled":true,
                   "device_id":106820,
                   "temp_min":null,
                   "session_id":2627590,
                   "minutes_repeat":30
                }
             ],
             "enabled":true,
             "id":14871975,
             "sessionid":2627590
          },
          {
             "channel_label":"Channel 3",
             "channel":3,
             "created":"2021-07-12T19:42:13Z",
             "alerts":[
                
             ],
             "\"enablePrivate" 

Under channels > current_temp I am trying to read this value for the channel labelled Pit, also referred to as channel 1 further down.

Anyone got a suggestion for formatting a value_template to capture this?

Use this tool to find the path to your wanted value: https://jsonpathfinder.com/

I can’t do it for you because you have not pasted the complete json message.

1 Like

Thanks Tom, this could be a key part of my problem, this is the complete message curl returned to me so it’s not being understood correctly by pathfinder. I’m missing something from curl, or maybe using an incorrect switch to get the full data.

You put me on the right track, I expanded the curl with -v and got the full data, pathfinder now works. Will post an update if I get it sorted for future users.

How are you getting the data?

If using a state, you’ve reached the 255 character limit for states. Attributes do not have this limit.

Go to the resource in a web browser and copy the data.

It should be just 4 char for the temp.

Path finder is returning to me the below as my reference, I just need to format this into a value template next.

x[0].latest_temps[0].temp

value_template: "{{ value_json[0].latest_temps[0].temp }}"

1 Like

Try:

value_template: >-
      {% set items = value_json[0]['channels'] | selectattr('channel_label','eq','Pit') | list | first %}
      {{ items['current_temp'] }}
1 Like

This is returning the following error, I got this on my configs also,

(MainThread) [homeassistant.helpers.template] Template variable error: dict object has no element 0 when rendering '{{ value_json[0].latest_temps[0].temp }}'

Hi Koying,

This won’t confirm, am I missing something?

Invalid config for [sensor.rest]: invalid template (TemplateSyntaxError: 
unexpected char '"' at 75) for dictionary value @ data['value_template']. 
Got '{% set items = value_json[0][\'channels\'] |
selectattr(\'channel_label\',\'eq\',"Pit\') | list | first %} 
{{ items[\'current_temp\'] }}'. (See ?, line ?).

Wrong quoting on my side. I have updated my post above.

Thanks for the help guys, errors are gone, but not seeing any data.

My sensor element looks like this,

sensor:
  - platform: time_date
    display_options:
      - 'time'
      - 'date'
      - 'date_time'
      - 'date_time_utc'
      - 'date_time_iso'
      - 'time_date'
      - 'time_utc'
      - 'beat'

  - platform: rest
    resource: https://fireboard.io/api/v1/devices/UUID/temps.json 
    json_attributes:
     - current_temp
    headers:
      Authorization: token
    value_template: >-
      {% set items = value_json[0]['channels'] | selectattr('channel_label','eq','Pit') | list | first %}
      {{ items['current_temp'] }}
    name: Fireboard Temp2

Entity just displays “Unknown”

Works for me with the partial json you provided

{% set my_test_json = [{
		"uuid": "Private",
		"id": Private,
		"title": "FireBoard 2 Drive",
		"hardware_id": "Private",
		"created": "2021-04-12T13:28:33.278Z",
		"latest_temps": [{
				"degreetype": 1,
				"channel": 1,
				"temp": 24.7,
				"created": "2021-07-12T20:05:44Z"
			}
		],
		"device_log": {
			"internalIP": "192.168.56.128",
			"onboardTemp": 25.7,
			"linkquality": "38/100",
			"bleClientMAC": "",
			"vBattPer": 0.9770082315882354,
			"nightmode": false,
			"deviceID": "Private",
			"macNIC": "7c:9e:bd:28:df:fc",
			"uptime": "0:20",
			"vBattPerRaw": 0.933,
			"publicIP": "Private",
			"frequency": "2.4 GHz",
			"cpuUsage": "20%",
			"date": "2021-07-12 20:02:15 UTC",
			"signallevel": -81,
			"boardID": "GHK4DKRRJ",
			"band": "802.11bgn",
			"mode": "Managed",
			"tempFilter": "true",
			"versionImage": "201703260554",
			"versionEspHal": "HAL: V1R2;MSP: 1.8.0;",
			"memUsage": "2.6M/4.2M",
			"txpower": 78,
			"model": "FBX2D",
			"ssid": "Private",
			"contrast": "10",
			"commercialMode": "false",
			"macAP": "74:83:c2:97:ef:e8",
			"drivesettings": "{\"p\":4,\"s\":1,\"d\":5,\"ms\":100,\"f\":0,\"l\":1}",
			"auxPort": "",
			"version": "3.1.30",
			"bleSignalLevel": 0,
			"versionNode": "5.0.0",
			"diskUsage": "0.7M/4.0M",
			"versionUtils": "6.1.3",
			"vBatt": 4.097,
			"versionJava": "7.6.5"
		},
		"last_templog": "2021-07-12T20:05:19Z",
		"last_battery_reading": 0.9770082315882354,
		"channels": [{
				"channel_label": "Pit",
				"current_temp": 24.7,
				"alerts": [

				],
				"range_average_temp": 24.4,
				"id": 14871974,
				"last_templog": {
					"degreetype": 1,
					"channel": 1,
					"temp": 24.7,
					"created": "2021-07-12T20:05:44Z"
				},
				"channel": 1,
				"created": "2021-07-12T19:42:13Z",
				"enabled": true,
				"range_min_temp": 22.3,
				"sessionid": 2627590,
				"range_max_temp": 24.7
			}, {
				"channel_label": "Meat",
				"channel": 2,
				"created": "2021-07-12T19:42:13Z",
				"alerts": [{
						"time_stop": "23:59:59",
						"notify_email": false,
						"time_start": "00:00:01",
						"id": 3145146,
						"notify_sms": false,
						"temp_max": 80.0,
						"minutes_buffer": 0,
						"channel": 2,
						"created": "2021-07-12T19:42:13Z",
						"notify_app": true,
						"enabled": true,
						"device_id": 106820,
						"temp_min": null,
						"session_id": 2627590,
						"minutes_repeat": 30
					}
				],
				"enabled": true,
				"id": 14871975,
				"sessionid": 2627590
			}, {
				"channel_label": "Channel 3",
				"channel": 3,
				"created": "2021-07-12T19:42:13Z",
				"alerts": [

				]
			}
		]
	}
]
 %}

      {% set items = my_test_json[0]['channels'] | selectattr('channel_label','eq','Pit') | list | first %}
      {{ items['current_temp'] }}

image

1 Like

Probably because you completed the json. Steve indicated he is not getting the complete message.

1 Like

Well I could have used that like a thousand times!!

2 Likes

Right. I assumed a curl issue, but obviously, if it’s an issue on the provider side, it won’t work.
IIRC, setting HA log level on “debug” will show what is the actual returned json, doesn’t it?

1 Like

Awesome, thanks.

I set [httpx._client] to debug and got back a 403 forbidden on the API.

2021-07-14 10:17:30 DEBUG (MainThread) [httpx._client] HTTP Request: GET https://fireboard.io/api/v1/devices/UUID/temps.json "HTTP/1.1 403 FORBIDDEN"

I will dig around this now and see whats going on,

Wahoo! It’s working, thanks for all the help guys, it’s really a great community!

A few things learned,
The temps API was returning nothing via curl, so switched to using the devices API.
The Authorization needed the word “Token” passed in the string.

My working Fireboard REST sensor is here,

I’m sure there are improvements that could be implemented for more detail, this is a good start for me, thanks to the gang here I hopefully won’t be burning the dinner :smiley:

  - platform: rest
    resource: https://fireboard.io/api/v1/devices/UUID.json 
    headers:
      Authorization: "Token XXXXXXX"
    json_attributes:
     - current_temp
    value_template: "{{ value_json.channels[0].current_temp }}"
    name: Fireboard Temp

Great.

Note that you don’t need json_attributes (at least not in your current yaml).
This is to extract additional sensor attributes from the JSON, and is to be used in association with json_attributes_path, which specifies the “root” from which the attributes will be extracted.

EDIT: I figured out how to get the key! I have one Temp probe setup and will work through the next 5. I don’t think I’ll use all 6 at the same time. Especially since we have a Yoder smoker coming with the new fireboard controller that I will also add the ambient and 2 probes for that in HA.

Has anyone been able to add info for Battery %?