Light.toggle in ESP32 not working properly with momentary button

I have an ESP32 connected to a relay and a momentary button. I want to use the button to turn off the relay locally as well as send a message (somehow) to HAss to turn on/off all lights.
Tap> toggle local relay(light); double tap>all lights on; hold for 2s> all lights off.
BUT—The light.toggle function is not working correctly. It turns off the light, and then IMMEDIATELY turns it back on. I tried to code a toggle from scratch using an if>then>else function, but it still did the same thing. Please Help!

binary_sensor:
  platform: gpio
  pin: 
    number: 12
    mode: INPUT_PULLUP
  name: "BedButton"
  filters: 
   - invert:
   - delayed_on: 50ms
   - delayed_off: 50ms
#   - delayed_on_off: 1000ms
  on_multi_click:
    - timing:
        - ON for at most 1s
        - OFF for at most 0.3s
        - ON for at most 1s
        - OFF for at least 0.2s
      then:
        - light.turn_on: BedLamp1
        - switch.template.publish:
            id: allLights
            state: ON
        - logger.log: "Double-Clicked"
    - timing:
        - ON for at most .5s
        - OFF for at least 0.1s
      then:
        light.toggle: BedLamp1
       # if:
       #   condition:
       #     light.is_on: BedLamp1
       #   then:
       #     - light.turn_off: BedLamp1
       #     - logger.log: "Single-Clicked Off"
         # else: 
         #   - light.turn_on: BedLamp1
         #   - logger.log: "Single-Clicked On"
    - timing:
        - ON for at least 2s
      then:
        - light.turn_on:
            id: ButtonLight
            brightness: 15%
        - switch.template.publish:
            id: allLights
            state: OFF
        - logger.log: "Hold-Click"

How are the lights connected to the ESP switch? Are they wired to it or does the button just send commands to HA?

Maybe there is another Automation in HA toggling the light. For example an automation that turns on the light, because it is dark outside. Then you toggle the light off with the button, but the automation is still running and keeps turning it on again. So the switch should not just toggle the light, but also the condition or automation that keeps turning on the light.

And there is a hyphen missing in your configuration in front of the light:

  on_multi_click:
    - timing:
        - ON for at most 1s
        - OFF for at most 0.3s
        - ON for at most 1s
        - OFF for at least 0.2s
      then:
        - light.turn_on: BedLamp1
        - switch.template.publish:
            id: allLights
            state: ON
        - logger.log: "Double-Clicked"
    - timing:
        - ON for at most .5s
        - OFF for at least 0.1s
      then:
        light.toggle: BedLamp1

Instead of

      then:
        light.toggle: BedLamp1

it should be

      then:
        - light.toggle: BedLamp1

i think.

Thank you for the response. This is the first ESP32. I have 1 light on the relay connected to it. But I intend to set up other ESP32s with more relays and possibly more buttons. I also just procured some Sonoff S20’s, and I’m working on Tasmotizing them. But for now, I just have the one, and I’m trying to set up the framework for controlling more than one with physical buttons.

I tried adding a hyphen to light.toggle. It still does exactly the same thing. Thank you anyways.

I FIXED IT!!! Yayy! I was so stupid. The problem was in the corresponding automation I set up in HAss. On the ESP32, I set up a switch.template to act as a virtual switch that would trigger an automation. But then in HAss, I accidentally set the automation to trigger off just the act of pressing the button, rather than the changing of the virtual switch. Thank you for your help.

1 Like