Assistance with Template sensor/entity not working

Hello All,

I am setting up a new sensor/entity following the guide here: [Template](https://www.home-assistant.io/integrations/template/#device_class)

I am using the examples from:

STATE BASED SENSOR MODIFYING ANOTHER SENSOR’S OUTPUT

template:
  - sensor:
      - name: "Sun State"
        state: >
          {% if is_state('sun.sun', 'above_horizon') %}
            up
          {% else %}
            down
          {% endif %}

and:

TATE BASED SENSOR WITH MULTILINE TEMPLATE WITH AN IF TEST

template:
  - sensor:
      - name: "Kettle"
        state: >
          {% if is_state('switch.kettle', 'off') %}
            off
          {% elif state_attr('switch.kettle', 'W')|float < 1000 %}
            standby
          {% elif is_state('switch.kettle', 'on') %}
            on
          {% else %}
            failed
          {% endif %}

This is my code I have in my configurations.yaml:

template:
    - binary_sensor:
        - name: "Water Level"
          state: >
            {% if is_state('binary_sensor.0x00158d0008796f2c_water_leak', 'Wet') %}
                Normal
            {% elif is_state('binary_sensor.0x00158d0008796f2c_water_leak', 'Dry') %}
                Low
            {% else %}
                failed
            {% endif %}

I have tried just sensor instead of binary_sensor with same results.

My issue is that no matter if the sensor is Wet or Dry it just stays “Normal” . I am trying to get this setup/working so I can view the state of the device more accurately for what it is being used for.

All of the guides and previous forum post I could find reference the old legacy way of writing this out. I am trying to use the new standard way. I can’t seem to get this to report correctly. I am hoping that someone on this forum is able to see what I am doing wrong. :slight_smile:

I just wanted to update everyone and leave this here for future people.

I changed my device to show as power. This gave me the on and off state values.

I then changed my yaml as follows:

template:
    - sensor:
        - name: "Water Level"
          state: >
            {% if is_state('binary_sensor.0x00158d0008796f2c_water_leak', 'on') %}
                Normal
            {% elif is_state('binary_sensor.0x00158d0008796f2c_water_leak', 'off') %}
                Low
            {% else %}
                failed
            {% endif %}

After applying this and restarting the yaml configuration. It started working!

Your binary_sensor will always have states of 'on' or 'off'. The front end might map those to different “friendly terms” like “Wet” and “Dry” depending on the device class selected, but for testing the state you must use the underlying 'on'/'off' true states.

Your first example tried to use the friendly terms; and then tried to set three possible states for a binary sensor.