On_press and on_multipress with same GPIO with single touch switch button

I have this device. I have the ESP8266 Version.

I want to setup the light switch button to toggle my light bulb on and off directtly (without turning off the relay cutting the power) when I touch the button. I would also like to make the same button to toggle the relay when I hold the button for 2 secs.

Issue is that the the same gpio can’t be use to do both. I wanted to avoid using MQTT as it has the issue of doubling up the switches in Home Assistant. I know I can make it so that doesn’t happening but I would like to see ESPhome can do this on it own.

I can kinda do this with the Tasmota scripting but it has the same GPIO issue and a difficult console interface and so many rules to setup for this device just to work I gave up trying to work it out.

I have the same device but with a WB3S - BK7231T chip running OpenBeken. It took some stuffing about but finally got it going with some easy autoexec.bat script. Unfortunatly, I have the ESP8266 version and OpenBeken doesn’t support that yet.

I wanted to give ESPhome a go as it functions well with in Home Assistant and would allow me to have all my devices running ESPhome and not have to use MQTT. Not that I hate MQTT it’s just an extra step. I guess If I can’t do it then I might leave everything as it is as and convert this device back to Tasmota and live with that fact that it can’t be done for now.

I would stay with ESPhome but it is not working well with this device as the fan get stuck on MED speed and I have no idea how to fix it at the moment. This device has a very stange relay system setup and the script on the ESPhome site doesn’t work how it should. That will be my next step if I get this working first. One thing at a time.

So far I haven’t had luck so I’m turning to you to see if you can help me work it out. So far i have this

I’ve also asked ChatGPT and Claude to help out but they all give the same answers and so far it appears it can’t be done easily. I have searched Google this forum and so many others like it but still can’t find an answer that works. My guess is it can’t be done but doesn’t hurt to ask.

binary_sensor:
  - platform: gpio
    pin: GPIO16
    name: "Touch Switch"
    id: touch_switch
    on_press:
      - homeassistant.service:
          service: light.toggle
          data:
            entity_id: light.lounge
    on_multi_click:
      - timing:
          - ON for at least 2s
        then:
          - switch.toggle: relay

switch:
  - platform: gpio
    name: "Relay"
    pin: GPIO16  # Physical relay connected to GPIO16
    id: relay

After a bit of screwing about I kinda got it to work. Looking over the code it appears that the light relay uses GPIO14 and GPIO16 have no idea why when it’s just one switch.

I can get it to turn the light on and off and toggle the relay but it’s a little bit funky. sometimes it works right and then sometimes it doesn’t. It’s very hit and miss. Something the relay will turn on by tapping the button or if the relay is on the light will turn off but then the relay will turn off shortly after. It does other weird combinations of this as well

I’m sure it my code and the fact it’s late and my brain hurts. Here is what I have. I’m kinda new to ESPhome coding so I’m sure I have this wrong

binary_sensor:
  - platform: gpio
    pin: GPIO16
    name: "Touch Switch"
    id: touch_switch
    on_press:
      - homeassistant.service:
          service: light.toggle
          data:
            entity_id: light.lounge
    on_click:         
      - min_length: 2000ms
        max_length: 5000ms
        then:
          - switch.toggle: relay

switch:
  - platform: gpio
    name: "Relay"
    pin: GPIO14
    id: relay

Hi, you can refer to this detach example for multi press and fallback

As for tasmota, this is the rule that i added to do the same thing. I’m using it with a tasmota flashed light bulbs with device group.

Relay 1= fake relay, added to the same device group for the light bulb
Relay 2 = actual relay for the light bulb, only gets toggled if wifi is down

on wifi#connected do var4 online endon
on wifi#disconnected do var4 offline endon
on switch1#state=2 do IF ((%var4%=online) AND (%power2%==1)) power1 toggle ELSE power2 toggle ENDIF endon