Rest API help needed (Multiple data from one resource)

Hello
I have stumbled in scrubbing the datapoints from a json reply. I want to get data from a json reply which answers like this -
Ask -http://192.168.39.14/app/config/0/1
Response -

	"Code":	0,
	"Result":	{
		"UpdateInterval":	{
			"EnMap":	0,
			"Value":	10
		},
		"DataValidTime":	{
			"EnMap":	0,
			"Value":	7
		},
		"Point":	[{
				"Name":	"CAP_A",
				"Unit":	"%",
				"Type":	"unsigned",
				"Value":	0,
				"Writable":	0,
				"EnMap":	1
			}, {
				"Name":	"cap_av_a",
				"Unit":	"%",
				"Type":	"unsigned",
				"Value":	0,
				"Writable":	0,
				"EnMap":	1
			}, {
				"Name":	"DP_A",
				"Unit":	"KPa",
				"Type":	"float",
				"Value":	1490.648560,
				"Writable":	0,
				"EnMap":	1,
				"EnMapAdd":	0
			}, {
				"Name":	"SP_A",
				"Unit":	"KPa",
				"Type":	"float",
				"Value":	821.659912,
				"Writable":	0,
				"EnMap":	1,
				"EnMapAdd":	0
			}, {
				"Name":	"SCT_A",
				"Unit":	"°C",
				"Type":	"float",
				"Value":	41.521896,
				"Writable":	0,
				"EnMap":	0,
				"EnMapAdd":	2
			}, {
				"Name":	"SST_A",
				"Unit":	"°C",
				"Type":	"float",
				"Value":	20.516191,
				"Writable":	0,
				"EnMap":	0,
				"EnMapAdd":	2
			}, {
				"Name":	"DEFRT_A",
				"Unit":	"°C",
				"Type":	"float",
				"Value":	-17.777779,
				"Writable":	0,
				"EnMap":	0,
				"EnMapAdd":	0
			}, {
				"Name":	"speed_a",
				"Unit":	"%",
				"Type":	"unsigned",
				"Value":	0,
				"Writable":	0,
				"EnMap":	0
			}],
		"RegStart":	15
	}
}
type or paste code here

To get this - I am using the below in my yaml. Its not getting me past this error

end of the stream or a document separator is expected at line 10, column 4:
    	  - name: "runstate"	
       ^

My yaml-

rest:
  - authentication: basic
    username: "xx"
    password: "xx"
    scan_interval: 2
  - resource: http://192.168.39.14/app/config/0/0
    sensor:
      - name: "ctr_type"
	    value_template: "{{ value_json.Point.0.Value }}"
	  - name: "runstate"	
		value_template: "{{ value_json.Point.1.Value }}"
	  - name: "CHIL_S_S"	
		value_template: "{{ value_json.Point.2.Value }}"
	  - name: "CHIL_OCC"	
		value_template: "{{ value_json.Point.3.Value }}"
	  - name: "min_left"	
		value_template: "{{ value_json.Point.4.Value }}"
	  - name: "ALM"	
		value_template: "{{ value_json.Point.7.Value }}"
	  - name: "CAP_T"	
		value_template: "{{ value_json.Point.13.Value }}"
	  - name: "SP"	
		value_template: "{{ value_json.Point.17.Value }}"
	  - name: "CTRL_PNT"	
		value_template: "{{ value_json.Point.18.Value }}"
	  - name: "CTRL_WT"	
		value_template: "{{ value_json.Point.19.Value }}"
	  - name: "OAT"	
		value_template: "{{ value_json.Point.20.Value }}"

  - authentication: basic
    username: "xx"
    password: "xx"
    scan_interval: 2
    resource: http://192.168.39.14/app/config/0/1
    sensor:
      - name: "CP_A1"
	    value_template: "{{ value_json.Point.0.Value }}"
	  - name: "DP_A"	
		value_template: "{{ value_json.Point.2.Value }}"
	  - name: "SP_A"	
		value_template: "{{ value_json.Point.3.Value }}"
	  - name: "SCT_A"	
		value_template: "{{ value_json.Point.4.Value }}"
	  - name: "SST_A"	
		value_template: "{{ value_json.Point.5.Value }}"

Please help.

I have never seen that syntax.
Shouldn’t it be

```
sensor:
- platform: rest

… and so on…
```
Never mind I found it RESTful - Home Assistant (home-assistant.io)
You have a - at resource: http://192.168.39.14/app/config/0/0 the docs does not have that.

But is your idea to have each value as a separate entity? You could have them as attributes if you wanted.

I dont know much about json syntax. I saw the example at https://www.home-assistant.io/integrations/rest/ but couldnot fit that to my resutt returns.
So, I did what I knew - each value as separate identity.

Please identify - I saw examples and I dont think I have bad indentation here

Ohh- Looks like I cut paste when I was editing.
In my real one there is no - here.
My bad.

rest:
  - authentication: basic
    username: "xx"
    password: "xx"
    scan_interval: 2
    resource: http://192.168.39.14/app/config/0/0
    sensor:
      - name: "ctr_type"
	    value_template: "{{ value_json.Point.0.Value }}"
	  - name: "runstate"	
		value_template: "{{ value_json.Point.1.Value }}"
	  - name: "CHIL_S_S"	
		value_template: "{{ value_json.Point.2.Value }}"
	  - name: "CHIL_OCC"	
		value_template: "{{ value_json.Point.3.Value }}"
	  - name: "min_left"	
		value_template: "{{ value_json.Point.4.Value }}"
	  - name: "ALM"	
		value_template: "{{ value_json.Point.7.Value }}"
	  - name: "CAP_T"	
		value_template: "{{ value_json.Point.13.Value }}"
	  - name: "SP"	
		value_template: "{{ value_json.Point.17.Value }}"
	  - name: "CTRL_PNT"	
		value_template: "{{ value_json.Point.18.Value }}"
	  - name: "CTRL_WT"	
		value_template: "{{ value_json.Point.19.Value }}"
	  - name: "OAT"	
		value_template: "{{ value_json.Point.20.Value }}"

  - authentication: basic
    username: "xx"
    password: "xx"
    scan_interval: 2
    resource: http://192.168.39.14/app/config/0/1
    sensor:
      - name: "CP_A1"
	    value_template: "{{ value_json.Point.0.Value }}"
	  - name: "DP_A"	
		value_template: "{{ value_json.Point.2.Value }}"
	  - name: "SP_A"	
		value_template: "{{ value_json.Point.3.Value }}"
	  - name: "SCT_A"	
		value_template: "{{ value_json.Point.4.Value }}"
	  - name: "SST_A"	
		value_template: "{{ value_json.Point.5.Value }}"

Tried it in my own config.
You have bad spaces.

Notice the difference between the three first and the following. Dots vs lines.
image

Remove the spaces and add “real” spaces.

Thank you - I found thousand of such “bad spaces” when I zoomed in.
I used hex editor - and it inserted ll these spaces, which are “invisble”

Have edited and waiting for it to restart to check if the REST works. (No errors as per config check)

Alittle more help needed.
The values being deciphered are unknown(My value_json.Point format)
So looking at the json response, what should it be?
(I have set it as value_json.Point.0.Value)
This is showing in log-eg- not Point 0

2022-08-29 13:03:02.762 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=sensor.sp_b, old_state=None, new_state=<state sensor.sp_b=unknown; friendly_name=SP_B @ 2022-08-29T13:03:02.762750+04:00>>

The json is not complete in your first post. But you are at least missing “Result”

value_template: "{{ value_json.Result.Point.0.Value }}"

And I think 0 wont work, so you probably need:

value_template: "{{ value_json.Result.Point[0].Value }}"

Got it working. Thank you. [ ] gave error. Not required.

Other than that - I moved everything from Modbus to web json as HA was slamming my modbus.
I now find exactly the same - Its now slamming my poor box with 1000s of queries per second. Scan interval just makes the Jason result delay. Why is that so?

You have one resource. Home Assistant does not call that resource once and then fill in all the sensors with the result. It calls the resource for each sensor. And you have set a scan interval of 2 seconds, which means Home Assistant is going to reach the point very quickly where it is still making resource requests from the previous interval, when the next interval begins.

Other than a custom_component that will call the resource just once, you are better doing that sort of thing in NodeRed which makes it easy to work with the data and then either push the results to HA over MQTT or the NodeRed companion which lets you update sensors that you create in NodeRed.

Just see this - Its slamming my poor box every ms.
Scan interval is set at 10s

2022-08-29 13:03:02.762 DEBUG (MainThread) [homeassistant.components.rest.sensor] Data fetched from resource: {
	"Code":	0,
	"Result":	{
		"UpdateInterval":	{
			"EnMap":	0,
			"Value":	10
		},
		"DataValidTime":	{
			"EnMap":	0,
			"Value":	6
		},
		"Point":	[{
				"Name":	"CAP_B",
				"Unit":	"%",
				"Type":	"unsigned",
				"Value":	42,
				"Writable":	0,
				"EnMap":	0
			}, {
				"Name":	"cap_av_b",
				"Unit":	"%",
				"Type":	"unsigned",
				"Value":	42,
				"Writable":	0,
				"EnMap":	0
			}, {
				"Name":	"DP_B",
				"Unit":	"KPa",
				"Type":	"float",
				"Value":	1778.719360,
				"Writable":	0,
				"EnMap":	0,
				"EnMapAdd":	0
			}, {
				"Name":	"SP_B",
				"Unit":	"KPa",
				"Type":	"float",
				"Value":	534.897644,
				"Writable":	0,
				"EnMap":	0,
				"EnMapAdd":	0
			}, {
				"Name":	"SCT_B",
				"Unit":	"°C",
				"Type":	"float",
				"Value":	48.569424,
				"Writable":	0,
				"EnMap":	0,
				"EnMapAdd":	0
			}, {
				"Name":	"SST_B",
				"Unit":	"°C",
				"Type":	"float",
				"Value":	7.935587,
				"Writable":	0,
				"EnMap":	0,
				"EnMapAdd":	0
			}, {
				"Name":	"DEFRT_B",
				"Unit":	"°C",
				"Type":	"float",
				"Value":	-17.777779,
				"Writable":	0,
				"EnMap":	0,
				"EnMapAdd":	0
			}, {
				"Name":	"speed_b",
				"Unit":	"%",
				"Type":	"unsigned",
				"Value":	0,
				"Writable":	0,
				"EnMap":	0
			}],
		"RegStart":	0
	}
}
2022-08-29 13:03:02.762 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=sensor.sp_b, old_state=None, new_state=<state sensor.sp_b=unknown; friendly_name=SP_B @ 2022-08-29T13:03:02.762750+04:00>>
2022-08-29 13:03:02.763 DEBUG (MainThread) [homeassistant.components.rest.sensor] Data fetched from resource: {
	"Code":	0,
	"Result":	{
		"UpdateInterval":	{
			"EnMap":	0,
			"Value":	10
		},
		"DataValidTime":	{
			"EnMap":	0,
			"Value":	6
		},
		"Point":	[{
				"Name":	"CAP_B",
				"Unit":	"%",
				"Type":	"unsigned",
				"Value":	42,
				"Writable":	0,
				"EnMap":	0
			}, {
				"Name":	"cap_av_b",
				"Unit":	"%",
				"Type":	"unsigned",
				"Value":	42,
				"Writable":	0,
				"EnMap":	0
			}, {
				"Name":	"DP_B",
				"Unit":	"KPa",
				"Type":	"float",
				"Value":	1778.719360,
				"Writable":	0,
				"EnMap":	0,
				"EnMapAdd":	0
			}, {
				"Name":	"SP_B",
				"Unit":	"KPa",
				"Type":	"float",
				"Value":	534.897644,
				"Writable":	0,
				"EnMap":	0,
				"EnMapAdd":	0
			}, {
				"Name":	"SCT_B",
				"Unit":	"°C",
				"Type":	"float",
				"Value":	48.569424,
				"Writable":	0,
				"EnMap":	0,
				"EnMapAdd":	0
			}, {
				"Name":	"SST_B",
				"Unit":	"°C",
				"Type":	"float",
				"Value":	7.935587,
				"Writable":	0,
				"EnMap":	0,
				"EnMapAdd":	0
			}, {
				"Name":	"DEFRT_B",
				"Unit":	"°C",
				"Type":	"float",
				"Value":	-17.777779,
				"Writable":	0,
				"EnMap":	0,
				"EnMapAdd":	0
			}, {
				"Name":	"speed_b",
				"Unit":	"%",
				"Type":	"unsigned",
				"Value":	0,
				"Writable":	0,
				"EnMap":	0
			}],
		"RegStart":	0
	}
}
2022-08-29 13:03:02.763 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=sensor.sct_b, old_state=None, new_state=<state sensor.sct_b=unknown; friendly_name=SCT_B @ 2022-08-29T13:03:02.763810+04:00>>
2022-08-29 13:03:02.764 DEBUG (MainThread) [homeassistant.components.rest.sensor] Data fetched from resource: {
	"Code":	0,
	"Result":	{
		"UpdateInterval":	{
			"EnMap":	0,
			"Value":	10
		},
		"DataValidTime":	{
			"EnMap":	0,
			"Value":	6
		},
		"Point":	[{
				"Name":	"CAP_B",
				"Unit":	"%",
				"Type":	"unsigned",
				"Value":	42,
				"Writable":	0,
				"EnMap":	0
			}, {
				"Name":	"cap_av_b",
				"Unit":	"%",
				"Type":	"unsigned",
				"Value":	42,
				"Writable":	0,
				"EnMap":	0
			}, {
				"Name":	"DP_B",
				"Unit":	"KPa",
				"Type":	"float",
				"Value":	1778.719360,
				"Writable":	0,
				"EnMap":	0,
				"EnMapAdd":	0
			}, {
				"Name":	"SP_B",
				"Unit":	"KPa",
				"Type":	"float",
				"Value":	534.897644,
				"Writable":	0,
				"EnMap":	0,
				"EnMapAdd":	0
			}, {
				"Name":	"SCT_B",
				"Unit":	"°C",
				"Type":	"float",
				"Value":	48.569424,
				"Writable":	0,
				"EnMap":	0,
				"EnMapAdd":	0
			}, {
				"Name":	"SST_B",
				"Unit":	"°C",
				"Type":	"float",
				"Value":	7.935587,
				"Writable":	0,
				"EnMap":	0,
				"EnMapAdd":	0
			}, {
				"Name":	"DEFRT_B",
				"Unit":	"°C",
				"Type":	"float",
				"Value":	-17.777779,
				"Writable":	0,
				"EnMap":	0,
				"EnMapAdd":	0
			}, {
				"Name":	"speed_b",
				"Unit":	"%",
				"Type":	"unsigned",
				"Value":	0,
				"Writable":	0,
				"EnMap":	0
			}],
		"RegStart":	0
	}
}
2022-08-29 13:03:02.764 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=sensor.sst_b, old_state=None, new_state=<state sensor.sst_b=unknown; friendly_name=SST_B @ 2022-08-29T13:03:02.764842+04:00>>
2022-08-29 13:03:02.765 

So, the behaviour is the same as modbus(I now have removed the modbus option as the library was old and is not maintatined well - was giving so many errros)

My problem is the same that means - HA doesnot respect scan interval - It will create a request for EACH sensor.

Any other solution other than Node red(native in HA) - Node red is additional maint point for me.

Adding - Exact behaviour of modbus. HA fails here disgracefully again (I thiugh it was modbus which was creating issue) - The senors value become unavavailable, expected, but later becomes random - It looses tracks of sensors. One sensor value becomes other sensor value.

There is something seriously wrong here.

Typically people will create a single rest sensor with the “OK” value - and all the JSON returned as an attribute, and then use template sensors to get the data out of the attributes of the main sensor.

So you would for example do something like set the attributes for the main sensor like this:

json_attributes:
  - "Result"

And then all the JSON under the Result key would be available as an attribute in the main sensor, which you could then access with template sensors:

{{ state_attr('sensor.my_rest_sensor','Result')['Point'][0]['Value'] }}

Makes sense. Let me try that.

Just updated the post, because it should be json_attributes instead of attributes

https://community.home-assistant.io/t/restful-sensor-state-attr-unknow-and-json-attributes-path-solved/373285/2
Please see this topic-21 Dec - tom_l, who is moderator(and assuming participating in maintaining) is writing clearly that one call is made and multiple sensors are updated using that call.

That is incorrect - As I see using my code is slamming my json box.

As I wrote above, my jason syntaxing is poor. I just cannot make head or tail from making single call and filling the sensor using your method. I tried and got stuck.

    scan_interval: 5
    resource: http://192.168.39.14/app/config/0/0
    sensor:
      - name: "HVAC_0"
	    value_template: "OK"
	    json_attributes:
          - "Result"
            {{ state_attr('sensor.HVAC_0')['Point'][0]['Value'] }}

Remove the last line (the template)

That goes in the template sensor.

template:
  - sensor:
      - name: "ctr_type"
        state: "{{ state_attr('sensor.HVAC_0','Result')['Point'][0]['Value'] }}"
      - name: "runstate"
        state: "{{ state_attr('sensor.HVAC_0','Result')['Point'][1]['Value'] }}"

etc

… If you need to have them in separate sensors.
Most of the times I prefer to have one sensor with all values as attributes to make it easier to see all values at one time.
You can still use the attributes in automations or whatever you need the values for.

1 Like