Trigger different actions on a single, double or double click on a binary sensor

This blueprint ease the configuration of any binary sensor to trigger different actions in case of a single click, long click (longer than a delay to configure) or double clicks (time between clicks to configure).

Get started

Click the badge to import this Blueprint: (needs Home Assistant Core 2021.3 or higher)

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

Or import this Blueprint by using the forum topic URL:

blueprint:
    name: Short long double click
    description: Different Actions on Short, Long and Double click
    domain: automation
    input:
        switch_id:
            name: Binary sensor entity
            description: The binary_sensor used to trigger the automation
            selector:
                entity:
                    domain: binary_sensor
        long_click_action:
            name: Long click action
            description: The action(s) to launch for a long click
            default: []
            selector: 
                action:
        short_click_action:
            name: Short click action
            description: The action(s) to launch for a single short click
            default: []
            selector: 
                action:
        double_click_action:
            name: Double click action
            description: The action(s) to launch for a double click
            default: []
            selector: 
                action:
        delay:
            name: Delay in seconds
            description: The time in seconds (can contain decimals) used as a delay for the double or long click detection
            default: 0.3
            selector:
                number:
                  min: 0.0
                  max: 10.0
                  unit_of_measurement: seconds
trigger:
  - platform: state
    entity_id: !input switch_id
    to: 'on'
mode: single
action:
  - variables: # create variable cause we need those in templates
        switch_id: !input switch_id
        double_click_action: !input double_click_action
  - wait_template: "{{ is_state(switch_id, 'off') }}"
    timeout: !input delay
    continue_on_timeout: true
  - choose:
      - conditions: "{{ not wait.completed }}" # long click
        sequence: !input long_click_action
    default:
      - choose:
          - conditions: "{{ double_click_action | length > 0 }}" #only wait for second click if a n action is defined for it to be more reactive
            sequence:
              - wait_template: "{{ is_state(switch_id, 'on') }}" # second click?
                timeout: !input delay
                continue_on_timeout: true
              - choose:
                  - conditions: "{{ not wait.completed }}" #single click
                    sequence: !input short_click_action
                default: !input double_click_action  # double click
        default: !input short_click_action
15 Likes

Do you mean in this case, you would use the toggle button on a smart plug to trigger an action?

What kind of physical switch are you using to test this automation? Is it a single button that you ‘push-on/push-off’?

The reason why I ask is because all of my switches are traditional Decora style with a single rocker button. Press the top of the rocker to turn on, press the bottom to turn off. That style of operation does not conform to the concept of a ‘click’ (as in a single or double-click).

Your automation relies on the timing between when the switch turns on and when it turns off. Using a rocker switch, that operation wouldn’t be inconvenient and would definitely not qualify as a ‘click’ (attempting a double-click would be even less practical).

On the other hand, if your switch is a single ‘push-on/push-off’ button then I can see how performing a ‘click’ or ‘double-click’ is feasible.

I’m guessing the likes a Xiaomi wireless button as they are what I use with single, double and long click operations currently.

In that case, it might be useful to mention that in the first post because this automation won’t work for traditional, North-American rocker-based switches. Well, it sort of can work but the action of turning the rocker on/off would be inconvenient and not what anyone would call a ‘click’.

FWIW, the switches I have are capable of supporting a ‘click’ operation but there’s no state-change involved (i.e. the switch’s state doesn’t change from on to off). The click operation is reported as a Scene.

I don’t have one, but does it make an entity under the domain of switch?

Yep. Integrated via the Xiaomi gateway you get single, double and long press. Via ZHA you get more clicks (ie: 4, 5…) but no long press from what I can tell.

Odd. Usually switchs are for smart plugs and stuff that you can toggle. IMO it’d make more sense if the Xiaomi thing made a binary_sensor.

If you integrate it with the Xiaomi gateway, it is a binary_sensor. I’ve created a blueprint for that.

Sorry, my mistake. It is in fact binary_sensor. Not sure what I was thinking :man_facepalming:

I hope the author returns to clarify the situation because the automation clearly requests a switch entity:

            selector:
                entity:
                    domain: switch

Hi all,
I’m using push button like this one: https://www.niko.eu/en/products/switching-material-and-socket-outlets/switches-and-sockets/push-button-with-three-connection-terminals-no-productmodel-niko-025f93c2-c921-59b0-b204-af07ed3ee2a8
You push -> the switch is activated,
You release -> the switch is disactivated.

I can add the possibility to use a binary_sensor in addition to switches

1 Like

In HA a switch is an output, not an input. The trigger to the automation needs to be an input domain. ie: binary_sensor

Ok I changed it to use a binary sensor instead.

So how did the original version ever work when it used a switch entity?

Cause in my configuration I do use switches (https://www.home-assistant.io/integrations/switch.mqtt/) entities to represent my physical switches. A push on the physical switch trigger an mqtt message that change the switch entity in HA.

This description is odd. If a push activates and a release deactivates, how do you leave it in the activated state without releasing the button?

I don’t. I activate automation (light.toggle generally) on the push button event (when the switch goes from off to on)

Nothing about the switch you use meets the classical definition of a switch. A traditional switch has two physical positions corresponding to its two logical states or is push-on/push-off to toggle between the two states.

What you have described behaves like a remote-control button. The original blueprint, that used a switch entity, would have been impractical with a traditional switch.

I believe this automation works with a single node with a button and therefore binary. in my case even if I have not tried yet, since I use esphome, an esp01 with a button creating a sensor binary entity to be associated with this automation