Help with value_template and conditional 'if'

Hi, I need some help with configuring this senson with rest and value_template. In my configuration.yml file:

  - platform: rest
    resource: http://localhost:3000/api/v1/hvac
    json_attributes:
      - data
    name: airzone
    method: POST
    payload: '{"systemID":1,"zoneID":0}'
    scan_interval: 60
    value_template: 'OK'
  - platform: template
    sensors:
      blueface_status:
        friendly_name: Status
        value_template: {{ states.sensor.airzone.attributes.data.0.on }}

{{ states.sensor.airzone.attributes.data.0.on }} show values 0 or 1, depending on the operating status. It’s ok.

Is it possible to use an ‘if’, as an example although it is incorrect, to be able to show “running -> 1” and “stopped -> 0”?

Ex:

        value_template: >-
-->      {% if {{ states.sensor.airzone.attributes.data.0.on }} == '1' %} <--
            Running 
          {% else %}
            Stopped
          {% endif %}

Thks in advance.

Cyn you please show how the sensor looks like in the states page?

Try like this:

value_template: >-
  {% if state_attr('sensor.airzone', 'data')['0']['on'] == 1 %}
     Running 
  {% else %}
     Stopped
  {% endif %}

First thanks for your reply.
I have done the test and it does not work. Do you mean json as an example?

{
	"data":	[{
			"systemID":	1,
			"zoneID":	1,
			"on":	1,
			"maxTemp":	30,
			"minTemp":	15,
			"setpoint":	20.500000,
			"roomTemp":	23.100000,
			"modes":	[1, 1, 4, 2, 3, 5],
			"mode":	3,
			"coldStages":	1,
			"coldStage":	1,
			"heatStages":	2,
			"heatStage":	2,
			"humidity":	61,
			"units":	0,
			"errors":	[]
		}, {
			"systemID":	1,
			"zoneID":	7,
			"on":	0,
			"maxTemp":	30,
			"minTemp":	15,
			"setpoint":	23,
			"roomTemp":	21.100000,
			"coldStages":	0,
			"coldStage":	0,
			"heatStages":	2,
			"heatStage":	2,
			"humidity":	67,
			"units":	0,
			"errors":	[]
		}]
}

Thks again!

No I mean the sensor.airzone when you go to Developer Tools -> States.
Try with removing the single quotes around the 0, I assumed 0 was the name of the dict.

value_template: >-
  {% if state_attr('sensor.airzone', 'data')[0]['on'] == 1 %}
     Running 
  {% else %}
     Stopped
  {% endif %}

try

        value_template: >
          {% set v = states.sensor.airzone.attributes.data[0].on %}
          {% set n = 'Running' if v else 'Stopped' %}
          {{ n }} -> {{ v }}

Hi again,
Your code works ok!! Great!!

As you told me:

data:
  - systemID: 1
    zoneID: 1
    'on': 1
    maxTemp: 30
    minTemp: 15
    setpoint: 20.5
    roomTemp: 20.9
    modes:
      - 1
      - 1
      - 4
      - 2
      - 3
      - 5
    mode: 3
    coldStages: 1
    coldStage: 1
    heatStages: 2
    heatStage: 2
    humidity: 57
    units: 0
    errors: []
  - systemID: 1
    zoneID: 2
    'on': 0
    maxTemp: 30
    minTemp: 15
    setpoint: 24.5
    roomTemp: 19.799999
    coldStages: 1
    coldStage: 1
    heatStages: 2
    heatStage: 2
    humidity: 64
    units: 0
    errors: []
  - systemID: 1
    zoneID: 3
    'on': 0
    maxTemp: 30
    minTemp: 15
    setpoint: 21.5
    roomTemp: 21.299999
    coldStages: 1
    coldStage: 1
    heatStages: 2
    heatStage: 2
    humidity: 60
    units: 0
    errors: []
  - systemID: 1
    zoneID: 4
    'on': 0
    maxTemp: 30
    minTemp: 15
    setpoint: 21.5
    roomTemp: 20.5
    coldStages: 1
    coldStage: 1
    heatStages: 2
    heatStage: 2
    humidity: 61
    units: 0
    errors: []
  - systemID: 1
    zoneID: 5
    'on': 0
    maxTemp: 30
    minTemp: 15
    setpoint: 19.5
    roomTemp: 19.5
    coldStages: 1
    coldStage: 1
    heatStages: 2
    heatStage: 2
    humidity: 69
    units: 0
    errors: []
  - systemID: 1
    zoneID: 6
    'on': 0
    maxTemp: 30
    minTemp: 15
    setpoint: 23
    roomTemp: 18.9
    coldStages: 1
    coldStage: 1
    heatStages: 2
    heatStage: 2
    humidity: 69
    units: 0
    errors: []
  - systemID: 1
    zoneID: 7
    'on': 0
    maxTemp: 30
    minTemp: 15
    setpoint: 23
    roomTemp: 18.200001
    coldStages: 0
    coldStage: 0
    heatStages: 2
    heatStage: 2
    humidity: 70
    units: 0
    errors: []
friendly_name: airzone

But now, works ok.

image

Thks a lot Burningstone!!

Hi Petro!
Thks in advance! I have already try to put your code, and functions ok!

image

Thks a lot!

Glad that it works now :slightly_smiling_face:

Please mark the post that helped you find the solution. This way others can easier find the solution in the future.

Done!!

Thks again!