Create a variable representing state of 3 switches with if function

Hi,

I have 3 binary sensors and I would like to create a variable with the number 0 when all 3 are off, 1 when vent 1 is on and the other 2 are off, 2 when vent 1 and 2 are on and vent 3 is off and 3 when vent 1 and 3 are on and vent 2 is off.

Thanks if you can help.

sensor:
  - platform: template
    sensors:
      combined_switch:
        friendly_name: "My Combined Switch"
        value_template: >-
          {%- set a = is_state('binary_sensor.vent_1', 'on') -%}
          {%- set b = is_state('binary_sensor.vent_2', 'on') -%}
          {%- set c = is_state('binary_sensor.vent_3', 'on') -%}
          {%- if a and not b and not c -%} 1
          {%- elif a and b and not c -%} 2
          {%- elif a and c and not b -%} 3
          {%- else -%} 0
          {%- endif -%}

This will also be 0 if all 3 are on. You didn’t cover that case.

Thanks it works :slight_smile: