-
Notifications
You must be signed in to change notification settings - Fork 0
/
moes_scene_switch_multi_press.yaml
633 lines (633 loc) · 28.7 KB
/
moes_scene_switch_multi_press.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
# Blueprint metadata
blueprint:
name: Moes / Tuya Scene Switch (Multi-press)
description: |
# Moes / Tuya Scene Switch - Multi-press Controller v0.1
domain: automation
input:
controller_device:
name: (ZHA) Button Device
description: The button device to use for the automation (do not fill if using Zigbee2MQTT).
default: ""
selector:
device:
entity:
- domain: sensor
device_class: battery
<<: &int-zha
integration: zha
filter:
# 1 Gang Wall Remote
- manufacturer: "_TYZB02_keyjqthh"
<<: &ws-1
<<: *int-zha
model: "TS0041"
- <<: *ws-1
manufacturer: "_TZ3000_tk3s5tyg"
- <<: *ws-1
manufacturer: "_TZ3000_fkp5zyho"
- <<: *ws-1
manufacturer: "_TZ3000_axpdxqgu"
- <<: *ws-1
manufacturer: "_TZ3000_peszejy7"
- <<: *ws-1
manufacturer: "_TZ3000_pzui3skt"
- <<: *ws-1
manufacturer: "_TZ3000_f97vq5mn"
# 2 Gang Wall Remote
- manufacturer: "_TZ3000_5e235jpa"
<<: &ws-2
<<: *int-zha
model: "TS0042"
- <<: *ws-2
manufacturer: "_TZ3000_owgcnkrh"
- <<: *ws-2
manufacturer: "_TYZB02_keyjhapk"
- <<: *ws-2
manufacturer: "_TZ3000_oikiyf3b"
- <<: *ws-2
manufacturer: "_TZ3000_dfgbtub0"
- <<: *ws-2
manufacturer: "_TZ3000_h1c2eamp"
- <<: *ws-2
manufacturer: "_TZ3400_keyjhapk"
- <<: *ws-2
manufacturer: "_TZ3000_fkvaniuu"
# 3 Gang Wall Remote
- manufacturer: "_TZ3000_gbm10jnj"
<<: &ws-3
<<: *int-zha
model: "TS0043"
- <<: *ws-3
manufacturer: "_TZ3000_a7ouggvs"
- <<: *ws-3
manufacturer: "_TYZB02_key8kk7r"
- <<: *ws-3
manufacturer: "_TZ3000_qzjcsmar"
- <<: *ws-3
manufacturer: "_TZ3000_rrjr1q0u"
- <<: *ws-3
manufacturer: "_TZ3000_w8jwkczz"
- <<: *ws-3
manufacturer: "_TZ3000_yw5tvzsk"
# 4 Gang Wall Remote
- manufacturer: "_TZ3000_vp6clf9d"
<<: &ws-4
<<: *int-zha
model: "TS0044"
- <<: *ws-4
manufacturer: "_TZ3000_xabckq1v"
- <<: *ws-4
manufacturer: "_TZ3000_wkai4ga5"
- <<: *ws-4
manufacturer: "_TZ3000_ufhtxr59"
- <<: *ws-4
manufacturer: "_TZ3000_ee8nrt2l"
button_endpoint_id:
name: (Required) Button Number
description: The button number to control for multi-gang
default: 1
selector:
number:
min: 1
max: 9
<<: &num-selector-slider
mode: slider
step: 1
helper_last_controller_event:
name: (Required) Helper - Input Text
description: Input Text used to store the last event fired by the controller. You will need to manually create a text input entity for this, please read the blueprint Additional Notes for more info.
default: ""
selector:
entity:
filter:
domain: input_text
controlled_light_entity:
name: (Optional) Toggle/Dim Light Entity
description: Light entity to be toggled on first press as configured below.
default: ""
selector:
entity:
filter:
domain: light
scene_first_release:
name: (Optional) First Press/Release Scene
description: Select a scene to be activated on the first button press/release.
<<: &scene-selector
default: ""
selector:
entity:
filter:
- domain: scene
scene_second_release:
<<: *scene-selector
name: (Optional) Second Press/Release Scene
description: Select a scene to be activated on the second button press/release.
scene_third_release:
<<: *scene-selector
name: (Optional) Third Press/Release Scene
description: Select a scene to be activated on the third button press/release.
scene_fourth_release:
<<: *scene-selector
name: (Optional) Fourth Press/Release Scene
description: Select a scene to be activated on the fourth button press/release.
scene_fifth_release:
<<: *scene-selector
name: (Optional) Fifth Press/Release Scene
description: Select a scene to be activated on the fifth button press/release.
button_toggle_controlled_light_entity_first_press:
name: (Optional) Enable Toggle Light on First Press
description: Enable or disable the behaviour of toggling the Toggle/Dim Light Entity on first press.
<<: &bool-selector-true
default: true
selector:
boolean:
button_multi_press_enabled:
<<: *bool-selector-true
name: (Optional) Enable button multi press events
description: Choose whether or not to enable the virtual multi press events for the button. Leave this on if you are providing actions for the button second/third/fourth/fifth press events.
# inputs for custom actions
action_button_short_release:
name: (Optional) Short button press release. (First)
description: >-
Action to run on short button press release.
There is no need to use this action if using the light toggle options unless you want to activate a particular scene.
<<: &action-selector
default: []
selector:
action:
action_button_double_press:
<<: *action-selector
name: (Optional) Button second press
description: Action to run on the release of a second button press.
action_button_triple_press:
<<: *action-selector
name: (Optional) Button third press
description: Action to run on the release of a third button press.
action_button_quadruple_press:
<<: *action-selector
name: (Optional) Button fourth press
description: Action to run on the release of a fourth button press.
action_button_quintuple_press:
<<: *action-selector
name: (Optional) Button fifth press
description: Action to run on the release of a fifth button press.
action_button_long_repeat:
<<: *action-selector
name: (ADVANCED) Button long hold
description: >-
Action to run on long button hold.
action_button_long_release:
<<: *action-selector
name: (ADVANCED) Long button press release
description: Action to run on button release after long press.
# helpers used to properly recognize the remote button events
helper_multi_press_delay:
name: (ADVANCED) Multi Press Window
description: Max time window between button presses for multi-press events. Increase this value if multi-press actions aren't recognised, decrease it if they're recognised too much.
default: 2500
selector:
number:
min: 1000
max: 43200000
step: 10
<<: &num-selector-box
unit_of_measurement: milliseconds
mode: box
resume_light_state_when_off:
name: (ADVANCED) Resume previous light state when off.
description: >-
Enable to perform a simple `light.turn_on` service when the light is off.
<<: &bool-selector-false
default: false
selector:
boolean:
toggle_controlled_light_entity_brightness:
name: (ADVANCED) First Press Toggle Brightness %
description: The Brightness % to set the Toggle/Dim Light Entity to when turning on with a first press toggle. (0 uses the previous brightness)
default: 0
selector:
number:
<<: *num-selector-slider
min: 0
max: 100
toggle_controlled_light_entity_transition:
name: (ADVANCED) Transition time for light on/off
description: >-
Transition time for switching the `Toggle/Dim Light Entity` on/off (where supported)
default: 1
selector:
number:
<<: *num-selector-slider
min: 0
max: 10
unit_of_measurement: seconds
button_multi_press_always_fire_first_release:
<<: *bool-selector-true
name: (ADVANCED) Always fire first short button release
description: Choose whether or not to always fire the first short button release action without waiting for the Multi Press delay.
action_repeat_on_press_count:
name: (ADVANCED) Repeat actions on multi-presses
description: >-
Repeats actions X times, on each press after the first.
Useful for scene actions to account for device delays to ensure the final press is activated.
default: 3
selector:
number:
<<: *num-selector-slider
min: 0
max: 5
unit_of_measurement: repetitions
delay_between_action_repeats:
name: (ADVANCED) Delay between multi-press action repititions
description: If you have issues with the multi-press action repititions try increasing this a bit.
default: 600
<<: &millisecond-selector
selector:
number:
<<: *num-selector-box
min: 0
max: 5000
step: 1
helper_debounce_delay:
<<: *millisecond-selector
name: (ADVANCED) Internal Debounce Delay
description: By default you should probably leave this at 0, but you might need to increase it in certain situations.
default: 0
# Automation schema
variables:
# convert input tags to variables, to be used in templates
button_multi_press_enabled: !input button_multi_press_enabled
button_multi_press_always_fire_first_release: !input button_multi_press_always_fire_first_release
helper_last_controller_event: !input helper_last_controller_event
helper_multi_press_delay: !input helper_multi_press_delay
button_toggle_controlled_light_entity_first_press: !input button_toggle_controlled_light_entity_first_press
toggle_controlled_light_entity_brightness: !input toggle_controlled_light_entity_brightness
controlled_light_entity: !input controlled_light_entity
toggle_controlled_light_entity_transition: !input toggle_controlled_light_entity_transition
helper_debounce_delay: !input helper_debounce_delay
delay_between_action_repeats: !input delay_between_action_repeats
action_repeat_on_press_count: !input action_repeat_on_press_count
first_release_action: !input action_button_short_release
second_release_action: !input action_button_double_press
third_release_action: !input action_button_triple_press
fourth_release_action: !input action_button_quadruple_press
fifth_release_action: !input action_button_quintuple_press
scene_first_release: !input scene_first_release
scene_second_release: !input scene_second_release
scene_third_release: !input scene_third_release
scene_fourth_release: !input scene_fourth_release
scene_fifth_release: !input scene_fifth_release
resume_light_state_when_off: !input resume_light_state_when_off
# adjusted debounce delay so that the resulting multi press delay is exactly as specified by the user when running the action, taking also account of debouncing
# make sure it never goes below the minimum multi press delay
adjusted_multi_press_delay: "{{ [helper_multi_press_delay - helper_debounce_delay, 100] | max }}"
# mapping between actions and integrations
button_short_release: [remote_button_short_press, remote_button_double_press]
button_long_repeat: [repeat, on_hold, hold]
button_long_release: [long_release, on_long_release, release]
# build data to send within a controller event
controller_device: !input controller_device
controller_id: >-
{{controller_device}}
action_repeat_on_press: "{{ action_repeat_on_press_count > 0 }}"
release_types:
- "short_release"
- "second_release"
- "third_release"
- "fourth_release"
- "fifth_release"
controlled_light_entity_configured: >-
{{ (controlled_light_entity | string | length) > 0 }}
toggle_controlled_light_entity_first_press_enabled: >-
{{ button_toggle_controlled_light_entity_first_press and controlled_light_entity_configured }}
trigger_variables:
button_endpoint_id: !input button_endpoint_id
mode: restart
max_exceeded: silent
trigger:
- event_data:
command: remote_button_short_press
<<: &tr-event-data
device_id: !input controller_device
endpoint_id: "{{ button_endpoint_id }}"
<<: &tr-zha-event
platform: event
event_type:
- zha_event
- <<: *tr-zha-event
event_data:
<<: *tr-event-data
command: remote_button_double_press
- <<: *tr-zha-event
event_data:
<<: *tr-event-data
command: remote_button_long_press
condition:
- condition: and
conditions:
# check that the button event is not empty
- condition: template
value_template: >-
{%- set trigger_action -%}
{% if "payload" in trigger %}
{{ trigger.payload }}
{% elif "type" in trigger.event.data %}
{{ trigger.event.data.type }}
{% else %}
{{ trigger.event.data.command }}
{% endif %}
{%- endset -%}
{{ trigger_action not in ["","None"] }}
action:
# Check config
- choose:
- conditions:
- condition: template
value_template: >-
{{ (helper_last_controller_event | length < 1) or state_attr(helper_last_controller_event, 'max') < 500 }}
sequence:
- service: persistent_notification.create
data:
message: >-
Invalid Config: "Helper - Last Controller Event" is not set or too short.
Set max length to 500 or more.
title: Moes Scene Switch
default:
# debouncing - when automation is triggered multiple times, the last automation run is the one which completes execution, due to mode restart
# therefore previous runs must wait for the debounce delay before executing any other action
# if the delay expires and the automation is still running it means it's the last run and execution can continue
- delay:
milliseconds: "{{ helper_debounce_delay }}"
# extract button event from the trigger
# provide a single string value to check against
- variables:
trigger_action: >-
{% if "payload" in trigger %}
{{ trigger.payload }}
{% elif "type" in trigger.event.data %}
{{ trigger.event.data.type }}
{% else %}
{{ trigger.event.data.command }}
{% endif %}
helper_valid: '{{ helper_last_controller_event is not none and (states(helper_last_controller_event)|length) and (states(helper_last_controller_event) | regex_match("^[{\[]{1}([,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]|\".*?\")+[}\]]{1}$")) }}'
helper_state: "{{ states(helper_last_controller_event) }}"
trigger_delta: >-
{% if helper_valid %}
{% set trig_time = helper_state.last_triggered %}
{% else %}
{% set trig_time = "1970-01-01 00:00:00" %}
{% endif %}
{{ (as_timestamp(now()) - as_timestamp(trig_time)) * 1000 }}
last_press_type: >-
{% if helper_valid and "press_type" in helper_state %}
{{ helper_state.press_type }}
{% else %}
{{ "None" }}
{% endif %}
trigger_valid: >-
{{ (trigger_delta | int) <= (helper_multi_press_delay | int) }}
multipress_valid: >-
{{ button_multi_press_enabled and trigger_valid }}
press_type: >-
{%- if (trigger_action | string) in button_short_release -%}
{%- if multipress_valid and (release_types[0] == last_press_type) and ((scene_second_release|length) > 0 or (second_release_action|length) > 0) -%}
{{- release_types[1] -}}
{%- elif multipress_valid and (release_types[1] == last_press_type) and ((scene_third_release|length) > 0 or (third_release_action|length) > 0) -%}
{{- release_types[2] -}}
{%- elif multipress_valid and (release_types[2] == last_press_type) and ((scene_fourth_release|length) > 0 or (fourth_release_action|length) > 0) -%}
{{- release_types[3] -}}
{%- elif multipress_valid and (release_types[3] == last_press_type) and ((scene_fifth_release|length) > 0 or (fifth_release_action|length) > 0) -%}
{{- release_types[4] -}}
{%- else -%}
{{- release_types[0] -}}
{%- endif -%}
{%- elif (trigger_action | string) in button_long_repeat -%}
{{- "long_repeat" -}}
{%- elif (trigger_action | string) in button_long_release -%}
{{- "long_release" -}}
{%- else -%}
{{- "" -}}
{%- endif -%}
pressed_scene_action: >-
{%- if press_type == release_types[0] -%}
{{- [scene_first_release, first_release_action] -}}
{%- elif press_type == release_types[1] -%}
{{- [scene_second_release, second_release_action] -}}
{%- elif press_type == release_types[2] -%}
{{- [scene_third_release, third_release_action] -}}
{%- elif press_type == release_types[3] -%}
{{- [scene_fourth_release, fourth_release_action] -}}
{%- elif press_type == release_types[4] -%}
{{- [scene_fifth_release, fifth_release_action] -}}
{%- else -%}
{{- ["", ""] -}}
{%- endif -%}
# choose the sequence to run based on the received button event
- choose:
- conditions:
- condition: template
value_template: >-
{{ press_type in release_types }}
sequence:
- choose:
- conditions:
- condition: template
value_template: >-
{{ press_type == release_types[0] }}
sequence:
# update helper
- &inputtext-update
service: input_text.set_value
data:
entity_id: !input helper_last_controller_event
value: '{{ {"press_type":press_type,"last_triggered":now() | string} | to_json }}'
- choose:
# if multi press events are enabled
- conditions:
- condition: template
value_template: >-
{{ button_multi_press_enabled }}
sequence:
# Optionally skip delay to always fire the first release
- choose:
- conditions:
- condition: template
value_template: >-
{{ button_multi_press_always_fire_first_release }}
sequence: []
default:
# if the second press is received, automation is restarted
- delay:
milliseconds: "{{ adjusted_multi_press_delay }}"
# run the short press action
# fire the event
- &fire-trigger-event
event: ahb_controller_event
event_data:
controller: "{{ controller_id }}"
action: "button_{{press_type}}"
# run the toggle dim entity action
- choose:
- conditions:
- &toggle-dim-entity-condition
condition: template
value_template: >-
{{ toggle_controlled_light_entity_first_press_enabled }}
sequence:
- choose:
- conditions:
- condition: template
value_template: >-
{{ expand(controlled_light_entity) | selectattr("state", "eq", "on") | list | count > 0 }}
- condition: template
value_template: >-
{{ not button_multi_press_enabled or not multipress_valid }}
sequence:
- service: input_text.set_value
data:
entity_id: !input helper_last_controller_event
value: ""
- service: light.turn_off
target:
entity_id: "{{ controlled_light_entity }}"
data:
transition: "{{ toggle_controlled_light_entity_transition }}"
default: &action-scene-first-release-action
- choose:
- conditions:
- condition: template
value_template: >-
{{ resume_light_state_when_off and (not button_multi_press_enabled or not multipress_valid) }}
sequence:
- &light-on
service: light.turn_on
target:
entity_id: "{{ controlled_light_entity }}"
default:
- default:
- choose:
- conditions:
- *toggle-dim-entity-condition
sequence:
- <<: *light-on
data: >
{% if (toggle_controlled_light_entity_brightness | int) > 0 %}
{{ {"transition": toggle_controlled_light_entity_transition, "brightness_pct": (toggle_controlled_light_entity_brightness | int)} }}
{% else %}
{{ {"transition": toggle_controlled_light_entity_transition } }}
{% endif %}
<<: &scene-press-sequence
choose:
- conditions:
- condition: template
value_template: >-
{{ pressed_scene_action[0]|length > 0 }}
sequence:
- service: scene.turn_on
target:
entity_id: "{{ pressed_scene_action[0] }}"
# run the custom action
- choose:
- sequence: !input action_button_short_release
<<: &action-press-sequence
conditions:
- condition: template
value_template: >-
{{ pressed_scene_action[1]|length > 0 }}
default: *action-scene-first-release-action
- conditions:
- condition: template
value_template: >-
{{ press_type == release_types[1] }}
sequence:
# store the second press event in the last controller event helper
- &fire-event-update-helper
choose:
- conditions: []
sequence:
- *inputtext-update
- *fire-trigger-event
# run the second press action
- repeat:
sequence:
- *scene-press-sequence
- choose:
- <<: *action-press-sequence
sequence: !input action_button_double_press
- &action-repeat-delay
choose:
- conditions:
- condition: template
value_template: >-
{{ action_repeat_on_press }}
sequence:
- delay:
milliseconds: "{{ delay_between_action_repeats }}"
<<: &action-scene-sequence
count: "{{action_repeat_on_press_count}}"
- conditions:
- condition: template
value_template: >-
{{ press_type == release_types[2] }}
sequence:
- *fire-event-update-helper
# run the third press action
- repeat:
<<: *action-scene-sequence
sequence:
- *scene-press-sequence
- choose:
- <<: *action-press-sequence
sequence: !input action_button_triple_press
- *action-repeat-delay
- conditions:
- condition: template
value_template: >-
{{ press_type == release_types[3] }}
sequence:
- *fire-event-update-helper
# run the fourth press action
- repeat:
<<: *action-scene-sequence
sequence:
- *scene-press-sequence
- choose:
- <<: *action-press-sequence
sequence: !input action_button_quadruple_press
- *action-repeat-delay
- conditions:
- condition: template
value_template: >-
{{ press_type == release_types[4] }}
sequence:
- *fire-event-update-helper
# run the fifth press action
- repeat:
<<: *action-scene-sequence
sequence:
- *scene-press-sequence
- choose:
- <<: *action-press-sequence
sequence: !input action_button_quintuple_press
- *action-repeat-delay
- conditions:
- condition: template
value_template: >-
{{ press_type == "long_repeat" }}
sequence:
- *fire-event-update-helper
# run the custom action
- choose:
- conditions: []
sequence: !input action_button_long_repeat
- conditions:
- condition: template
value_template: >-
{{ press_type == "long_release" }}
sequence:
- *fire-event-update-helper
# run the custom action
- choose:
- conditions: []
sequence: !input action_button_long_release