Struggling with JSON and using If else

I have been playing with this for a couple of days now and its testing me…

I am really new to Home Assistant, but i always like to give it ago. I have a aftermarket hot tub controller that can be controlled via a web interface, i have ran a packet capture to find the commands it needs to write settings to the unit, that i have working just fine. What i am struggling with is to read the JSON and use the result in an if else scenario.

The JSON i get back from the unit is:

{ "temperature": 31, "waterpump": 1, "temperature_set": 38, "airpump": 0, "heaters": 0, "heater1": 0, "heater2": 0, "override": 130, "errorcode": "NONE", "last_swser1": "0", "last_swser2": "0", "or_waterpump": "2", "or_heater": "0", "code_ver": "10.5", "ssid": "Area12", "autoupdate": "1", "autoreboot": "1", "hwversion": "3", "sendreceive": "1", "mqttdestination": "0.0.0.0", "mqttuser": "secretuser", "mqttpass": "secret", "homeassistant": "0.0.0.0", "discovery_mode": "0", "or_air": "0" } 

I am using the following Rest sensor:

- platform: rest
  name: hottub_controller
  resource_template: http://192.168.10.167/stat
  timeout: 15
  scan_interval: 60
  json_attributes:
    - temperature
    - temperature_set
    - waterpump
    - heaters
    - airpump
  value_template: "OK"

- platform: template
  sensors:
    hottub_current_temp:
      value_template: '{{ states.sensor.hottub_controller.attributes["temperature"] }}'
      device_class: temperature
      unit_of_measurement: "°C"
      friendly_name: "Current Temp"

    hottub_target_temp:
      value_template: '{{ states.sensor.hottub_controller.attributes["temperature_set"] }}'
      device_class: temperature
      unit_of_measurement: "°C"
      friendly_name: "Target Temp"

    hottub_filter_pump:
      friendly_name: "Filter Pump"
      value_template: >
        {% if (states('sensor.hottub_controller.attributes["waterpump"]')) == "0" %}
          Off
        {% elif (states('sensor.hottub_controller.attributes["waterpump"]')) == "1" %}
          On          
        {% else %}
          Unknown
        {% endif %}

I can read the JSON and display the results with the first two sensors, the issue i have is that teh hottub_filter_pump sensor wont use the result of a 0 or 1 and then print yes if its a 1 or no if its a 0.

What am i missing?

      value_template: >
        {% if is_state_attr('sensor.hottub_controller', 'waterpump', '0') %}
          Off
        {% elif is_state_attr('sensor.hottub_controller', 'waterpump', '1') %}
          On          
        {% else %}
          Unknown
        {% endif %}

By the way, you can create all these sensors with one call to the resource, without needing template sensors by using the rest integration rather than the rest sensor platform. See:

Just use the value_templates for each sensor instead of attributes.

Also see the warning box at the bottom of this section: https://www.home-assistant.io/docs/configuration/templating/#states So like this:

value_template: "{{ states_attr('sensor.hottub_controller', 'temperature') }}"

Thanks Tom.

This unfortunately doesn’t work either It always reports back with Unknown, i had tried this already, i have tried so much i cant remember where i started.

This is the output from the developer tools

    -  platform: template
       sensors:
         hottub_filter_pump:
           friendly_name: "Filter Pump"
             value_template: >
               
                Unknown

I will have a read on the rest integration. thanks for the pointer.

Try these two in the template editor:

{{ is_state_attr('sensor.hottub_controller', 'waterpump', '0') }}
{{ is_state_attr('sensor.hottub_controller', 'waterpump', 0) }}

Do either of them return true?

No both false:

False
False

Hang on, I just realised the pump is running so its reporting a 1.

So changed your code to:

{{ is_state_attr('sensor.hottub_controller', 'waterpump', '1') }}
{{ is_state_attr('sensor.hottub_controller', 'waterpump', 1) }}

and i got this back:

False
True
1 Like

Tom,

Thank you!! It now works correctly:

    -  platform: template
       sensors:
         hottub_filter_pump:
           friendly_name: "Filter Pump"
             value_template: >
               {% if is_state_attr('sensor.hottub_controller', 'waterpump', 0) %}
                Off
               {% elif is_state_attr('sensor.hottub_controller', 'waterpump', 1) %}
                On          
               {% else %}
                Unknown
               {% endif %}

This now returns:

    -  platform: template
       sensors:
         hottub_filter_pump:
           friendly_name: "Filter Pump"
             value_template: >
               
                On