Json templating help

I have a multiple rest sensor working but I need to convert some returned numbers into the strings they represent. For example the sensor state is returning 1 or 0 but I would like to display this as On or Off.

I’ve been trying for hours without success.

- platform: rest
  name: JSON Lounge Aircon
  json_attributes:
    - state
    - mode
    - temp
    - fan
    - aux
    - roomt
  resource: http://10.1.1.9/json

- platform: template
  sensors:
    state:
      friendly_name: 'State'
      value_template: >
        {% if is_state('{{ states.sensor.json_lounge_aircon.attributes["mode"] }}' '0' ) %}
          'Off'
        {% elif is_state('{{ states.sensor.json_lounge_aircon.attributes["mode"] }}' '1' ) %}
          'On'
        {% endif %}
    mode:
      friendly_name: 'Mode'
      value_template: '{{ states.sensor.json_lounge_aircon.attributes["mode"] }}'
    temp:
      friendly_name: 'Set Temperature'
      value_template: '{{ states.sensor.json_lounge_aircon.attributes["temp"] }}'
    fan:
      friendly_name: 'Fan mode'
      value_template: '{{ states.sensor.json_lounge_aircon.attributes["fan"] }}'
    aux:
      friendly_name: 'Power mode'
      value_template: '{{ states.sensor.json_lounge_aircon.attributes["aux"] }}'
    roomt:
      friendly_name: 'Current Temperature'
      value_template: '{{ states.sensor.json_lounge_aircon.attributes["roomt"] | round(1) }}'
      unit_of_measurement: "°C"

Your syntax is a bit off - no double-curly-quotes required inside the is_state parameter, comma missing between the state and value parameters. I would also simplify the if-statement: Test for one value to evaluate to true, and then just use else for any other value to evaluate to false.
Also, are you sure you want a sensor to contain the values on/off? Wouldn’t a binary template sensor be more suitable? In that case a simple template without if-else would suffice. And lastly, look at is_state_attr for evaluating attributes.

In conclusion (without having tested this syntax) I would propose:

binary_sensor:
- platform: template
  sensors:
    state:
      friendly_name: 'State'
      device_class: cold
      value_template: "{{ is_state_attr('sensor.json_lounge_aircon', 'mode', '1' ) }}"

Thanks for your help.

In the state: sensor case a device_class is a good idea

I tried using the none: device_class to cast the state sensor as a binary_sensor in the customize section. As HA complained about the device_class statement being in the template. However this did not work (state: still returned ‘0’ or ‘1’).

Also some items have more than one state, e.g.

fan: could be:

176=Night mode
160=Auto
48=Speed 1
80=Speed 2
112=Speed 3

and as they are sensors, not binary_sensors the only classes available are:

battery: Percentage of battery that is left.
humidity: Percentage of humidity in the air.
illuminance: The current light level in lx or lm.
temperature: Temperature in °C or °F.

The missing comma was due to it being 3:45am. I really should have stopped earlier.

I am making no progress messing about with the the template editor

'{{ states.sensor.json_lounge_aircon.attributes["mode"] }}'

returns the correct value, 0 or 1 but I can not get this if statement to work. This still returns as unknown:

- platform: template
  sensors:
    state:
      friendly_name: 'State'
      value_template: >
        {% if is_state(states.sensor.json_lounge_aircon.attributes["mode"], '1' ) %}
          'On'
        {% elif is_state(states.sensor.json_lounge_aircon.attributes["mode"], '0' ) %}
          'Off'
        {% endif %}

This:

{% if {{ states.sensor.json_lounge_aircon.attributes["mode"] }} == '1' %}
  'On'
{% elif {{ states.sensor.json_lounge_aircon.attributes["mode"] }} == '0' %}
  'Off'
{% endif %}

errors with

invalid template (TemplateSyntaxError: expected token ‘:’, got ‘}’) for dictionary value @ data[‘sensors’][‘state’][‘value_template’]

This:

{% if states.sensor.json_lounge_aircon.attributes["mode"] == '1' %}
  'On'
{% elif states.sensor.json_lounge_aircon.attributes["mode"] == '0' %}
  'Off'
{% endif %}

returns “unknown”.

So does this:

{% if 'states.sensor.json_lounge_aircon.attributes["mode"]' == '1' %}
  'On'
{% elif 'states.sensor.json_lounge_aircon.attributes["mode"]' == '0' %}
  'Off'
{% endif %}

is_state expect the entity ID of a sensor, so remove the states., but I’m not sure if is_state works with attributes.

No double-curly-quotes.
Sorry, and in your context you have to access the attribute differently:
{% if states.sensor.json_lounge_aircon.attributes.mode == '1' %}

Awsome. Thanks for your help. This works:

- platform: template
  sensors:
    state:
      friendly_name: 'State'
      value_template: >
        {% if states.sensor.json_lounge_aircon.attributes.state == '1' %}
          On
        {% elif states.sensor.json_lounge_aircon.attributes.state == '0' %}
          Off
        {% endif %}