Skip to content

Commit 931940e

Browse files
authored
Improve longpress example for binary sensor (#5753)
* Improve longpress example for binary sensor Replaced the Binary Sensor longpress example that needed an additional Template Binary Sensor, with a regular on_multi_click automation. * Fix YAML indentation issue
1 parent 311ebee commit 931940e

File tree

1 file changed

+34
-39
lines changed

1 file changed

+34
-39
lines changed

content/components/binary_sensor/_index.md

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,6 @@ binary_sensor:
268268

269269
Configuration variables: See [Automation](/automations).
270270

271-
See [`on_click`](#binary_sensor-on_click) for an example using a single button for both `on_click` and a continuous longpress.
272-
273271
{{< anchor "binary_sensor-on_release" >}}
274272

275273
### `on_release`
@@ -376,43 +374,6 @@ Configuration variables:
376374
> - switch.turn_on: relay_1
377375
> ```
378376

379-
> [!NOTE]
380-
> A single button can be used to handle both `on_click` and a continuous longpress, by making a
381-
> [Template Binary Sensor](/components/binary_sensor/template/) follow a GPIO button with a delay:
382-
>
383-
> ```yaml
384-
> binary_sensor:
385-
> - platform: gpio
386-
> id: button_1
387-
> # ...
388-
> on_click:
389-
> min_length: 50ms
390-
> max_length: 350ms
391-
> then:
392-
> - light.turn_on:
393-
> id: light_1
394-
> brightness: 10%
395-
>
396-
> - platform: template
397-
> # ...
398-
> condition:
399-
> binary_sensor.is_on: button_1
400-
> filters:
401-
> - delayed_on: 1s
402-
> on_press:
403-
> - while:
404-
> condition:
405-
> # Using `!lambda: 'return true;'` would run endlessly
406-
> # once triggered, so repeat the outer condition
407-
> binary_sensor.is_on: button_1
408-
> then:
409-
> - light.dim_relative:
410-
> id: light_1
411-
> relative_brightness: 5%
412-
> transition_length: 0.1s
413-
> - delay: 0.1s
414-
> ```
415-
416377
{{< anchor "binary_sensor-on_double_click" >}}
417378

418379
### `on_double_click`
@@ -503,6 +464,40 @@ on_multi_click:
503464
- logger.log: "Single Short Clicked"
504465
```
505466

467+
While [`on_click`](#binary_sensor-on_click) only triggers on the falling edge of the signal,
468+
and [`on_double_click`](#binary_sensor-on_double_click) only on the second leading edge, an
469+
automation for `on_multi_click` can trigger at any time. For example, an `ON for at least`
470+
timing without an `OFF` does not await a falling edge. This supports implementing a
471+
continuous longpress, optionally also handling clicks for the very same sensor:
472+
473+
```yaml
474+
binary_sensor:
475+
- platform: gpio
476+
id: button_1
477+
# ...
478+
on_multi_click:
479+
# One can also replace this part with `on_click`
480+
- timing:
481+
- ON for at most 0.7s
482+
then:
483+
- light.turn_on:
484+
id: light_1
485+
brightness: 10%
486+
- timing:
487+
- ON for at least 1s
488+
then:
489+
- while:
490+
condition:
491+
# Self-reference this very sensor
492+
binary_sensor.is_on: button_1
493+
then:
494+
- light.dim_relative:
495+
id: light_1
496+
relative_brightness: 5%
497+
transition_length: 0.1s
498+
- delay: 0.1s
499+
```
500+
506501
{{< anchor "binary_sensor-is_on_condition" >}}
507502
{{< anchor "binary_sensor-is_off_condition" >}}
508503

0 commit comments

Comments
 (0)