Conditionally turn on the GPIO switch

Good day,

I’m a beginner in HA, I’m migrating devices from Blynk, but I can’t figure out the condition.

I turn on the pump from the HA, when the level drops, the pump turns off.

How do I set it so that when the level is low, the switch will turn on?

I tried everything possible but I keep getting errors.

I have this shutdown code working, but I don’t know how to prevent the switch from turning on when the level is low.

Thank you Pavel

binary_sensor:
  - platform: gpio
    name: "hladina"
    id: hladina
    pin:
      number: GPIO14
    on_release:
      - switch.turn_off: relay_pump

switch:
  ## relay pump
  - platform: gpio
    pin: GPIO5
    name: "Relay pump"  
    id: relay_pump

Is this what you are looking for?

on_...:
  then:
    - if:
        condition:
          # Should return either true or false
          lambda: |-
            return id(some_sensor).state < 30;
        # ...

Or from the sensor measuring the level you trigger a command to turn on and / or off

sensor:
  - platform: dht
    humidity:
      name: "Living Room Humidity"
      on_value_range:
        - above: 65.0
          then:
            - switch.turn_on: dehumidifier1
        - below: 50.0
          then:
            - switch.turn_off: dehumidifier1
    temperature:
      name: "Living Room Temperature"

Hallo,

Thank you, I made progress in the evening, currently

the sensor turns the pump off, after switching on it does not turn it on
this is ok

I currently don’t know how to disable turning on the switch when the sensor is off. the sensor sends me ON/OFF states in the log, I tried ON/OFF, 0/1, true/false in the code. fails, I’m still missing something or have the syntax wrong. I looked at the manuals, but I still can’t figure it out.

Thank you

binary_sensor:
  - platform: gpio
    name: "hladina"
    id: hladina
    pin: 
      number: GPIO14
    on_release:
      - switch.turn_off: relay_pump

switch:
  ## relay pump
  - platform: gpio
    pin: GPIO5
    name: "Relay pump"  
    id: relay_pump
    on_turn_on:
      then:
        - if:
            condition:
              lambda: 'return id(hladina).state == 1;'
            then:
              - switch.turn_on: relay_pump
              - logger.log: "*****on ******"
            else:
              - switch.turn_off: relay_pump
              - logger.log: "***** OFF *****"

I currently have it in a working condition as far as the hardware is concerned, but I still don’t know why after a low level (sensor false) and when the switch is turned on, the switch lights up on the panel, even when the hardware is turned off, as I expect. As if the status was not updated.

2023-01-24_125301

binary_sensor:
  - platform: gpio
    name: "hladina"
    id: hladina
    pin: 
      number: GPIO14
    on_release:
      - switch.turn_off: relay_pump

switch:
  ## relay pump
  - platform: gpio
    pin: GPIO5
    name: "Relay pump"  
    id: relay_pump
    on_turn_on:
      then:
        - if:
            condition:
              lambda: 'return id(hladina).state == true;'
            then:
              - switch.turn_on: relay_pump
            else:
              - switch.turn_off: relay_pump
    on_turn_off:
      - switch.turn_off: relay_pump

Show my ignorance but I think reading the document, automation goes under sensor/ binary sensors and not a switch component ( which is an output and not an input component).

Edit: I read docs and this is correct. I also have to write automation for my pump based on water level. I will look at this tomorrow