Templates - extracting a list/array element from a json output

Hi All,
I put the following into the template editor and it works fine. However, when I put it into configuration.yaml (extract below) I get unknown value for each sprinkler. In Python it’s simple, I must be missing something.
Most grateful for any assistance or a better approach to the issue.

Template editor code

{% set my_test_json = {"sn":[1,0,1,0,1,0,1,0],"nstations":8} %}

- platform: rest
    name: sprinkler_states       
    value_template: '{{ my_test_json }}'

  - platform: template 
    binary_sensors:
      sprinkler1: 
        value_template: '{{ my_test_json.sn[0] }}
      sprinkler2: 
        value_template: '{{ my_test_json.sn[1] }}
      sprinkler3: 
        value_template: '{{ my_test_json.sn[2] }}
      sprinkler4: 
        value_template: '{{ my_test_json.sn[3] }}
      sprinkler5: 
        value_template: '{{ my_test_json.sn[4] }}
      sprinkler6: 
        value_template: '{{ my_test_json.sn[5] }}
      sprinkler7: 
        value_template: '{{ my_test_json.sn[6] }}
      sprinkler8: 
        value_template: '{{ my_test_json.sn[7] }}

Configuration.yaml Code

- platform: rest
    name: sprinkler_states
    
    resource: http://x.x.x.x:8080/js?pw=XXX
    value_template: '{{ value_json }}'

  - platform: template 
    sensors:
      sprinkler1: 
        value_template: '{{ value_json.sn[0] }}'
      sprinkler2: 
        value_template: '{{ value_json.sn[1] }}'
      sprinkler3: 
        value_template: '{{ value_json.sn[2] }}'
      sprinkler4: 
        value_template: '{{ value_json.sn[3] }}'
      sprinkler5: 
        value_template: '{{ value_json.sn[4] }}'
      sprinkler6: 
        value_template: '{{ value_json.sn[5] }}'
      sprinkler7: 
        value_template: '{{ value_json.sn[6] }}'
      sprinkler8: 
        value_template: '{{ value_json.sn[7] }}'

Because you aren’t extracting the information from the rest sensor, your extracting from an unknown object named value_json.

try this:

Place the sn object into the attributes, then access that attribute from the states object in the value template. Just take a look at the documentation for rest sensors, this method is one of the examples below.

- platform: rest
    name: sprinkler_states
    resource: http://x.x.x.x:8080/js?pw=XXX
    json_attributes: 
      - sn
    value_template: '{{ value_json }}'
  - platform: template 
    sensors:
      sprinkler1: 
        value_template: "{{ states.sensor.sprinkler_states.attributes['sn'][0] }}"
      ... etc ...
1 Like

A couple further suggestions…

One needs to be careful what gets parsed into the REST sensor’s state since there is a character limit (255 or 256, I can never remember which.) In this case, if the JSON really is that small it probably doesn’t matter. But often it does. So it might be better to use "{{ value_json.nstations }}" or something like that.

I’ve also found that using states.sensor.xxx... in templates can sometimes fool the entity extraction logic. So it might be better in this case to use the state_attr function.

So:

  - platform: rest
    name: sprinkler_states
    resource: http://x.x.x.x:8080/js?pw=XXX
    json_attributes: 
      - sn
    value_template: "{{ value_json.nstations }}"
  - platform: template 
    sensors:
      sprinkler1: 
        value_template: "{{ state_attr('sensor.sprinkler_states', 'sn')[0] }}"
      ... etc ...
1 Like

Yeah, I wasn’t sure what to do with value_template. I’ve never used the rest sensor. Just followed the docs.

I implemented your suggested change and it worked perfectly, Many thanks.
On last question, I want sprinklers 1-8 to to be represent as Off/On on the front end, so I put them under binary_sensor: instead of sensor: for some reason they will only display as Off and won’t recognise a state change. Thanks again

Ok noted. The state_attr works well, I’ll use that instead, thank you

For a binary template sensor, value_template has to return true for the state to be on. Anything else will cause the sensor to be off. Assuming the values can only be the numeric values 0 and 1 you should change it to:

value_template: "{{ state_attr('sensor.sprinkler_states', 'sn')[0] == 1 }}"
1 Like

Ok that worked, it recognises the state change, although it now displays True or False, would prefer On/OFF, I should be able to figure that one out.
Wow I’ve got a lot to learn, it’s a great product very impressed
Thanks again

Are you sure it’s under binary_sensor? It should be displaying on and off by default. Have you set a device class? That controls how a binary_sensor is displayed.

EDIT: Note that the actual state of a binary_sensor is always on or off.

1 Like

My mistake, I had removed binary_sensor. All good now.
Thanks again