Rest sensor with xml tojson is working but no attributes created

Hello everybody,
I try to setup a sensor for my termostat.
with postman I can query the termostat and I receive back this xml:

<?xml version="1.0"?>
<root>
	<comando>gsf</comando>
	<parametri>
		<parametro0>4</parametro0>
	</parametri>
	<records>
		<record>
			<dato0>31</dato0>
			<dato1>128</dato1>
		</record>
		<record>
			<dato0>32</dato0>
			<dato1>128</dato1>
		</record>
	</records>
</root>

here the configuration for my sensor :

  - platform: rest
    name: termo_status
    resource: http://192.168.1.14/termo.php?command=XYZ&parameter=123
   # value_template: "{{ value_json.root }}"
    json_attributes:
      - termo1 # 31
      - termo2 # 32
    value_template: OK

  - platform: template  #estraggo i valori dal JSON sopra
    sensors:
      termo1:
        friendly_name: termo1_on_off
        value_template: "{{ state_attr('sensor.termo_status', 'termo1')['records'][0]['dato1'] }}"
      termo2:
        friendly_name: termo2_on_off
        value_template: "{{ state_attr('sensor.termo_status', 'termo2')['records'][1]['dato1'] }}"

if I change the value_template: to
“{{ value_json.root }}”
I can see the json structure parsed by home assistant :

But as you can see from the screenshot I cannot create attributes as expected ,neither trying to change the path of Json.

the error log reports:

* TemplateError('UndefinedError: 'None' has no attribute 'records'') while processing template 'Template("{{ state_attr('sensor.termo_status', 'termo1')['records'][0]['dato1'] }}")' for attribute '_attr_native_value' in entity 'sensor.termo1'
* TemplateError('UndefinedError: 'None' has no attribute 'records'') while processing template 'Template("{{ state_attr('sensor.termo_status', 'termo2')['records'][1]['dato1'] }}")' for attribute '_attr_native_value' in entity 'sensor.termo2'

Can you help me to troubleshoot it?

Thank you

From the screenshot your ‘status’ sensor has no (well: 1) attribute so the template sensors won’t work logically. try using: states(’… instead…sorry to be short but leaving office… just my 2cts, can look more tomorrow

Hi
tried your suggestion, but states or state_attr did not change the current result :frowning:
it seems that anything I do will probably not affect missing attributes just because home assistant did not create them.

I thought it was due to a wrong conversion from xml to json but the strange thing is that if I change the value template to

value_template: “{{ value_json.root }}”

or to

value_template: "{{ value_json.root.records.record[0]}}"

I can navigate the json structure… so why attributes are not there?

Thank you for any suggestions !

Can you send me the json as shown in the state of the sensor please, as I cannot reproduce the rest…

Try this
[/quote]

  - platform: template  #estraggo i valori dal JSON sopra
    sensors:
      termo1:
        friendly_name: termo1_on_off
        value_template: "{{ (states('sensor.termo_status')|from_json())['records']['record'][0]['dato1'] }}"
      termo2:
        friendly_name: termo2_on_off
        value_template: "{{ (states('sensor.termo_status')|from_json())['records']['record'][1]['dato1'] }}"

same result as before :frowning:
here my Json as I can copy and paste from developer tool:

{'comando': 'gsf', 'parametri': {'parametro0': '4'}, 'records': {'record': [{'dato0': '31', 'dato1': '128'}, {'dato0': '32', 'dato1': '128'}, {'dato0': '33', 'dato1': '128'}, {'dato0': '34', 'dato1': '128'}, {'dato0': '35', 'dato1': '128'}]}}

one interesting thing … If I use the jinja2 template into developer tool and replace the single quote ( ’ ) with the double ( " ) in the json string :

{% set my_test_testxx = {"comando": "gsf", "parametri": {"parametro0": "4"}, "records": {"record": [{"dato0": "31", "dato1": "128"}, {"dato0": "32", "dato1": "128"}, {"dato0": "33", "dato1": "128"}, {"dato0": "34", "dato1": "128"}, {"dato0": "35", "dato1": "128"}]}}
%}

the sintax:
{{ my_test_testxx.records.record[1].dato0 }}

produces the correct output:
32

this make me crazy…

thank you!

Hard to repro but this is what I did
Added file: in www/test3.json, this as data

{
	"root": {
		"comando": "gsf",
		"parametri": {
			"parametro0": "4"
		},
		"records": {
			"record": [
				{
					"dato0": "31",
					"dato1": "128"
				},
				{
					"dato0": "32",
					"dato1": "128"
				}
			]
		}
	}
}

the rest sensor is then this

rest:
  - authentication: basic
    scan_interval: 300
    verify_ssl: false
    headers:
      Content-Type: application/json
      User-Agent: Home Assistant
      Accept-Encoding: identity
    resource: http://192.168.1.20:8124/local/test3.json
    sensor:             
      - name: testing_a1
        value_template: "{{ value_json.root }}"
        json_attributes_path: $.root.records.record.0
        json_attributes:
          - dato0
          - dato1       
      - name: testing_a2
        value_template: "{{ value_json.root }}"
        json_attributes_path: $.root.records.record.1
        json_attributes:
          - dato0
          - dato1 

your code works like a charm!!!
thank you very much!!
I put your code in my rest.yaml file and attributes are there!!

I still do not understand why the alternative syntax works… I supposed both way were valid… :thinking:

thank you for your time !

I believe it is not a ‘neat’ json, I got errors with incorrect characters, strings not being used in json (although I did a to_json first)… might also be that the rest sensor has a bug as … what you noticed… it does mostly work in devtools.

Anyhow, if done, please mark my post for solution, may help others in the future :slight_smile: