ESPHome binary sensors comparing status

Hya,

i have the following code, working. By accessing the web, all shows correct.

binary_sensor:
  # ON/OF push button TASTE_PIN - physical pin
  - platform: gpio
    pin:
      number: $TASTE_PIN
      mode:
        input: true
    name: "Druckknopf"
    id: push_button
    on_press:
      then:
        switch.toggle: relai_ein_aus

  - platform: template
    name: "Relai Status"
    lambda: |-
      return (id(relai_ein_aus).state);

  # HEATER - AC DETECTOR - physical pin
  - platform: gpio
    pin:
      number: $HEIZER_PIN
      inverted: true
      mode:
        input: true
    name: "Heizer Pin"
    id: heizkoerper

  - platform: template
    name: "Heizer Status"
    lambda: |-
      return (id(heizkoerper).state);

  # Summer / Buzzer
  - platform: gpio
    pin:
      number: $SUMMER_PIN
      inverted: false
      mode:
          input: true
    name: "Summer Pin"
    id: summer_pin

  - platform: template
    name: "Summer Status"
    lambda: |-
      return (id(summer_pin).state);

#    - platform: template
#    name: "Eierkocher Status"
#    lambda: |-
    # relai_status
    # heizer_status
    # summer_status
    # if (id(summer_pin).state == ON)

Now i want to take some actions based on gpio (id) status, ON or OFF.

For example, if id(relai_ein_aus).state is ON and id(heizkoerper).state is ON, ok, it’s working, but if id(relai_ein_aus).state is ON and id(heizkoerper).state is OFF, there is an error and i want to switch.toggle: relai_ein_aus.

I’m not finding the right way to do it. How to compare if ON or OFF?

Thx in advance.

Anyone able to advise how it shall be done?

Theres lots of examples here and it specifically covers comparing states.

globals:
   - id: ek3117_status
     type: int
     restore_value: no
     initial_value: '0'

status_led:
  pin:
    number: $STATUS_PIN
    inverted: false
    
binary_sensor:
  # ON/OF push button TASTE_PIN - physical pin
  - platform: gpio
    pin: 
      number: $TASTE_PIN
      mode: 
        input: true
    name: "Druckknopf"
    id: push_button
    on_press:
      then:
        switch.toggle: relai_ein_aus

  - platform: template 
    name: "Relai Status"
    lambda: |-
      return (id(relai_ein_aus).state);

  # HEATER - AC DETECTOR - physical pin
  - platform: gpio
    pin: 
      number: $HEIZER_PIN
      inverted: true
      mode: 
        input: true
    name: "Heizer Pin"
    id: heizkoerper

  - platform: template
    name: "Heizer Status"
    lambda: |-
      return (id(heizkoerper).state);

  # Summer / Buzzer
  - platform: gpio
    pin:
      number: $SUMMER_PIN
      inverted: false
      mode:
          input: true
    name: "Summer Pin"
    id: summer_pin

  - platform: template
    name: "Summer Status"
    lambda: |-
      return (id(summer_pin).state);
      
  - platform: template
    name: "Eierkocher Status"
    lambda: |-
      if (id(relai_ein_aus).state) == false {
        return id(ek3117_status).state = 0;
      }
      if (id(relai_ein_aus).state) == true {
        return id(ek3117_status).state = +1);
      }
      if (id(heizkoerper).state) == true {
        return id(ek3117_status).state =+ 2);
      }
      if (id(summer_pin).state) == true {
        return id(ek3117_status).state =+4);
      }
      return id(ek3117_status).state;

switch:
  # RELAY LOGICAL SWITCH (toggle by ON/OFF push button)
  - platform: output
    id: relai_ein_aus
    name: "Relai Pin"
    output: relai_opin
          
output:
  # RELAY IN physical pin
  - platform: gpio
    pin: $RELAI_PIN
    inverted: false
    id: relai_opin

so, when id(gpio).state = ON or OFF i’ll take actions.

and i get:

Compiling .pioenvs/ek3117-severin/src/main.cpp.o
<unicode string>: In lambda function:
<unicode string>:118:33: error: expected primary-expression before '==' token
<unicode string>:121:31: error: expected primary-expression before '==' token
<unicode string>:124:30: error: expected primary-expression before '==' token
<unicode string>:127:3: error: no return statement in function returning non-void [-Werror=return-type]
cc1plus: some warnings being treated as errors
*** [.pioenvs/ek3117-severin/src/main.cpp.o] Error 1
========================== [FAILED] Took 3.06 seconds ==========================

i really get confused. i just want to compare states and set a variable with a number, fro 0 to 7 depending on each status, for a later case and actions.

But i’m really not finding the way.

You have a problem with the braces for the if statements. All braces are closed before == but the whole test needs to be in braces. If you move the closing brace to before the { it should compile.

Also, some return statements have unmatched braces. They close a brace that was never opened.

Also, are you aware you are both assigning a value and returning the result in one statement? It is easily confused with a compare statement (in which case you would return true or false. If you meant to compare, you would need to use == and not =

If assigment is what you intend, this is best avoided by writing an assignment statement separate from the return statement. It is allowed to do though.

The + in the statement is obsolete. At other places you write “=+ 2”. Here it seems like the + belongs to the = and not the 2. This is also a confusing notation, because += has a special meaning in c++ but =+ has no special meaning. += means you add something to the original value (increment). You now wrote = +2; In that case I would write = 2.

Last but not least, you say the state would be a value from 0 to 7. The way you wrote it, every return statement would stop further execution. The highest value you would get now is 4. If you meant to add values, you would likely start with 0 and use += to add values, not =+ which is just assign a positive number. You would only use return at the end, when the result is complete, and not halfway, before you are done. I would also use a temporary variable and only set the state at the end. Now you’d have many state changes that are possibly incomplete states.