Virtual switch for disabling binary sensor in esphome

I have water leak sensor and electric control valve.
It works, in HA it looks like on attached picture. ESP8266 automatically close valve if water leak detected, and notify HA.
water
I want to add new feature. I want to have ability to disable water leak sensor for manual only operation.
I think it must be additional switch like “Enable/Disable water leak sensor” but I have not idea how to implement it in esphome. It also must work without connection to HA.
I have only one idea.
New switch will set value for global variable. And inside binary sensor I will analyze value of that global variable. If value represents sensor off value I have to skip scrip execution in binary_sensor.

Maybe there is better way?

My current esphome configuration below:

switch:
  - platform: gpio
    pin: GPIO2 #led pin
    id: WaterBathroomVirtualGPIO
    name: "WaterBathroom Open" 
    restore_mode: RESTORE_DEFAULT_ON 
    on_turn_on: #low level, no water
      then:
        - script.stop: water_close
        - script.execute: water_open
    on_turn_off:
      then:
        - script.stop: water_open
        - script.execute: water_close
    
#M1 for hot water
  - platform: gpio
    pin: D3
    id: M1_IN1
    name: "WaterBathroom_M1_IN1"  
  - platform: gpio
    pin: D2
    id: M1_IN2
    name: "WaterBathroom_M1_IN2"     
  - platform: gpio
    pin: D5
    id: M1_ENA
    name: "WaterBathroom_M1_ENA"     
#M2 for cold water      
  - platform: gpio
    pin: D6
    id: M2_IN1
    name: "WaterBathroom_M2_IN1"  
  - platform: gpio
    pin: D7
    id: M2_IN2
    name: "WaterBathroom_M2_IN2"     
  - platform: gpio
    pin: D8
    id: M2_ENA
    name: "WaterBathroom_M2_ENA"   

output:
  - platform: esp8266_pwm
    pin: D0
    frequency: 1000 Hz
    id: pwm_output
    min_power: 0.5
    max_power: 0.5
 
# WATER_DET
binary_sensor:
  - platform: gpio
    pin: D1
    name: "Bathroom water leak"
    device_class: moisture   
    on_press: #low level, no water
      then:
        - script.stop: water_open
        - script.execute: water_close
    on_release:
      then:
        - script.stop: water_close
        - script.execute: water_open
    
script:
  - id: water_open
    then:
      - switch.turn_on: M1_IN1
      - switch.turn_off: M1_IN2
      - switch.turn_on: M1_ENA
      - delay: 1s # to avoid inrush current
      - switch.turn_on: M2_IN1
      - switch.turn_off: M2_IN2
      - switch.turn_on: M2_ENA      
      - delay: 10s
      - switch.turn_off: M1_ENA    
      - delay: 1s
      - switch.turn_off: M2_ENA     
      - lambda: |-        
          id(WaterBathroomVirtualGPIO).publish_state(true);
      
  - id: water_close
    then:
      - switch.turn_off: M1_IN1
      - switch.turn_on: M1_IN2
      - switch.turn_on: M1_ENA    
      - delay: 1s
      - switch.turn_off: M2_IN1
      - switch.turn_on: M2_IN2
      - switch.turn_on: M2_ENA         
      - delay: 10s
      - switch.turn_off: M1_ENA
      - delay: 1s
      - switch.turn_off: M2_ENA    
      - lambda: |-            
          id(WaterBathroomVirtualGPIO).publish_state(false);
1 Like

This may be helpful to you: Template Switch — ESPHome

The template switch will be visible to HA and you can use its value to enable/ disable the functionality in esphome itself.

Thank you for solution. Now it looks like this in HA enable
It changes global variable IntSensorEnabled

switch:
  - platform: template
    name: "Enable water sensor"
    turn_on_action:
      - lambda: |-        
          id(IntSensorEnabled) = 1;
    turn_off_action:
      - lambda: |-        
          id(IntSensorEnabled) = 0;

But now I have no idea how to use this variable inside binary sensor

# WATER_DET
binary_sensor:
  - platform: gpio
    pin: D1
    name: "Bathroom water leak"
    device_class: moisture   
    on_press: #low level, no water
      then:
        - script.stop: water_open
        - script.execute: water_close
    on_release:
      then:
        - script.stop: water_close
        - script.execute: water_open

If IntSensorEnabled == 1 binary_sensor must be executed as is, but if
IntSensorEnabled == 0 binary_sensor must skip running any scripts

Watching this one - I’m interested in how you get to the bottom of this. I’m attempting to disable a PIR sensor on_double_click: to turn on a relay and bypass the PIR sensor on <> off.

1 Like

Exactly that, AFAIK, binary_sensor doesn’t have a disable feature, only on or off.

Create a dummy switch.

switch:
  - platform: template        
    name: "Script Control"
    id: script_control
    optimistic: true

binary_sensor:
  - platform: gpio
    pin: D1
    name: "Bathroom water leak"
    device_class: moisture
    on_press:
      then:
        - if:
            condition:
              - switch.is_on: script_control
            then:
            - script.stop: water_open
            - script.execute: water_close
2 Likes

Thanks a lot! Coolie1101, your solution looks the best and it works as I want!
My current script below:

  
switch:
  - platform: gpio
    pin: GPIO2 #led pin
    id: WaterBathroomVirtualGPIO
    name: "WaterBathroom Open" 
    restore_mode: RESTORE_DEFAULT_ON 
    on_turn_on: #low level, no water
      then:
        - script.stop: water_close
        - script.execute: water_open
    on_turn_off:
      then:
        - script.stop: water_open
        - script.execute: water_close
        
  - platform: template
    name: "Enable water sensor"
    id: WaterBathroomSensorEnabled
    optimistic: true

    
#M1 for hot water
  - platform: gpio
    pin: D3
    id: M1_IN1
    name: "WaterBathroom_M1_IN1"  
  - platform: gpio
    pin: D2
    id: M1_IN2
    name: "WaterBathroom_M1_IN2"     
  - platform: gpio
    pin: D5
    id: M1_ENA
    name: "WaterBathroom_M1_ENA"     
#M2 for cold water      
  - platform: gpio
    pin: D6
    id: M2_IN1
    name: "WaterBathroom_M2_IN1"  
  - platform: gpio
    pin: D7
    id: M2_IN2
    name: "WaterBathroom_M2_IN2"     
  - platform: gpio
    pin: D8
    id: M2_ENA
    name: "WaterBathroom_M2_ENA"   

output:
  - platform: esp8266_pwm
    pin: D0
    frequency: 1000 Hz
    id: pwm_output
    min_power: 0.5
    max_power: 0.5
 
# WATER_DET
binary_sensor:
  - platform: gpio
    pin: D1
    name: "Bathroom water leak"
    device_class: moisture   
    on_press: #low level, no water
      then:
        - if:
            condition:
              switch.is_on: WaterBathroomSensorEnabled
            then:  
              - logger.log: "close water"
              - script.stop: water_open
              - script.execute: water_close
            else:
              - logger.log: "skip close water"
    on_release:
      then:
        - if:
            condition:
              switch.is_on: WaterBathroomSensorEnabled
            then:
              - logger.log: "open water"
              - script.stop: water_close
              - script.execute: water_open
            else:
              - logger.log: "skip open water"
    
script:
  - id: water_open
    then:
      - switch.turn_on: M1_IN1
      - switch.turn_off: M1_IN2
      - switch.turn_on: M1_ENA
      - delay: 1s # to avoid inrush current
      - switch.turn_on: M2_IN1
      - switch.turn_off: M2_IN2
      - switch.turn_on: M2_ENA      
      - delay: 10s
      - switch.turn_off: M1_ENA    
      - delay: 1s
      - switch.turn_off: M2_ENA     
      - lambda: |-        
          id(WaterBathroomVirtualGPIO).publish_state(true);
      
  - id: water_close
    then:
      - switch.turn_off: M1_IN1
      - switch.turn_on: M1_IN2
      - switch.turn_on: M1_ENA    
      - delay: 1s
      - switch.turn_off: M2_IN1
      - switch.turn_on: M2_IN2
      - switch.turn_on: M2_ENA         
      - delay: 10s
      - switch.turn_off: M1_ENA
      - delay: 1s
      - switch.turn_off: M2_ENA    
      - lambda: |-            
          id(WaterBathroomVirtualGPIO).publish_state(false);
1 Like

Hey, I have a related question/problem but for a light.binary
I would like to have a template switch that, if turned off, disables the light component, i.e. one (and also HA) cannot switch the light on/off.

However, on_press doesn’t work within light.binary - only on_turn_on / on_turn_off / on_state are available.

Anyone got an idea how to implement this?