Quantcast
Channel: ESPHome - Home Assistant Community
Viewing all articles
Browse latest Browse all 1486

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

$
0
0

@patfelst wrote:

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.

Posts: 4

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 1486


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>