Template sensor Unavailable but working in developer tools

Hi Guys,

Please could i have some assistance.

I am trying to create a template senor that will return “On” if a physical sensors value is between 2 values set by input_numbers or “Off” if it falls outside. The template I have works in developer tools > Template but the sensor’s state appears as unavailable.

Here’s what i have tried:

binary_sensor:
    - platform: template
      sensors:
        res1_safe:
            friendly_name: "Reservoir 1 pH Safe"
            value_template: >
                {% set upper = states('input_number.reservoir_1_ph_max') | float %}
                {% set lower = states('input_number.reservoir_1_ph_min') | float %}
                {% set current = states('sensor.reservoir_1_ph') | float %}
                {{ current >= lower and current <= upper}}

any help greatly appreciated!

check your log for errors. There’s only 2 reasons why this wouldn’t work:

  • You have 2 binary_sensor sections in configuration.yaml

or

  • {% set current = states('sensor.reservoir_1_ph') | float %} is causing an error because you aren’t supplying a default to float.
1 Like

thank you!!!

this worked, all 3 floats needed the default:

binary_sensor:
    - platform: template
      sensors:
        res1_safe:
            friendly_name: "Reservoir 1 pH Safe"
            value_template: >
                {% set upper = states('input_number.reservoir_1_ph_maximum') | float(default=0) %}
                {% set lower = states('input_number.reservoir_1_ph_minimum') | float(default=0) %}
                {% set current = states('sensor.reservoir_1_ph') | float(default=0) %}
                {{ current >= lower and current <= upper}}  

this is strange though…

my config is copied from a working live HA up to date env running on a Hyper V platform, the only difference now is this HA env is running directly on a physical x64 based machine… previously it never cared if floats had defaults or not but now it does!

Thanks anyway problem solved :smiley: