Two if statement under switch :: on_turn_on

Hello,

I am new to ESP home before used Tasmota but since it not support local automatization i need to switch to ESPhome.

I am trying to achieve the following in this section:

  • IF “GA_Kazan_Relay_4” is ON “GA_Kazan_Relay_1” should not turn ON
  • “GA_Kazan_Relay_1” only can turned ON if at least “GA_Kazan_Relay_2” or “GA_Kazan_Relay_3” is ON
switch:
# GAS furnes 
  - platform: gpio
    pin: GPIO2
    inverted: true
    name: "GA_Kazan_Relay_1"
    id:   "GA_Kazan_Relay_1"
    on_turn_on:
      - if: #R1: if [4] is ON [1] must be OFF
        condition:
          switch.is_on: GA_Kazan_Relay_4
      then:
        switch.turn_off: GA_Kazan_Relay_1
      - if:  #R3: [1] is only can turned ON if at least [2] or [3] is ON
        condition:
          switch.is_of: GA_Kazan_Relay_2
          switch.is_of: GA_Kazan_Relay_3
      then:
        switch.turn_off: GA_Kazan_Relay_1     
    

2022_09_17_08_55_38_EspHOME_Home_Assistant

If i only has one if statement there is no error:

switch:
# GAS furnes 
  - platform: gpio
    pin: GPIO2
    inverted: true
    name: "GA_Kazan_Relay_1"
    id:   "GA_Kazan_Relay_1"
    on_turn_on:
      if: #R1: if [4] is ON [1] must be OFF
        condition:
          switch.is_on: GA_Kazan_Relay_4
        then:
          switch.turn_off: GA_Kazan_Relay_1

I wonder what should i do diferently?

Thank you

Seems that indenting is wrong for the ‘then’ parts.
Also the second ‘if’ depends on 2 conditions, use an ‘or’ and check for ‘is_off’ instead of ‘is_of’.

Try this:

switch:
# GAS furnes 
  - platform: gpio
    pin: GPIO2
    inverted: true
    name: "GA_Kazan_Relay_1"
    id:   "GA_Kazan_Relay_1"
    on_turn_on:
      - if: #R1: if [4] is ON [1] must be OFF
          condition:
            switch.is_on: GA_Kazan_Relay_4
          then:
            switch.turn_off: GA_Kazan_Relay_1
      - if:  #R3: [1] is only can turned ON if at least [2] or [3] is ON
          condition:
            or:
              - switch.is_off: GA_Kazan_Relay_2
              - switch.is_off: GA_Kazan_Relay_3
          then:
            switch.turn_off: GA_Kazan_Relay_1
1 Like

Thank you,

I only needed to change the or to and statement. It is doing what i want to do so that is god. To only problem is the status is not updated in homeasistant correctly :frowning: .

In during turn_on i switching off the switch but in the GUI it show as turned on.

I am trying to update manually the status with:
- lambda: "id(GA_Kazan_Relay_1).publish_state(false);"

but that just make thing worse leading to crash ESPHOME.

# GAS furnes 
  - platform: gpio
    pin: GPIO2
    inverted: true
    name: "GA_Kazan_Relay_1"
    id:   "GA_Kazan_Relay_1"
    on_turn_on:
      - if: #R1: if [4] is ON [1] must be OFF
          condition:
            switch.is_on: GA_Kazan_Relay_4
          then:
            - switch.turn_off: GA_Kazan_Relay_1
            - lambda: "id(GA_Kazan_Relay_1).publish_state(false);"
      - if:  #R3: [1] is only can turned ON if at least [2] or [3] is ON
          condition:
            and:
              - switch.is_off: GA_Kazan_Relay_2
              - switch.is_off: GA_Kazan_Relay_3
          then:
            - switch.turn_off: GA_Kazan_Relay_1 
            - lambda: "id(GA_Kazan_Relay_1).publish_state(false);"

I wonder what can i do here?

I have no experience with lambda calls. But perhaps a little delay before turn_off might help for getting the state correctly in HA?

1 Like

Thank you

that do the trick.

1 Like