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:
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
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:
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.