Binary sensor for momentary switch - Can I use "on_click" and "on_double_click" in the same automation?

I have a momentary pushbutton connected to GPIO27 and want to perform different actions (set a global variable value for a countdown timer) depending on whether the button is clicked, or double clicked. A switch is turned on at the first single click, and then off when the countdown reaches zero.

Both single and double click triggers work, but after a double click, a single click is always called immediately after. Single clicks add a number of seconds to a counter, whereas a double click sets it to zero. So I only get the zero value for a very short time.

I’ve tried lengthening the debounce filter time, and have tried many combinations of the min_length and max_length times with no solution.

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO27
      inverted: true
      mode: INPUT_PULLUP
    internal: true # don't publish to Home Assistant
    filters:
      - delayed_on: 60ms
    name: "Push button"

    # Single click
    # User has requested to turn ON the switch, or wants to add some time to the countdown
    on_click:
    - min_length: 20ms
      max_length: 150ms      
      then:
        # Only turn the switch ON if this is the first press (i.e. don't turn on if user is adding time)
        - if:
            condition:
              lambda: 'return id(countdown) == 0;'
            then:
              - homeassistant.service:
                  service: switch.turn_on
                  data:
                    entity_id: $switch_name

        # Is this the first press to turn ON, or does the user want to add time?
        - lambda: |-
            if (id(countdown) == 0) {
              // First press - Turn ON
              id(countdown) = 300;
            }
            else {
              // Switch is on, so add some time to the countdown
              id(countdown) += 10;
            }
        - logger.log: "Short Click"
        - light.turn_on:
            id: neopixel_single_1
            brightness: 50%
            red: 0%
            green: 100%
            blue: 0%        
        - delay: 200ms
        - light.turn_off: neopixel_single_1

    # Double click
    # Let user turn off before countdown is finished
    on_double_click:
    - min_length: 50ms
      max_length: 600ms
      then:
        - homeassistant.service:
            service: switch.turn_off
            data:
              entity_id: $switch_name
        - lambda: |-
            id(countdown) = 1;
        - logger.log: "Long Click"
        - light.turn_on:
            id: neopixel_single_1
            brightness: 50%
            red: 100%
            green: 0%
            blue: 0%        
        - delay: 200ms
        - light.turn_off: neopixel_single_1

I realise there are simpler ways of automating turn off of a switch after a timeout period, but I have an OLED display connected which shows the remaining time.

I will try on_multi_click, that might be the solution

I use on_double_click to extend the use of some light switches, and the double click always triggers the single click, so it seems like normal behaviour.

As for your push button, rather than on_double_click I use on_click with three non overlapping min / max lengths for a pushbutton I have in the garage.
So the action will depend on click length rather than the number of clicks.
This way, the actions I don’t want are not triggered.

    on_click:
    - min_length: 50ms        # Short press
      max_length: 500ms
      then:
        # Some action
    - min_length: 500ms      # Medium press
      max_length: 3000ms
      then:
        # Some other action
    - min_length: 3000ms        # Long press
      max_length: 10000ms
      then:
        # Some other action
1 Like

awesome, thanks that works really well