How to trigger an action when one presses a button (binary_sensor) for 5 seconds?

Hi there,
Basically I’m trying to perform a restart with a restart switch when the button on my Sonoff S26 is pressed for 5 seconds.
I’ve got the followig code:

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO0
      mode: INPUT_PULLUP
      inverted: True
    name: "Sonoff-S26_1 przycisk"
    id: button
    on_click:
      then:
        - switch.toggle: przekaznik
        
    on_press:
      - logger.log: "on_press"
      - if:
          condition:
            for:
              time: 5s
              condition:
                binary_sensor.is_on: button
          then:
            - logger.log: "5 seconds!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
            - switch.toggle: switch_restart

and this action is never trigerred. Any ideas why??

 then:
            - logger.log: "5 seconds!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
            - switch.toggle: switch_restart

I’ve tried using two on_click triggers with different min/max times, but in this case I’ve got a YAML error that there is a double key on_click.

Hello,
did you manage to do this?

Leaving my solutions here in case of someone wonders.

The reason why the first code isn’t working is that the code is based on the “on_press” trigger, which will only run once when you press the button.

So, when you press the button, the code will check if the button is being held for 5 seconds, which will never happen, and therefore, the action is never triggered.

Instead of using the “on_press” trigger, you should use an interval trigger, checking every second to see if the button has been held for 5 seconds.

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO0
      mode: INPUT_PULLUP
      inverted: True
    name: "Sonoff-S26_1 przycisk"
    id: button
    on_click:
      then:
        - switch.toggle: przekaznik
        
    on_press:
      - logger.log: "on_press"

interval:
  - interval: 1s
    then:
      - if:
          condition:
            for:
              time: 5s
              condition:
                binary_sensor.is_on: button
          then:
            - logger.log: "5 seconds!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
            - switch.toggle: switch_restart

You could also just use a copy binary sensor with a delayed_on filter.

Solution is: `on_multi_click’.