Anyone help? Template sensor - Formula for changing state when above 35!

Hello!
I’ve an arduino analog input via home assistant.

Can someone help with the template sensor to do this??

Any reading from 0 to 34 id like to reflect ON state. At 35 and above I’d like a template sensor to change its state to OFF from ON.

sensor:
  - platform: template
    sensors:
      water_present:
        value_template: >-
          {% if states('sensor.sensor.water_present')|float < 35 }
            ON
          {% else %}
            OFF
          {% endif %}

Any pointers appreciated!

Given that you only want the sensor to have one of two possible states, ON or OFF, make it a Template Binary Sensor.

binary_sensor:
  - platform: template
    sensors:
      water_present:
        value_template: "{{ states('sensor.water_present')|float < 35 }}"

You can optionally add a device_class to enhance the binary_sensor’s appearance in the Lovelace UI.

binary_sensor:
  - platform: template
    sensors:
      water_present:
        value_template: "{{ states('sensor.water_present')|float < 35 }}"
        device_class: moisture

EDIT
Sensor’s entity_id was incorrect and I just blindly copied it.
Changed:
sensor.sensor.water_present
to:
sensor.water_present

1 Like

That was a quick reply, thanks for taking the time!

It worked. I had a blonde moment with a sensor name. I put “sensor.sensor_blah” instead of just “sensor.blah”.
But its working…

Much appreciated 123 :grinning:

1 Like

… and I just blindly copy-pasted the invalid entity_id. :man_facepalming:

I’ve corrected the example above.

BTW, there’s something not quite right about the template sensor in the example. It has the same name (water_present) as the sensor being testing within the template (water_present). One of them should be different, yes?

Working a charm! Thank you