Binary Sensor With Latch

Is there a way to make a Binary Sensor act as a latch, so once it’s turned on, it stays on until it’s reset or otherwise changed?

I’m using an emergency shut-off switch that’s an NC pushbutton, so it stays closed until pressed. At that point it’ll be open, but when released, it’ll close again. Is there a way, within ESPHome, to have it latch as open until something is done to change the state?

My first thought is to have the push button (binary sensor) change the state of a separate template switch on_press. The switch is your latch.

Or you could use a template binary sensor as your latch, with the first one changing the state of the second one via a publish state action.

Thanks! I had completely overlooked Templates when I was thinking over possible solutions.

1 Like

Just a note I followed this up, since I’m using a momentary contact pushbutton and debouncing and timing could be an issue. That’s on this thread.

Can’t see why contact bounce is an issue in your application the first open of the contact will trigger the latch immediately, doesn’t matter if you get any further on/off pulse as the latch has already been triggered. You will need another switch to reset but the same applies.

I’d also use a template switch as @Mahko_Mahko suggests with two binary sensors for start/stop

If the first contact closes it and triggers the latch to go on, then the next bounce after that, when it goes off, or another one after that, could just as easily read as off and turn it off. Without debouncing, it’s close to a race condition where it could shift back and forth, on and off, several times - and I wouldn’t even be sure the last “on” will take.

But, even more, I’m dealing with controlling some potentially dangerous equipment like a CNC and an etching laser. I don’t want something like that to be getting non-debounced control feeds.

This is how I would do it, define two buttons one for off the other for on, each has an on_press action that switches the latch either off in case of button one or on in case off button two. Button 1 has no capability to switch the latching switch on regardless if it is pressed multiple times or bounces 100 times.

The template switch could easily be a real GPIO driving a relay.

PS I have picked the GPIOs at random check they are suitable some can’t be used and some toggle during boot.

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO13
      mode:
        input: true
        pullup: true
    name: button1_Off
    on_press: 
      then:
        - switch.turn_off: latch_switch

  - platform: gpio
    pin:
      number: GPIO12
      mode:
        input: true
        pullup: true
    name: button2_On
    on_press:
      then:
        - switch.turn_on: latch_switch

switch:
  - platform: template
    name: "Latch"
    id: latch_switch
    optimistic: True
    restore_mode: ALWAYS_OFF

I was thinking of something close, but using a binary variable. But it’s a lot easier to use the objects than just variables.

You can also use the above to electronically de-bounced a toggle switch, use a SPDT with the common connected to GND and the two poles connected to the GPIOs. This will then be electronically de-bounced without resorting to time delays, you can use the same principle electronically with an SR flip flop.

I’ve thought about debouncing electronically. Extra hardware, which is doable, but I’m from a programming background and tend toward software solutions. Also, I have a master switch and 4 others that can’t go on unless the master is on. I’m thinking of debouncing the master electronically and the others with software.