-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeconz_rules.sh
1453 lines (1263 loc) · 42.4 KB
/
deconz_rules.sh
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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
#
# deconz_rules.sh
# Copyright © 2017-2025 Erik Baauw. All rights reserved.
#
# Create/configure rules on a deCONZ gateway.
#
# Room Status:
# -2 Wakeup
# -1 Disabled
# 0 No presence
# 1 Presence
# 2 Pending no presence
# 3 Presence in adjacent room
# 4 TV
. deconz.sh
if [ $? -ne 0 ] ; then
_deconz_error "cannot load deconz.sh"
fi
# Usage: deconz_rule name conditions actions
function deconz_rule() {
local name="${1}"
local conditions="${2}"
local actions="${3}"
local body="{
\"name\": \"${name}\",
\"status\": \"enabled\",
\"conditions\": ${conditions},
\"actions\": ${actions}
}"
local -i id=$(deconz_unquote $(deconz -t 10 post "/rules" "${body}"))
if [ ${id} -eq 0 ] ; then
_deconz_error "cannot create rule \"${name}\""
json -c "${body}" >&2
return 1
fi
_deconz_info "/rules/${id}: \"${name}\""
}
# Usage: deconz_rules_delete
function deconz_rules_delete() {
local -i rule
local rules="$(deconz get -al /rules | grep /name: | cut -d / -f 2)"
local -i nrules=$(echo ${rules} | wc -w)
_deconz_info "deleting ${nrules} rules..."
for rule in ${rules}; do
deconz delete /rules/${rule}
done
deconz_restart
}
# Usage: nrules=$(deconz_rules_count)
function deconz_rules_count() {
local -i nrules=$(deconz get -al /rules | grep /name: | wc -l)
echo ${nrules}
}
# ===== Conditions =============================================================
# Usage: condition="$(deconz_condition address [[operator] value | 'dx'])"
function deconz_condition() {
if [ -z "${3}" ] ; then
if [ "${2}" == dx ] ; then
echo "{
\"address\": \"${1}\",
\"operator\": \"${2}\"
}"
else
echo "{
\"address\": \"${1}\",
\"operator\": \"eq\",
\"value\": \"${2}\"
}"
fi
else
echo "{
\"address\": \"${1}\",
\"operator\": \"${2}\",
\"value\": \"${3}\"
}"
fi
}
# Usage: condition="$(deconz_condition_sensor id attribute [[operator] value | 'dx'])"
function deconz_condition_sensor() {
deconz_condition "/sensors/${1}/state/${2}" "${3}" "${4}"
}
# Usage: condition="$(deconz_condition_sensor_config id attribute [[operator] value | 'dx'])"
function deconz_condition_sensor_config() {
deconz_condition "/sensors/${1}/config/${2}" "${3}" "${4}"
}
# Usage: condition="$(deconz_condition_light id attribute [[operator] value | 'dx'])"
function deconz_condition_light() {
deconz_condition "/lights/${1}/state/${2}" "${3}" "${4}"
}
# Usage: condition="$(deconz_condition_group id attribute [[operator] value | 'dx'])"
function deconz_condition_group() {
deconz_condition "/groups/${1}/state/${2}" "${3}" "${4}"
}
# Usage: condition="$(deconz_condition_config attribute [[operator] value | 'dx'])"
function deconz_condition_config() {
deconz_condition "/config/${1}" "${2}" "${3}"
}
# Usage: condition="$(deconz_condition_dx sensor [attribute])"
function deconz_condition_dx() {
deconz_condition_sensor "${1}" "${2:-lastupdated}" dx
}
# Usage: condition="$(deconz_condition_ddx sensor [attribute] time)"
function deconz_condition_ddx() {
if [ -z "${3}" ] ; then
deconz_condition_sensor "${1}" lastupdated ddx "PT${2}"
else
deconz_condition_sensor "${1}" "${2}" ddx "PT${3}"
fi
}
# Usage: condition="$(deconz_condition_on light [value])"
function deconz_condition_on() {
deconz_condition_light "${1}" on eq "${2:-true}"
}
# Usage: condition="$(deconz_condition_allon group [value])"
function deconz_condition_allon() {
deconz_condition_group "${1}" all_on eq "${2:-true}"
}
# Usage: condition="$(deconz_condition_anyon group [value])"
function deconz_condition_anyon() {
deconz_condition_group "${1}" any_on eq "${2:-true}"
}
# Usage: condition="$(deconz_condition_flag sensor [value])"
function deconz_condition_flag() {
deconz_condition_sensor "${1}" flag eq "${2:-true}"
}
# Usage: condition="$(deconz_condition_presence sensor [value])"
function deconz_condition_presence() {
deconz_condition_sensor "${1}" presence eq "${2:-true}"
}
# Usage: condition="$(deconz_condition_vibration sensor [value])"
function deconz_condition_vibration() {
deconz_condition_sensor "${1}" vibration eq "${2:-true}"
}
# Usage: condition="$(deconz_condition_dark sensor [value])"
function deconz_condition_dark() {
deconz_condition_sensor "${1}" dark eq "${2:-true}"
}
# Usage: condition="$(deconz_condition_daylight sensor [value])"
function deconz_condition_daylight() {
deconz_condition_sensor "${1}" daylight eq "${2:-true}"
}
# Usage: condition="$(deconz_condition_open sensor [value])"
function deconz_condition_open() {
deconz_condition_sensor "${1}" open eq "${2:-true}"
}
# Usage: condition="$(deconz_condition_status sensor [op] value)"
function deconz_condition_status() {
deconz_condition_sensor "${1}" status "${2}" "${3}"
}
# Usage: condition="$(deconz_condition_temperature sensor [op] value)"
function deconz_condition_temperature() {
deconz_condition_sensor "${1}" temperature "${2}" "${3}"
}
# Usage: condition="$(deconz_condition_humidity sensor [op] value)"
function deconz_condition_humidity() {
deconz_condition_sensor "${1}" humidity "${2}" "${3}"
}
# Usage: condition="$(deconz_condition_pressure sensor [op] value)"
function deconz_condition_pressure() {
deconz_condition_sensor "${1}" pressure "${2}" "${3}"
}
# Usage: condition="$(deconz_condition_localtime [op] from to)"
function deconz_condition_localtime() {
if [ -z "${3}" ] ; then
deconz_condition_config localtime in "T${1}/T${2}"
else
deconz_condition_config localtime "${1}" "T${2}/T${3}"
fi
}
# Usage: condition="$(deconz_condition_buttonevent sensor value [value])"
function deconz_condition_buttonevent() {
if [ -z "${3}" ] ; then
echo $(deconz_condition_sensor "${1}" buttonevent "${2}"),
else
echo $(deconz_condition_sensor "${1}" buttonevent gt "${2}"),
echo $(deconz_condition_sensor "${1}" buttonevent lt "${3}"),
fi
deconz_condition_dx "${1}"
}
# ===== Actions ================================================================
# Usage: action="$(deconz_action address [method] [body])"
function deconz_action() {
if [ -z "${3}" ] ; then
if [ -z "${2}" -o "${2}" == PUT -o "${2}" == POST ] ; then
echo "{
\"address\": \"${1}\",
\"method\": \"${2:-PUT}\"
}"
else
echo "{
\"address\": \"${1}\",
\"method\": \"PUT\",
\"body\": ${2}
}"
fi
else
echo "{
\"address\": \"${1}\",
\"method\": \"${2}\",
\"body\": ${3}
}"
fi
}
# Usage: action="$(deconz_action_sensor_state id [method] [body])"
function deconz_action_sensor_state() {
deconz_action "/sensors/${1}/state" "${2}" "${3}"
}
# Usage: action="$(deconz_action_sensor_config id [method] [body])"
function deconz_action_sensor_config() {
deconz_action "/sensors/${1}/config" "${2}" "${3}"
}
# Usage: action="$(deconz_action_light_state id [method] [body])"
function deconz_action_light_state() {
deconz_action "/lights/${1}/state" "${2}" "${3}"
}
# Usage: action="$(deconz_action_group_action id [method] [body])"
function deconz_action_group_action() {
deconz_action "/groups/${1}/action" "${2}" "${3}"
}
# Usage: condition="$(deconz_action_flag sensor [value])"
function deconz_action_flag() {
deconz_action_sensor_state "${1}" "{\"flag\": ${2:-true}}"
}
# Usage: action="$(deconz_action_status sensor value)"
function deconz_action_status() {
deconz_action_sensor_state "${1}" "{\"status\": ${2}}"
}
# Usage: action="$(deconz_action_light_on light [value])"
function deconz_action_light_on() {
deconz_action_light_state "${1}" "{\"on\": ${2:-true}}"
}
# Usage: action="$(deconz_action_light_bri light [value])"
function deconz_action_light_bri() {
deconz_action_light_state "${1}" "{\"bri\": ${2:-254}}"
}
# Usage: condition="$(deconz_action_blind_open blind [value])"
# function deconz_action_blind_open() {
# deconz_action_light_state "${1}" "{\"open\": ${2:-true}}"
# }
# Usage: condition="$(deconz_action_blind_lift blind [value])"
function deconz_action_blind_lift() {
deconz_action_light_state "${1}" "{\"lift\": ${2:-100}}"
}
# Usage: condition="$(deconz_action_blind_stop blind)"
function deconz_action_blind_stop() {
deconz_action_light_state "${1}" "{\"stop\": true}"
}
# Usage: condition="$(deconz_action_group_on group [value])"
function deconz_action_group_on() {
deconz_action_group_action "${1}" "{\"on\": ${2:-true}}"
}
# Usage: action="$(deconz_action_group_alert group [value])"
function deconz_action_group_alert() {
deconz_action_group_action "${1}" "{\"alert\": \"${2:-select}\"}"
}
# Usage: action="$(deconz_action_sensor_alert sensor [value])"
function deconz_action_sensor_alert() {
deconz_action_sensor_config "${1}" "{\"alert\": \"${2:-select}\"}"
}
# Usage: action=$(deconz_action_heatsetpoint sensor value)
function deconz_action_heatsetpoint() {
deconz_action_sensor_config "${1}" "{\"heatsetpoint\": ${2}}"
}
# Usage: action="$(deconz_action_light_dim group [up|down|stop|wakeup])"
function deconz_action_group_dim() {
local -i value
local -i tt
local -i hb
case "${2}" in
up) value=254 ; tt=30 ;;
down) value=-254 ; tt=30 ;;
stop) value=0 ; tt=0 ;;
wakeup) value=254 ; tt=6000 ;;
esac
deconz_action_group_action "${1}" "{\"bri_inc\": ${value}, \"transitiontime\": ${tt}}"
}
# Usage: action="$(deconz_action_scene group scene)"
function deconz_action_scene_recall() {
deconz_action "/groups/${1}/scenes/${2}/recall"
}
# ===== Mirror =================================================================
# deconz_rules_mirror name flag host apikey
function deconz_rules_mirror() {
local name="${1}"
local -i flag="${2}"
local host="${3}"
local apikey = "${4}"
deconz_rule "Mirror ${name} Off" "[
$(deconz_condition_flag ${flag} false)
]" "[
$(deconz_action http://${host}/api/${apikey}/sensors/${flag}/state PUT '{"flag": false}')
]"
deconz_rule "Mirror ${name} On" "[
$(deconz_condition_flag ${flag})
]" "[
$(deconz_action http://${host}/api/${apikey}/sensors/${flag}/state PUT '{"flag": true}')
]"
}
# ===== Boot ===================================================================
# On startup, all CLIP sensors are initialised to 0. Then the Hue bridge
# updates the built-in Daylight sensor. We can use this to detect that the
# Hue bridge has booted. We keep a CLIPGenericFlag sensor (boottime) which is
# set to true when the Daylight sensor changes while boottime is 0. As we never
# change boottime afterwards, it's state.lastupdated attribute reflects boot
# time.
# Assuming the Hue bridge reboots because power has just been restored after a
# power outage, we'll also set the power restored flag turn off all lights.
# Usage: deconz_rules_boottime boottime [flag] [daylight]
function deconz_rules_boottime() {
local -i boottime="${1}"
local -i flag="${2}"
local -i daylight="${3:-1}"
if [ -z "${2}" ] ; then
deconz_rule "Boot Time" "[
$(deconz_condition_dx ${daylight}),
$(deconz_condition_flag ${boottime} false)
]" "[
$(deconz_action_flag ${boottime})
]"
else
deconz_rule "Boot Time" "[
$(deconz_condition_dx ${daylight}),
$(deconz_condition_flag ${boottime} false)
]" "[
$(deconz_action_flag ${boottime}),
$(deconz_action_flag ${flag})
]"
fi
}
# ===== Night and Day ==========================================================
# Usage: deconz_rules_night night [daylight [morning evening]]
function deconz_rules_night() {
local -i night=${1}
local -i daylight=${2:-1}
local morning="${3:-07:00:00}"
local evening="${4:-23:00:00}"
deconz_rule "Daylight On" "[
$(deconz_condition_daylight ${daylight})
]" "[
$(deconz_action_flag ${night} false)
]"
deconz_rule "Night On" "[
$(deconz_condition_localtime ${evening} ${morning})
]" "[
$(deconz_action_flag ${night})
]"
}
# ===== Power Restore ==========================================================
# Usage: deconz_rules_power flag [group]
# Flag is true when power has been restored.
function deconz_rules_power() {
local -i flag="${1}"
local -i group="${2:-0}"
deconz_rule "Power 1/5" "[
$(deconz_condition_flag ${flag}),
$(deconz_condition_dx ${flag})
]" "[
$(deconz_action_group_on ${group} false)
]"
deconz_rule "Power 2/5" "[
$(deconz_condition_flag ${flag}),
$(deconz_condition_ddx ${flag} "00:00:02")
]" "[
$(deconz_action_group_on ${group} false)
]"
deconz_rule "Power 3/5" "[
$(deconz_condition_flag ${flag}),
$(deconz_condition_ddx ${flag} "00:00:04")
]" "[
$(deconz_action_group_on ${group} false)
]"
deconz_rule "Power 4/5" "[
$(deconz_condition_flag ${flag}),
$(deconz_condition_ddx ${flag} "00:00:06")
]" "[
$(deconz_action_group_on ${group} false)
]"
deconz_rule "Power 5/5" "[
$(deconz_condition_flag ${flag}),
$(deconz_condition_ddx ${flag} "00:00:08")
]" "[
$(deconz_action_group_on ${group} false),
$(deconz_action_flag ${flag} false)
]"
}
# ===== Room Status ============================================================
# For each room, we keep a CLIPGenericFlag sensor (flag), as virtual master
# switch, and a CLIPGenericStatus sensor (status), to store the room state:
#
# status state room automation
# ====== ========================= ==== ==========
# -2 wakeup in progress off off
# -1 disabled off off
# 0 no presence off on
# 1 presence on on
# 2 presence in adjacent room on on
# 3 pending no presence on on
# 4 tv on on off
#
# The status is maintained from motion sensors, door sensors, and switches.
# The flag is maintained automatically, from the status, or manually from
# HomeKit where the flag is exposed as switch. The lights are controlled from
# the flag (in combination with the time of day and lightlevel).
# Usage: deconz_rules_status room status flag [group]
function deconz_rules_status() {
local room="${1}"
local -i status=${2}
local -i flag=${3}
local -i group=${4}
deconz_rule "${room} Off" "[
$(deconz_condition_flag ${flag} false),
$(deconz_condition_dx ${flag}),
$(deconz_condition_status ${status} gt 0)
]" "[
$(deconz_action_status ${status} 0)
]"
deconz_rule "${room} Status <1" "[
$(deconz_condition_status ${status} lt 1),
$(deconz_condition_dx ${status})
]" "[
$(deconz_action_flag ${flag} false)
]"
deconz_rule "${room} Status >0" "[
$(deconz_condition_status ${status} gt 0),
$(deconz_condition_dx ${status})
]" "[
$(deconz_action_flag ${flag})
]"
if [ ! -z "${4}" ] ; then
deconz_rule "${room} Status 2" "[
$(deconz_condition_status ${status} 2),
$(deconz_condition_ddx ${status} "00:00:15")
]" "[
$(deconz_action_status ${status} 1)
]"
deconz_rule "${room} Status 3 (1/3)" "[
$(deconz_condition_status ${status} 3),
$(deconz_condition_ddx ${status} "00:04:00"),
$(deconz_condition_flag ${flag})
]" "[
$(deconz_action_group_alert ${group} breathe)
]"
deconz_rule "${room} Status 3 (2/3)" "[
$(deconz_condition_status ${status} 3),
$(deconz_condition_ddx ${status} "00:04:01"),
$(deconz_condition_flag ${flag})
]" "[
$(deconz_action_group_alert ${group} finish)
]"
deconz_rule "${room} Status 3 (3/3)" "[
$(deconz_condition_status ${status} 3),
$(deconz_condition_ddx ${status} "00:05:00")
]" "[
$(deconz_action_status ${status} 0)
]"
fi
}
# For each room, we keep a CLIPGenericFlag sensor (flag), as virtual master
# switch, and a CLIPGenericStatus sensor (status), to store the room state:
#
# status state room automation timeout
# ====== ========================= ==== ========== =======
# -2 wakeup in progress off off yes
# -1 disabled off off no
# 0 no presence off on no
# 1 no presence on on yes
# 2 presence on on no
# 3 not used on on no
# 4 tv on on off no
#
# The status is maintained from motion sensors, door sensors, and switches.
# The flag is maintained automatically, from the status, or manually from
# HomeKit where the flag is exposed as switch. The lights are controlled from
# the flag (in combination with the time of day and lightlevel).
# Usage: deconz_rules_status_timeout room status flag [timeout]
function deconz_rules_status_timeout()
{
local room="${1}"
local -i status=${2}
local -i flag=${3}
local timeout=${4:-00:05:00}
deconz_rule "${room} Off" "[
$(deconz_condition_flag ${flag} false),
$(deconz_condition_dx ${flag}),
$(deconz_condition_status ${status} gt 0)
]" "[
$(deconz_action_status ${status} 0)
]"
deconz_rule "${room} On" "[
$(deconz_condition_flag ${flag}),
$(deconz_condition_dx ${flag}),
$(deconz_condition_status ${status} eq 0)
]" "[
$(deconz_action_status ${status} 1)
]"
deconz_rule "${room} Status <1" "[
$(deconz_condition_status ${status} lt 1),
$(deconz_condition_dx ${status})
]" "[
$(deconz_action_flag ${flag} false)
]"
deconz_rule "${room} Status >0" "[
$(deconz_condition_status ${status} gt 0),
$(deconz_condition_dx ${status})
]" "[
$(deconz_action_flag ${flag})
]"
deconz_rule "${room} Status 1" "[
$(deconz_condition_status ${status} eq 1),
$(deconz_condition_ddx ${status} ${timeout})
]" "[
$(deconz_action_status ${status} 0)
]"
}
# Usage: deconz_rules_wakeup room status flag group night scene
function deconz_rules_wakeup() {
local room="${1}"
local -i status=${2}
local -i flag=${3}
local -i group=${4}
local -i night=${5}
local scene="${6}"
deconz_rule "${room} Wakeup 1/3" "[
$(deconz_condition_status ${status} -2)
]" "[
$(deconz_action_scene_recall ${group} ${scene}),
$(deconz_action_flag ${night} false)
]"
deconz_rule "${room} Wakeup 2/3" "[
$(deconz_condition_status ${status} -2),
$(deconz_condition_ddx ${status} status "00:00:10")
]" "[
$(deconz_action_group_dim ${group} wakeup)
]"
deconz_rule "${room} Wakeup 3/3" "[
$(deconz_condition_status ${status} -2),
$(deconz_condition_ddx ${status} status "00:10:10")
]" "[
$(deconz_action_status ${status} 1)
]"
}
# ===== Motion Sensors =========================================================
# We use two different patterns for motion sensors:
# - For hallways (where you never sit still), the room status is set to no
# presence when motion hasn't been detected for 1 minute;
# - For rooms (where you sit still), the room is set to no presence when
# motion hasn't been detected for 5 minutes, after some-one has left the room.
# This is detected by a sequence of motion detected in room (status 1), motion
# detected in adjacent room (status 2), no more motion detected in room within
# 15 seconds (status 3). A warning is given at 1 minute's notice before the
# lights are turned off.
# Usage: deconz_rules_motion room status motion [timeout]
function deconz_rules_motion() {
local room="${1}"
local -i status=${2}
local -i motion=${3}
local timeout=${4}
if [ -z "${timeout}" ] ; then
deconz_rule "${room} Motion Clear" "[
$(deconz_condition_presence ${motion} false),
$(deconz_condition_dx ${motion} presence),
$(deconz_condition_status ${status} 2)
]" "[
$(deconz_action_status ${status} 3)
]"
deconz_rule "${room} No Motion" "[
$(deconz_condition_presence ${motion} false),
$(deconz_condition_ddx ${motion} presence "00:55:00"),
$(deconz_condition_status ${status} gt 0),
$(deconz_condition_status ${status} lt 4)
]" "[
$(deconz_action_status ${status} 3)
]"
else
deconz_rule "${room} Motion Clear" "[
$(deconz_condition_presence ${motion} false),
$(deconz_condition_ddx ${motion} presence ${timeout}),
$(deconz_condition_status ${status} gt 0),
$(deconz_condition_status ${status} lt 4)
]" "[
$(deconz_action_status ${status} 0)
]"
fi
deconz_rule "${room} Motion Detected" "[
$(deconz_condition_presence ${motion}),
$(deconz_condition_dx ${motion} presence),
$(deconz_condition_status ${status} gt -1),
$(deconz_condition_status ${status} lt 4)
]" "[
$(deconz_action_status ${status} 1)
]"
}
# Usage: deconz_rules_motion_timeout room status motion
function deconz_rules_motion_timeout() {
local room="${1}"
local -i status=${2}
local -i motion=${3}
deconz_rule "${room} Motion Detected" "[
$(deconz_condition_presence ${motion}),
$(deconz_condition_dx ${motion}),
$(deconz_condition_status ${status} gt -1),
$(deconz_condition_status ${status} lt 4)
]" "[
$(deconz_action_status ${status} 2)
]"
deconz_rule "${room} Motion Clear" "[
$(deconz_condition_presence ${motion} false),
$(deconz_condition_dx ${motion}),
$(deconz_condition_status ${status} 2)
]" "[
$(deconz_action_status ${status} 1)
]"
}
# Usage: deconz_rules_motion room status presence motion
function deconz_rules_presence() {
local room="${1}"
local -i status=${2}
local -i presence=${3}
local -i motion=${4}
deconz_rule "${room} Motion Detected" "[
$(deconz_condition_presence ${motion}),
$(deconz_condition_dx ${motion} presence),
$(deconz_condition_status ${status} gt -1),
$(deconz_condition_status ${status} lt 4)
]" "[
$(deconz_action_status ${status} 1)
]"
# ddx condition as workaround for deCONZ DDF bug
deconz_rule "${room} Presence Clear" "[
$(deconz_condition_presence ${presence} false),
$(deconz_condition_presence ${motion} false),
$(deconz_condition_ddx ${motion} presence "00:00:45"),
$(deconz_condition_status ${status} gt 0),
$(deconz_condition_status ${status} lt 4)
]" "[
$(deconz_action_status ${status} 0)
]"
}
# Usage: deconz_rules_vibration room status vibration [timeout]
function deconz_rules_vibration() {
local room="${1}"
local -i status=${2}
local -i vibration=${3}
local timeout=${4}
deconz_rule "${room} Vibration Detected" "[
$(deconz_condition_vibration ${vibration}),
$(deconz_condition_dx ${vibration} vibration),
$(deconz_condition_status ${status} gt 0),
$(deconz_condition_status ${status} lt 4)
]" "[
$(deconz_action_status ${status} 1)
]"
}
# Usage: deconz_rules_room room status room2 motion
function deconz_rules_leave_room() {
local room="${1}"
local -i status=${2}
local room2="${3}"
local -i motion=${4}
deconz_rule "${room} to ${room2}" "[
$(deconz_condition_presence ${motion}),
$(deconz_condition_dx ${motion} presence),
$(deconz_condition_status ${status} 1)
]" "[
$(deconz_action_status ${status} 2)
]"
}
# ===== Door Sensors ===========================================================
# Usage: deconz_rules_room room status door [noclose]
function deconz_rules_door() {
local room="${1}"
local -i status=${2}
local -i door=${3}
local noclose="${4}"
if [ -z "${noclose}" ] ; then
deconz_rule "${room} Door Close" "[
$(deconz_condition_open ${door} false),
$(deconz_condition_dx ${door} open),
$(deconz_condition_status ${status} gt -1)
]" "[
$(deconz_action_status ${status} 0)
]"
fi
deconz_rule "${room} Door Open" "[
$(deconz_condition_open ${door}),
$(deconz_condition_dx ${door} open),
$(deconz_condition_status ${status} gt -1)
]" "[
$(deconz_action_status ${status} 1)
]"
}
# ===== Switches ===============================================================
# Usage: deconz_rules_dimmer_onoff room status flag dimmer motion group scene
function deconz_rules_dimmer_onoff() {
local room="${1}"
local -i status=${2}
local -i flag=${3}
local -i dimmer=${4}
local -i motion=${5}
local -i group=${6}
local -i scene=${7}
deconz_rule "${room} Dimmer Off Press" "[
$(deconz_condition_buttonevent ${dimmer} 4002)
]" "[
$(deconz_action_status ${status} 0)
]"
# Disable room automation. Flash motion sensor light as confirmation.
deconz_rule "${room} Dimmer Off Hold" "[
$(deconz_condition_buttonevent ${dimmer} 4001)
]" "[
$(deconz_action_sensor_alert ${motion}),
$(deconz_action_status ${status} -1)
]"
deconz_rule "${room} Dimmer On Press" "[
$(deconz_condition_buttonevent ${dimmer} 1002)
]" "[
$(deconz_action_status ${status} 1)
]"
# Set default scene.
deconz_rule "${room} Dimmer On Hold" "[
$(deconz_condition_buttonevent ${dimmer} 1001)
]" "[
$(deconz_action_scene_recall ${group} ${scene})
]"
}
# Usage: deconz_rules_dimmer2_onoff room status flag dimmer group scene
function deconz_rules_dimmer2_onoff() {
local room="${1}"
local -i status=${2}
local -i flag=${3}
local -i dimmer=${4}
local -i group=${5}
local -i scene=${6}
local -i offGroup=${7:-${5}}
# Set room off after dimmer sends _Off_
deconz_rule "${room} Dimmer OnOff Press Off" "[
$(deconz_condition_sensor ${dimmer} buttonevent 1002),
$(deconz_condition_ddx ${dimmer} buttonevent "00:00:01"),
$(deconz_condition_allon ${group} false)
]" "[
$(deconz_action_status ${status} 0)
]"
# Set room on after dimmer sends _On_
deconz_rule "${room} Dimmer OnOff Press On" "[
$(deconz_condition_sensor ${dimmer} buttonevent 1002),
$(deconz_condition_ddx ${dimmer} buttonevent "00:00:01"),
$(deconz_condition_allon ${group})
]" "[
$(deconz_action_status ${status} 1)
]"
# Set group off off.
deconz_rule "${room} Dimmer OnOff Hold Off" "[
$(deconz_condition_buttonevent ${dimmer} 1001),
$(deconz_condition_status ${status} lt 1)
]" "[
$(deconz_action_group_action "${group}" "{\"on\": false}")
]"
# Set default scene.
deconz_rule "${room} Dimmer OnOff Hold On" "[
$(deconz_condition_buttonevent ${dimmer} 1001),
$(deconz_condition_status ${status} gt 0)
]" "[
$(deconz_action_scene_recall ${group} ${scene})
]"
}
# Usage: deconz_rules_dimmer2_hue room status flag dimmer group scene
function deconz_rules_dimmer2_hue() {
local room="${1}"
local -i status=${2}
local -i flag=${3}
local -i dimmer=${4}
local -i group=${5}
local -i scene=${6}
deconz_rule "${room} Dimmer Hue Press Off" "[
$(deconz_condition_buttonevent ${dimmer} 4002),
$(deconz_condition_status ${status} gt 0)
]" "[
$(deconz_action_status ${status} 0)
]"
deconz_rule "${room} Dimmer Hue Press On" "[
$(deconz_condition_buttonevent ${dimmer} 4002),
$(deconz_condition_status ${status} lt 1)
]" "[
$(deconz_action_status ${status} 1)
]"
# Set extended room off.
deconz_rule "${room} Dimmer Hue Hold Off" "[
$(deconz_condition_buttonevent ${dimmer} 4001),
$(deconz_condition_status ${status} lt 1)
]" "[
$(deconz_action_group_action "${group}" "{\"on\": false}")
]"
# Set default scene.
deconz_rule "${room} Dimmer Hue Hold On" "[
$(deconz_condition_buttonevent ${dimmer} 4001),
$(deconz_condition_status ${status} gt 0)
]" "[
$(deconz_action_scene_recall ${group} ${scene})
]"
}
# Usage: deconz_rules_switch_toggle room status switch off
function deconz_rules_switch_toggle() {
local room="${1}"
local -i status=${2}
local -i switch=${3}
local -i off=${4:-0}
deconz_rule "${room} Switch On/Off Press (1/2)" "[
$(deconz_condition_buttonevent ${switch} 1002),
$(deconz_condition_status ${status} gt 0)
]" "[
$(deconz_action_status ${status} ${off})
]"
deconz_rule "${room} Switch On/Off Press (2/2)" "[
$(deconz_condition_buttonevent ${switch} 1002),
$(deconz_condition_status ${status} lt 1)
]" "[
$(deconz_action_status ${status} 1)
]"
}
# Usage: deconz_rules_wall_module room status switch [left|right] [off]
function deconz_rules_wall_module() {
local room="${1}"
local -i status=${2}
local -i switch=${3}
case "${4}" in
"left") button=1; name=" Left" ;;
"right") button=2; name=" Right" ;;
"") button=1; name=" Button" ;;
esac
local -i off=${5:-0}
deconz_rule "${room} Switch${name} Press (1/2)" "[
$(deconz_condition_buttonevent ${switch} ${button}002),
$(deconz_condition_status ${status} gt 0)
]" "[
$(deconz_action_status ${status} ${off})
]"
deconz_rule "${room} Switch${name} Press (2/2)" "[
$(deconz_condition_buttonevent ${switch} ${button}002),
$(deconz_condition_status ${status} lt 1)
]" "[
$(deconz_action_status ${status} 1)
]"
}
# Usage: deconz_rules_switch_foh room status switch group [left|right|both] [off]
function deconz_rules_switch_foh() {
local room="${1}"
local -i status=${2}
local -i switch=${3}
local -i group=${4}
case "${5}" in
"left") up=1; down=2; name=" Left" ;;
"right") up=3; down=4; name=" Right" ;;
"both") up=5; down=6; name=" Both" ;;
"") up=1; down=2; name="" ;;
esac
local -i off=${6:-0}
deconz_rule "${room} Switch Up${name} Press" "[
$(deconz_condition_buttonevent ${switch} ${up}002)
]" "[
$(deconz_action_status ${status} 1)
]"
deconz_rule "${room} Switch Up${name} Hold" "[
$(deconz_condition_buttonevent ${switch} ${up}001)
]" "[
$(deconz_action_group_on "${group}"),
$(deconz_action_group_dim "${group}" up)
]"
deconz_rule "${room} Switch Up${name} Release" "[