Condition never triggers

Hi there,

I’m struggling with this simple condition which never fires:

  - platform: gpio
    name: "SSR Pumpe"
    id: pump
    pin:
      sn74hc595: sn74hc595_hub
      number: 0
      inverted: false
    on_turn_on:  
      then:  
        if:
          condition:
            - switch.is_off: v1
          then: 
            - logger.log: "Valve 1 is off!"
            - switch.turn_off: pump

It’s a fairly easy part of my script but I don’t get this to work. It’s the code for my DIY Triac-based irrigation controller.

That’s the complete code and everything else is working fine:

esphome:
  name: bewaesserung
  platform: ESP8266
  board: d1_mini
  on_boot: 
    then:
      - switch.turn_off: v1
      - switch.turn_off: v2
      - switch.turn_off: v3
      - switch.turn_off: v4
      - switch.turn_off: v5
      - switch.turn_off: pump
      - sensor.rotary_encoder.set_value:
          id: enc
          value: 0


# Enable logging
logger: 
  baud_rate: 0

# Enable Home Assistant API
api:

ota:
  password: !secret ota_password

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Bewaesserung Fallback Hotspot"
    password: !secret fallback_password

captive_portal:

time:
  - platform: homeassistant
    id: homeassistant_time

sn74hc595:
  - id: 'sn74hc595_hub'
    data_pin: D5
    clock_pin: D6
    latch_pin: D7
    oe_pin: D2
    sr_count: 1
    


# Individual outputs
switch:
  - platform: gpio
    name: "Ventil Ost"
    id: v1
    pin:
      sn74hc595: sn74hc595_hub
      # Use pin number 1
      number: 1
      inverted: false
    interlock: [v2,v4,v5]
    on_turn_on:
      - switch.turn_on: pump
      - delay: 10s
      - switch.turn_off: v1

  - platform: gpio
    name: "Ventil West"
    id: v2
    pin:
      sn74hc595: sn74hc595_hub
      # Use pin number 2
      number: 2
      inverted: false
    interlock: [v1,v4,v5]
    on_turn_on:
      - switch.turn_on: pump
      - delay: 10s
      - switch.turn_off: v2

  - platform: gpio
    name: "Ventil Beet"
    id: v3
    pin:
      sn74hc595: sn74hc595_hub
      # Use pin number 3
      number: 3
      inverted: false
    interlock: [v2]
    on_turn_on:
      - switch.turn_on: pump
      - delay: 10s
      - switch.turn_off: v3

  - platform: gpio
    name: "Ventil Vorgarten"
    id: v4
    pin:
      sn74hc595: sn74hc595_hub
      # Use pin number 2
      number: 4
      inverted: false
    interlock: [v1,v2]
    on_turn_on:
      - switch.turn_on: pump
      - delay: 10s
      - switch.turn_off: v4

  - platform: gpio
    name: "Ventil Einfahrt"
    id: v5
    pin:
      sn74hc595: sn74hc595_hub
      # Use pin number 1
      number: 5
      inverted: false
    interlock: [v2,v5]
    on_turn_on:
      - switch.turn_on: pump
      - delay: 10s
      - switch.turn_off: v5

  - platform: gpio
    name: "SSR Pumpe"
    id: pump
    pin:
      sn74hc595: sn74hc595_hub
      number: 0
      inverted: false
    on_turn_on:  
      then:  
        if:
          condition:
            - switch.is_off: v1
          then: 
            - logger.log: "Valve 1 is off!"
            - switch.turn_off: pump
      

      
      
spi:
  clk_pin: D0
  mosi_pin: D1


globals: 
  - id: setpage #Einstellmodus
    type: bool
    restore_value: no
    initial_value: 'false'
  
  - id: setvalve #
    type: int
    restore_value: no
    initial_value: '0'

  

display:
  - platform: max7219
    cs_pin: D3
    num_chips: 1
    update_interval: 300ms
    lambda: |-
      // Page 5 Uhrzeit anzeigen
      if ((id(enc).state == 5) && (id(setpage) == false)) {
        it.print("        ");
        it.strftime("%H.%M.%S", id(homeassistant_time).now());
      }

      // Page 4 Wifi Level
      if ((id(enc).state == 4) && (id(setpage) == false)) {
        it.print("        ");
        it.printf("Wi %.0fdB", id(wlan_signal).state);
      }

      // Page 1 Wasserstand Höhe
      if ((id(enc).state == 1) && (id(setpage) == false)) {
        it.print("        ");
        it.printf("FH %.1fcn", id(zisterne_cm).state);
          }

      // Page 2 Wasserstand Liter
      if ((id(enc).state == 2) && (id(setpage) == false)) {
        it.print("        ");
        it.printf("FS %.0fL", id(zisterne_liter).state);
          }

      // Page 3 Wasserverbrauch heute
      if ((id(enc).state == 3) && (id(setpage) == false)) {
        it.print("        ");
        it.printf(1,"= %.0f L", id(zisterne_delta_heute).state);

          }      

      // Page 0 Status Ventile     
      if ((id(enc).state == 0) && (id(setpage) == false)) {
        it.print("        ");
          if ((id(v1).state)) {
            it.print("o");
            } else {
            it.print("_");
              }
              
          if ((id(v2).state)) {
            it.print(1,"o");
            } else {
            it.print(1,"_");
              }
          
          if ((id(v3).state)) {
            it.print(2,"o");
            } else {
            it.print(2,"_");
              }
          
          if ((id(v4).state)) {
            it.print(3,"o");
            } else {
            it.print(3,"_");
              }
          
          if ((id(v5).state)) {
            it.print(4,"o");
            } else {
            it.print(4,"_");
              }
          if ((id(pump).state)) {
            it.print(6,"P");
            } else {
            it.print(6,"_");
              }
          }      
      
      // Erste Seite Setup-Page
      if (id(setpage) == true && id(enc).state == 0) {
        it.print("       S");
        if ((id(v1).state)) {
          it.print("o");
          } else {
          it.print("_");
            }
            
        if ((id(v2).state)) {
          it.print(1,"o");
          } else {
          it.print(1,"_");
            }
        
        if ((id(v3).state)) {
          it.print(2,"o");
          } else {
          it.print(2,"_");
            }
        
        if ((id(v4).state)) {
          it.print(3,"o");
          } else {
          it.print(3,"_");
            }
        
        if ((id(v5).state)) {
          it.print(4,"o");
          } else {
          it.print(4,"_");
            }
        if ((id(pump).state)) {
          it.print(6,"P");
          } else {
          it.print(6,"_");
            }

      }




      // Ventil 1 einstellen 

      if (id(setpage) == true && id(enc).state == 1) {


        if ((id(v1).state)) {
          it.print("o.");
          } else {
          it.print("_.");
            }
          
        if ((id(v2).state)) {
          it.print(1,"o");
          } else {
          it.print(1,"_");
            }
          
        if ((id(v3).state)) {
          it.print(2,"o");
          } else {
          it.print(2,"_");
            }

        if ((id(v4).state)) {
          it.print(3,"o");
          } else {
          it.print(3,"_");
            }
              
        if ((id(v5).state)) {
          it.print(4,"o");
          } else {
          it.print(4,"_");
            }
               
          }  // Ende V1  

        // Ventil 2 einstellen 

        if (id(setpage) == true && id(enc).state == 2) {

    
          if ((id(v1).state)) {
            it.print("o");
            } else {
            it.print("_");
              }
            
          if ((id(v2).state)) {
            it.print(1,"o.");
            } else {
            it.print(1,"_.");
              }
            
          if ((id(v3).state)) {
            it.print(2,"o");
            } else {
            it.print(2,"_");
              }
  
          if ((id(v4).state)) {
            it.print(3,"o");
            } else {
            it.print(3,"_");
              }
                
          if ((id(v5).state)) {
            it.print(4,"o");
            } else {
            it.print(4,"_");
              }
              
          }

          if (id(setpage) == true && id(enc).state == 3) {

    
            if ((id(v1).state)) {
              it.print("o");
              } else {
              it.print("_");
                }
              
            if ((id(v2).state)) {
              it.print(1,"o");
              } else {
              it.print(1,"_");
                }
              
            if ((id(v3).state)) {
              it.print(2,"o.");
              } else {
              it.print(2,"_.");
                }
    
            if ((id(v4).state)) {
              it.print(3,"o");
              } else {
              it.print(3,"_");
                }
                  
            if ((id(v5).state)) {
              it.print(4,"o");
              } else {
              it.print(4,"_");
                }
                
            }

            if (id(setpage) == true && id(enc).state == 4) {

    
              if ((id(v1).state)) {
                it.print("o");
                } else {
                it.print("_");
                  }
                
              if ((id(v2).state)) {
                it.print(1,"o");
                } else {
                it.print(1,"_");
                  }
                
              if ((id(v3).state)) {
                it.print(2,"o");
                } else {
                it.print(2,"_");
                  }
      
              if ((id(v4).state)) {
                it.print(3,"o.");
                } else {
                it.print(3,"_.");
                  }
                    
              if ((id(v5).state)) {
                it.print(4,"o");
                } else {
                it.print(4,"_");
                  }
                  
              }
              if (id(setpage) == true && id(enc).state == 5) {

    
                if ((id(v1).state)) {
                  it.print("o");
                  } else {
                  it.print("_");
                    }
                  
                if ((id(v2).state)) {
                  it.print(1,"o");
                  } else {
                  it.print(1,"_");
                    }
                  
                if ((id(v3).state)) {
                  it.print(2,"o");
                  } else {
                  it.print(2,"_");
                    }
        
                if ((id(v4).state)) {
                  it.print(3,"o");
                  } else {
                  it.print(3,"_");
                    }
                      
                if ((id(v5).state)) {
                  it.print(4,"o.");
                  } else {
                  it.print(4,"_.");
                    }
                    
                }        

sensor:
  - platform: rotary_encoder
    name: "Rotary Encoder"
    id: enc
    publish_initial_value: true
    pin_a: 
      number: TX
      inverted: true
      mode:
        input: true
        pullup: true
    pin_b: 
      number: RX
      inverted: true
      mode:
        input: true
        pullup: true
    max_value: 5
    min_value: 0

  - platform: homeassistant
    id: zisterne_liter
    entity_id: sensor.zisterne_liter
  
  - platform: homeassistant
    id: zisterne_delta_heute
    entity_id: sensor.zisterne_delta_heute
    
  - platform: homeassistant
    id: zisterne_cm
    entity_id: sensor.wasserstandraw # Notabschaltung niedriger Wasserstand
    on_value_range:
      - below: 71
        then:
          - switch.turn_off: v1
          - switch.turn_off: v2
          - switch.turn_off: v3
          - switch.turn_off: v4
          - switch.turn_off: v5
          - switch.turn_off: pump

  - platform: wifi_signal
    name: "Bewaesserung ESP Wifi Signal"
    update_interval: 30s
    id: wlan_signal
      

binary_sensor:
  - platform: gpio
    id: encswitch
    pin: 
      number: GPIO2
      mode: INPUT_PULLUP
      inverted: True 
    on_click:
    - min_length: 50ms
      max_length: 250ms
      then: 
        - lambda: |-
            if(id(setpage)) {
              id(setpage) = false; 
            } else {
              id(setpage) = true;
              //id(enc).state = 0;  

            } 

    - min_length: 800ms
      max_length: 1500ms
      then:
        - if:
            condition:
              lambda: 'return (id(setpage) == true && id(enc).state == 1);'
            then:
              switch.toggle: v1
             


        - if:
            condition:
              lambda: 'return (id(setpage) == true && id(enc).state == 2);'
            then:
              switch.toggle: v2              
        - if:
            condition:
              switch.is_on: v2
            then:
              - delay: 5s
              - switch.turn_off: v2
          
        - if:
            condition:
              lambda: 'return (id(setpage) == true && id(enc).state == 3);'
            then:
              switch.toggle: v3    
        - if:
            condition:
              switch.is_on: v3
            then:
              - delay: 5s
              - switch.turn_off: v3

        - if:
            condition:
              lambda: 'return (id(setpage) == true && id(enc).state == 4);'
            then:
              switch.toggle: v4             
        - if:
            condition:
              switch.is_on: v4
            then:
              - delay: 5s
              - switch.turn_off: v4    
                        
        - if:
            condition:
              lambda: 'return (id(setpage) == true && id(enc).state == 5);'
            then:
              switch.toggle: v5 
        - if:
            condition:
              switch.is_on: v5
            then:
              - delay: 5s
              - switch.turn_off: v5


    - min_length: 3000ms # alle ventile aus
      max_length: 60000ms
      then:
        - switch.turn_off: v1
        - switch.turn_off: v2
        - switch.turn_off: v3
        - switch.turn_off: v4
        - switch.turn_off: v5
        - switch.turn_off: pump

Not really sure what you are trying to do but maybe

    on_turn_on:  
      then:  
        if:
          condition:
            - switch.is_off: v1
          then: 
            - logger.log: "Valve 1 is off!"
            - switch.turn_off: pump
          else:
            - switch.turn_on:
                id: pump

Thank you for your suggestion. Unfortunately, it won’t work.

The goal is to switch off the pump if all valves are off. If limited it to one valve (v1) for now, but the pump switch keeps enabled.

    on_turn_on:  
      then:  
        if:
          condition:
            or:
              - switch.is_on: v1
              - switch.is_on: v2
              - switch.is_on: v3
              - switch.is_on: v4
              - switch.is_on: v5
          then: 
            - logger.log: "Valve 1 is ON!"
            - switch.turn_on: pump
          else:
            - switch.turn_off:
                id: pump

also check this on turn on then

switch:
  - platform: gpio
    name: "Ventil Ost"
    id: v1
    pin:
      sn74hc595: sn74hc595_hub
      # Use pin number 1
      number: 1
      inverted: false
    interlock: [v2,v4,v5]
    on_turn_on:
      then:
        - switch.turn_on: pump
        - delay: 10s
        - switch.turn_off: v1

I tried all that before posting here unsuccessfully. I don’t get it …
But thanks anyway for your support, Blacky!

Last one and someone else can have a go… remove the turn on under pump and try it under valve.

switch:
  - platform: gpio
    name: "Ventil Ost"
    id: v1
    pin:
      sn74hc595: sn74hc595_hub
      # Use pin number 1
      number: 1
      inverted: false
    interlock: [v2,v4,v5]
    on_turn_on:
      then:
        - switch.turn_on: pump
        - delay: 10s
        - switch.turn_off: pump
        - switch.turn_off: v1

Yes, this would work, but it’s not the goal. In your example, the pump would switch off as soon as v1 switches off. But the pump should only turn off, when the last active valve turns off.

Yea I thought that was what you were trying to do… looks like you know your stuff so I am at the same point as you… the only thing “thinking out loud” would be “encswitch” in a condition…hhmmm… hopefully someone else could jump in with the solution.

Failing that… what we do is have a pressure sensor on the pump that turns it ON. So when pressure drops the pump turns ON and will stay ON until all valve are closed and pressure builds up again turning the pump OFF (you can purchase pumps that have this feature built in and can be stand alone, no code required or you can make one). We also have a main valve so 2 valve must be ON to allow the water to flow so you don’t flood the area (like a safe guard, 2 valves must fail)…

Sorry I couldn’t be of any assistance :pensive:

Then why are you trying to use the on_turn_on trigger?

Try this:

switch:
  - platform: gpio
    name: "Ventil Ost"
    id: v1
    pin:
      sn74hc595: sn74hc595_hub
      # Use pin number 1
      number: 1
      inverted: false
    interlock: [v2,v4,v5]
    on_turn_on:
      - switch.turn_on: pump
      - delay: 10s
      - switch.turn_off: v1
    on_turn_off:
      then:  
        if:
          condition:
            and:
              - switch.is_off: v1 # this is redundant but makes it easy to copy to the other valves.
              - switch.is_off: v2
              - switch.is_off: v3
              - switch.is_off: v4
              - switch.is_off: v5
          then: 
            - switch.turn_off:
                id: pump

This on_turn_off trigger needs to be repeated for all the valves.

2 Likes

We have a pump without pressure sensor, which worked fine with a ZigBee-switch the last few years - so I won’t replace it.

Thank you again for looking at my code! I’ll have a fresh look at it later today. Maybe I’ll have more luck then.

1 Like

You’re totally right, and your suggestions works perfectly!

My wrong assumption was, that I thought, that the conditions in the on_turn_on of the pump part would be checked continuously. Instead, it is only checked at the on_turn_on event and at that point the valves were still on and so the pump never switched off.

I wanted to create a less redundant code instead of having the on_turn_off conditions for all valves in all valve parts.

Thank you very much for your support!

You could create a template binary sensor and automation instead:

binary_sensor:
  - platform: template
    name: "Any Valve On"
    internal: true
    lambda: 'return id(v1).state or id(v2).state or id(v3).state or id(v4).state or id(v5).state ;'
    on_release:
      then:
        - switch.turn_off: pump

This sensor will be on if any valve is on. As soon as all valves are off it switches off the pump.

2 Likes

That’s wonderful! It makes it easier to maintain. Today, I’ve learned a lot about ESPHome.
Really appreciate your support.

Best,
Markus