ESPHOME - tactile switch and virtual switch toggle

I want to connect a tactile micro switch to my esp32 device (Node MCU 32) but when the button is pressed it registers On then Off when released. So to solve that I thought of using a virtual switch and then use On_click for the GPIO button to toggle the virtual switch:

binary_sensor:
  - platform: gpio
    id: button01
    pin:
      number: 25
      inverted: true
      mode:
        input: true
        pullup: true
    internal: true
    on_click:
      then:
        - switch.toggle: virtual_switch
switch:
  - platform: template
    name: Virtual Switch
    id: virtual_switch
    turn_on_action:
      - switch.turn_on: virtual_switch
    turn_off_action:
      - switch.turn_off: virtual_switch

but that just crashes my device. What am I doing wrong here?
Thanks

Just stop there

Yes - that was my starting point but the switch I am using is a momentary push-button switch (where the state changes only when the button is actively pressed) so with that config - when I press the button -it will change to ON, and when the button is released it is OFF.
That is why I was thinking to use the virtual switch

What outcome or functionality are you actually trying to achieve?

Do you want a virtual switch whose state can be toggled by pressing the momentary button?

Do you want a virtual switch whose state can be toggled by pressing the momentary button?

Yes Exactly that

Just remove this - it’s redundant and is probably causing an infinite loop thus crashing your device:

    turn_on_action:
      - switch.turn_on: virtual_switch
    turn_off_action:
      - switch.turn_off: virtual_switch
1 Like

yep - tried that already - but ESPhome complains that:
Either optimistic mode must be enabled, or turn_on_action or turn_off_action must be set, to handle the switch being set.
I also tried:

switch:
  - platform: template
    name: Virtual Switch
    id: virtual_switch
    optimistic: true

That fixes the error above and it kind of works but every second or third time you have to press the button 2-3 times before the virtual switch changes state. And yes I checked the physical button is fine because ESPHOME logs the presses.

Sure - you need some actions for switch on and off. But don’t reference the switch you are in, do whatever you want the switch to do. Toggle an output GPIO for instance or turn on an led. Just don’t turn on the switch you are in which will turn on the switch you are on which will etc etc…

If you just want the state in HA - what you have above should work but you may need to debounce the input GPIO.

Also try using on_press: rather than on_click:

1 Like

what do you mean by “debounce the input GPIO” ?

Debounce means ignore multiple detects because of the physical switch not being perfect. But also see my comment about using on_press:

1 Like

For debouncing, watch the logs and observe if there is an extra unexpected on and/or off associated with the tactile switch.

Then you typically use one of these
… delayed on, off, on_off.

Often a delayed off of about 500ms does the job.

But as zoogara says, move to on_press first.