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)
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
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.
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.
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.
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.
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