Long press button on sonoff basic

I have a sonoff basic R2 controlling a lamp.
I have set it up with esphome, and it works great.
The sonoff basic R2 has a physical button to trigger the relay, I have extended that button out of the sonoff case into the base of the lamp and that works great too.
I have set up a script in home assistant that if the binary sensor of the sonoff’s button is held down for 2 seconds, it will turn off all of the lights in that room.
I have executed the script and it works.
When I try to use this automation in real life by holding that button for 2 seconds, it doesn’t work. It seems the automation isn’t being triggered.
I think it’s because of bounce on the physical button, it’s may be being detected as several button presses rather than one long button press, and somewhere in the works something stops long pressing the button from repeat triggering the function of the button.
So I’d like to know if anyone has attempted this or had success with a function like this, I’m not sure how to approach this issue.

As it turns out it was all the user error of a noobie plodding along.
After some adjustment I got the automation working without needing to debounce the button at all.
Below is the automation code.

- id: '1594766677619'
  alias: Bedroom - Lamp - 2 sec hold
  description: ''
  trigger:
  - type: turned_on
    platform: device
    device_id: c8ca6bec55d948acbgh56ae03aaf7b2b
    entity_id: binary_sensor.lamp_button
    domain: binary_sensor
    for:
      hours: 0
      minutes: 0
      seconds: 2
  condition: []
  action:
  - data: {}
    entity_id: light.bedroom_lights
    service: light.turn_off
  mode: single

I also found another way to do something like this which I think is pretty great.
Here is some code to perform different functions by different button press patterns in ESPHome.
It also demonstrates how to instruct the ESPHome code to control other devices and entities within your Home Assistant environment.

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO0
      mode: INPUT_PULLUP
      inverted: True
    name: "Bedroom Main Button"
    on_click:
    - min_length: 200ms
      max_length: 900ms
      then:
        - switch.toggle: relay_1
    - min_length: 1000ms
      max_length: 5000ms
      then:
        - homeassistant.service:
            service: cover.toggle
            data:
              entity_id: cover.bedroom_curtains
    on_multi_click:
    - timing:
      - ON for at most 0.5s
      - OFF for at most 0.5s
      - ON for at most 0.5s
      - OFF for at least 0.2s
      then:
        - homeassistant.service:
            service: light.toggle
            data:
              entity_id: light.bedroom_lights

With this code a single short press of the button toggles the relay of the sonoff, a longer press operates my curtains, and a double press toggles the light group which is all of the lights in that room.

Hopefully someone finds that helpful :slight_smile:

7 Likes