Syntax problem with states, state_attr & JSON

Hello
I use JSON file to get init values in automations.
the sensor works well, data are seen in TEMPLATE UI:

{{states.sensor.test_json_command_line.attributes.zone}}

give the good value:

bureau

prefered ‘new’ syntax:

{{state_attr('sensor.test_json_command_line','zone')}}

give the good value (but this is first level of JSON file): :slight_smile:

bureau

Again:

{{states.sensor.test_json_command_line.attributes.radiateur.rad2}}

give again the good value:

{'nom': 'bureau2', 'pilote': '100', 'puiss': '1500', 'priorite': '10'}

my (double) question:
1 How transform

{{states.sensor.test_json_command_line.attributes.radiateur.rad2}}

in the ‘state_attr’ syntax ? (this is second level of JSON file) like:

{{state_attr('sensor.test_json_command_line','??? radiateur.rad2 ???')}}

2 How make iteration to read second or third level of JSON, EG (pseudo code):

for each radiateur
  if {{states.sensor.test_json_command_line.attributes.radiateur.rad(i).pilote}}==1
    set command inverse
  else
    set command direct

I have read many doc/posts without success

EDIT
found answer to point 1

{{ state_attr('sensor.test_json_command_line', 'radiateur').rad1 }}
{{ state_attr('sensor.test_json_command_line', 'radiateur').rad1.pilote }}

give good values:

{'nom': 'bureau1', 'pilote': '1', 'puiss': '1000', 'priorite': '10'}
1

point N°2 stay without answer
i try Template looping through state-attributes - #6 by Didgeridrew
with adapted:

{% set rads = state_attr('sensor.test_json_command_line','radiateur') %}
{% for i in range(0, rads | count, 1) %}
{{ rads[i] }}
{% endfor %}

TEMPLATE UI says:

UndefinedError: dict object has no element 0

Post the radiateur attribute’s entire JSON value. I assume it’s a list of dictionaries but it would be best to see the actual value.

@123 thanks for reading:
if i do:

{{states.sensor.test_json_command_line.attributes}}

i get:

{'zone': 'bureau', 'capteur': 'bureau_th', 'radiateur': {'rad1': {'nom': 'bureau1', 'pilote': '1', 'puiss': '1000', 'priorite': '10'}, 'rad2': {'nom': 'bureau2', 'pilote': '100', 'puiss': '1500', 'priorite': '10'}}, 'friendly_name': 'Test JsON command_line'}

it s ok:
the real file is:

{
	"zone": "bureau",
	"capteur": "bureau_th",
	"radiateur": {
		"rad1": {
			"nom": "bureau1",
			"pilote": "1",
			"puiss": "1000",
			"priorite": "10"
		},
		"rad2": {
			"nom": "bureau2",
			"pilote": "100",
			"puiss": "1500",
			"priorite": "10"
		}
	}
}

with:

{{ state_attr('sensor.test_json_command_line', 'radiateur') }}

I get also good values:

{'rad1': {'nom': 'bureau1', 'pilote': '1', 'puiss': '1000', 'priorite': '10'}, 'rad2': {'nom': 'bureau2', 'pilote': '100', 'puiss': '1500', 'priorite': '10'}}

strange: if I do:

radiateur count: {{ state_attr('sensor.test_json_command_line', 'radiateur')|count}}

I get the good value= 2 and ‘UndefinedError: dict object has no element 0’

Copy-paste the following template into the Template Editor and confirm it reports what you want:

{% set rads = state_attr('sensor.test_json_command_line','radiateur') %}
{% for rad in rads.values() %}
{{ rad }}
{% endfor %}
1 Like

good values:

{'nom': 'bureau1', 'pilote': '1', 'puiss': '1000', 'priorite': '10'}
{'nom': 'bureau2', 'pilote': '100', 'puiss': '1500', 'priorite': '10'}

with your code i can also get sub arguments:

{% set rads = state_attr('sensor.test_json_command_line','radiateur') %}
{% for rad in rads.values() %}
{{ rad.pilote }}
{% endfor %}

give:

1
100

good, I will try to adapt to my needs.
Thanks , many thanks

1 Like