Water Tank High, Half, Low! Multiple binary sensors one template sensor issues

Scenario.
I have two two binary sensors with ON or OFF statuses reporting the water level on a tank.

I’d like two write a template sensor to combine the states of those individual binary sensors to a single sensor output.

Formula:
Sensor one ON and sensor two ON = Tank High
Sensor one OFF and sensor two ON = Tank Half
Sensor one OFF and sensor two OFF = Tank Low

Ive tried this to no avail…

sensors:
  water_tank_level:
    friendly_name: "Water Tank"
    value_template: >-
      {% if is_state('sensor.binary_sensor.aeon_labs_dsb45_water_sensor_sensor_3', 'on' ) %}
          Full
      {% elif is_state('sensor.binary_sensor.aeon_labs_dsb45_water_sensor_sensor_3', 'off' ) %}
          Half Full
      {% elif is_state('sensor.binary_sensor.aeon_labs_dsb45_water_sensor_sensor_4', 'off' ) %}
           Water Tank Low
      {% endif %}

Any help greatly appreciated!

Try this:

sensors:
  water_tank_level:
    friendly_name: "Water Tank"
    entity_id:
      - binary_sensor.aeon_labs_dsb45_water_sensor_sensor_3
      - binary_sensor.aeon_labs_dsb45_water_sensor_sensor_4
    value_template: >-
      {% set top = states('binary_sensor.aeon_labs_dsb45_water_sensor_sensor_3') %}
      {% set bottom = states('binary_sensor.aeon_labs_dsb45_water_sensor_sensor_4') %}
      {% if top == 'on' and bottom == 'on' %} Full
      {% elif top == 'off' and bottom == 'on' %} Half
      {% elif top == 'off' and bottom == 'off' %} Low
      {% else %} Error
      {% endif %}

For this template, I chose to use temporary variables (as stand-ins for the long-winded sensor names) to help make reading the logic easier.


EDIT

  • Removed extraneous word sensor. Should’ve caught that one!
  • Added entities to be monitored and the missing % symbols.
1 Like

Really excited to get your reply!
Getting the idea of whats trying to be achieved.

It turns out “sensor” wasnt needed in the binary_sensor ID as I’ve written a standard template sensor with two outcomes and it worked with “binary_sensor.aeon_labs_dsb45_water_sensor_sensor_4”

So modified the code:

  • platform: template
    sensors:
    water_tank_level:
    friendly_name: “Water Tank”
    icon_template: mdi:cup-water
    value_template: >-
    {% set top = states(‘binary_sensor.aeon_labs_dsb45_water_sensor_sensor_3’) %}
    {% set bottom = states(‘binary_sensor.aeon_labs_dsb45_water_sensor_sensor_4’) %}
    {% if top == ‘on’ and bottom == ‘on’} Full
    {% elif top == ‘off’ and bottom == ‘on’} Half
    {% elif top == ‘off’ and bottom == ‘off’} Low
    {% else %} Error
    {% endif %}

But I get this error on checking the config:

Invalid config for [sensor.template]: expected dictionary for dictionary value @ data[‘sensors’]. Got None. (See ?, line ?). Please check the docs at Template - Home Assistant

Comment the lot out and the config file works again…

My apologies. I cranked that one out a little too fast. I corrected the syntax errors in my previous post but it still needs some fine-tuning.

Have replied with formatted code and now get a different error message!
Dunno why its different to before as I just pasted in the suggested text again…
This is directly from my configuration file.

  - platform: template
    sensors:
      water_tank_level:
        friendly_name: "Water Tank"
        value_template: >-
          {% set top = states('binary_sensor.aeon_labs_dsb45_water_sensor_sensor_3') %}
          {% set bottom = states('binary_sensor.aeon_labs_dsb45_water_sensor_sensor_4') %}
          {% if top == 'on' and bottom == 'on'} Full
          {% elif top == 'off' and bottom == 'on'} Half
          {% elif top == 'off' and bottom == 'off'} Low
          {% else %} Error
          {% endif %}

But get a new error:

Configuration invalid

Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: unexpected ‘}’) for dictionary value @ data[‘sensors’][‘water_tank_level’][‘value_template’]. Got “{% set top = states(‘binary_sensor.aeon_labs_dsb45_water_sensor_sensor_3’) %} {% set bottom = states(‘binary_sensor.aeon_labs_dsb45_water_sensor_sensor_4’) %} {% if top == ‘on’ and bottom == ‘on’} Full {% elif top == ‘off’ and bottom == ‘on’} Half {% elif top == ‘off’ and bottom == ‘off’} Low {% else %} Error {% endif %}”. (See ?, line ?). Please check the docs at Template - Home Assistant

Look at the revised version of what I posted previously. You’ll see I corrected several syntax errors. It passes Config Check now but while testing it it doesn’t report anything other than Error so I have to track down what silly thing is causing it. May take awhile because I have to step out right now.

Got it. My test version, which uses input_booleans instead of binary_sensors was configured incorrectly. The revised posted version (using your binary_sensors) should work for you.

You’re the man! Thank you so much.

1 Like