forked from adeparker/RuleMachine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rule.groovy
2730 lines (2569 loc) · 132 KB
/
Rule.groovy
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
/**
* Rule
*
* Copyright 2015, 2016 Bruce Ravenel
*
* Version 1.9.1a 26 Mar 2016
*
* Version History
*
* 1.9.1 26 Mar 2016 Added yearly period, and two periodic trigger events
* 1.9.0 24 Mar 2016 Added periodic trigger, disable logging, bug fixes re ZWN buttons
* 1.8.6 19 Mar 2016 Bug fixes re private Boolean as trigger event
* 1.8.5 13 Mar 2016 Added support for single button device, more private Boolean options, emergency heat
* 1.8.4 11 Mar 2016 Strengthened code pertaining to evaluation of malformed rules
* 1.8.3 3 Mar 2016 Changed method of reporting version number to Rule Machine
* 1.8.2 2 Mar 2016 Reorganized UI for selecting Actions, group pages, for speed of mobile app
* 1.8.1 2 Mar 2016 Added NOT for rules: NOT Condition and NOT (sub-rule)
* 1.8.0 2 Mar 2016 Major code cleanup; added random color, decimal for energy & power, and door control
* 1.7.16 28 Feb 2016 Added cancel for delay set private, and delay for restore with cancel
* 1.7.15 24 Feb 2016 Minor UI cleanup, bug fixes
* 1.7.14 23 Feb 2016 Added adjust thermostat setpoints, delay Set Private Boolean
* 1.7.13 21 Feb 2016 Improved custom command selection
* 1.7.12 19 Feb 2016 Added Private Boolean enable/disable, capture/restore color hue and saturation
* 1.7.11 15 Feb 2016 Further UI redesign to better distinguish triggers, added seconds for delayed on/off
* 1.7.10 9 Feb 2016 Added Music player condition, fixed Days of Week schedule bug
* 1.7.9 8 Feb 2016 Added set Boolean for other Rules, and Send notification event
* 1.7.8 7 Feb 2016 Added Evaluate Rule after Delay (loop possible), and Private Boolean
* 1.7.7 6 Feb 2016 UI cleanup and organization, added capture/restore for switches/dimmers
* 1.7.6 5 Feb 2016 Added action to update rule(s) to fix broken schedules due to ST issues
* 1.7.5 3 Feb 2016 Removed use of unschedule() for delay cancel, to avoid ST issues
* 1.7.4 2 Feb 2016 Redesign of UI to make it clearer between Triggers and Rules
* 1.7.3 2 Feb 2016 Bug fix for multi-button device with more than 4 buttons
* 1.7.2 31 Jan 2016 Added mode based dimming action, and cause rule actions action
* 1.7.1 30 Jan 2016 Added support for more buttons than 4 on button device, now as many as 20
* 1.7.0 27 Jan 2016 Fixed thermostat mode trigger/condition, added thermostat operating state condition
* 1.6.13 17 Jan 2016 Added Text to speech support
* 1.6.12 10 Jan 2016 Bug fix re removing parts of a rule
* 1.6.11 8 Jan 2016 Added offset to compare to device, fixed bugs in compare to device
* 1.6.10 6 Jan 2016 Returned Delay on/off pending cancel per user request, further debug of rule evaluation
* 1.6.9 6 Jan 2016 Fixed bugs related to presence in triggers, added Off as disable option, fixed bug in rule evaluation
* 1.6.8 1 Jan 2016 Added version numbers to main Rule Machine page, multi SMS
* 1.6.7 31 Dec 2015 Added speak to send message
* 1.6.6 30 Dec 2015 Expert multi-commands added per Maxwell
* 1.6.5 29 Dec 2015 Added action to set dimmers from a track dimmer, restored turn on/off after delay action
* 1.6.4 29 Dec 2015 Added action to adjust dimmers +/-, fixed time bug for triggered rule, fixed dimmer level condition bug
* 1.6.3 26 Dec 2015 Added color temperature bulb set, per John-Paul Smith
* 1.6.2 26 Dec 2015 New delay selection, minor bug fixes, sub-rule input improvements
* 1.6.1 24 Dec 2015 Added ability to send device name with push or SMS, show rule truth on main page
* 1.6.0 23 Dec 2015 Added expert commands per Mike Maxwell, and actions for camera to take photo burst
* 1.5.11 23 Dec 2015 Fixed bug that prevented old triggers from running, minor UI change for rule display
* 1.5.10 22 Dec 2015 Require capability choice for all but last rule or trigger
* 1.5.9 21 Dec 2015 Fixed overlap of Days of Week selection
* 1.5.8 20 Dec 2015 More repair for that same mode bug; fixed so triggered-rule not tested at install
* 1.5.7 19 Dec 2015 Fixed bug re: selecting mode as condition/trigger, UI display
* 1.5.6 18 Dec 2015 Fixed bug re: old triggers not editable
* 1.5.5 17 Dec 2015 Added milliseconds to Delayed off, uses dev.off([delay: msec]) instead of runIn()
*
* This software if free for Private Use. You may use and modify the software without distributing it.
*
* This software and derivatives may not be used for commercial purposes.
* You may not modify, distribute or sublicense this software.
* You may not grant a sublicense to modify and distribute this software to third parties not included in the license.
*
* Software is provided without warranty and the software author/license owner cannot be held liable for damages.
*
*/
definition(
name: "Rule",
namespace: "bravenel",
author: "Bruce Ravenel",
description: "Rule",
category: "Convenience",
parent: "bravenel:Rule Machine",
iconUrl: "https://raw.githubusercontent.com/bravenel/Rule-Trigger/master/smartapps/bravenel/RuleMachine.png",
iconX2Url: "https://raw.githubusercontent.com/bravenel/Rule-Trigger/master/smartapps/bravenel/RuleMachine%402x.png",
)
preferences {
page(name: "mainPage")
page(name: "selectTrig")
page(name: "selectCTrig")
page(name: "selectRule")
page(name: "selectActions")
page(name: "selectTriggers")
page(name: "selectConditions")
page(name: "defineRule")
page(name: "certainTime")
page(name: "certainTimeX")
page(name: "atCertainTime")
page(name: "periodic")
page(name: "selectActionsTrue")
page(name: "selectActionsFalse")
page(name: "delayTruePage")
page(name: "delayFalsePage")
page(name: "switchTruePage")
page(name: "switchFalsePage")
page(name: "dimmerTruePage")
page(name: "dimmerFalsePage")
page(name: "doorTruePage")
page(name: "doorFalsePage")
page(name: "modeTruePage")
page(name: "modeFalsePage")
page(name: "ruleTruePage")
page(name: "ruleFalsePage")
page(name: "selectMsgTrue")
page(name: "selectMsgFalse")
page(name: "selectCustomActions")
}
//
//
//
def appVersion() {
return "1.9.1a"
}
def mainPage() {
//expert settings for rule
try {
state.isExpert = parent.isExpert()
if (state.isExpert) state.cstCmds = parent.getCommands()
else state.cstCmds = []
}
catch (e) {log.error "Please update Rule Machine to V1.6 or later"}
if(state.private == null) state.private = "true"
if(state.logging == null) state.logging = true
def myTitle = "Define a Rule, Trigger or Actions\n"
if(state.howManyT > 1 || state.isTrig) myTitle = "Define a Trigger"
else if(state.howMany > 1) myTitle = "Define a Rule"
else if(app.label != null) myTitle = "Define Actions"
def myUninstall = state.isTrig || state.isRule || state.howManyT > 1 || state.howMany > 1 || (app.label != "Rule" && app.label != null)
dynamicPage(name: "mainPage", title: myTitle, uninstall: myUninstall, install: myUninstall) {
if(state.isTrig) { // old Trigger
section() {
label title: "Name the Trigger", required: true
def condLabel = conditionLabel()
href "selectConditions", title: "Select Trigger Events", description: condLabel ? (condLabel) : "Tap to set", required: true, state: condLabel ? "complete" : null, submitOnChange: true
href "selectActionsTrue", title: "Select Actions", description: state.actsTrue ? state.actsTrue : "Tap to set", state: state.actsTrue ? "complete" : null
}
section(title: "Restrictions", hidden: hideOptionsSection(), hideable: true) {
def timeLabel = timeIntervalLabel()
href "certainTime", title: "Only during a certain time", description: timeLabel ?: "Tap to set", state: timeLabel ? "complete" : null
input "days", "enum", title: "Only on certain days of the week", multiple: true, required: false,
options: ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
input "modesY", "mode", title: "Only when mode is", multiple: true, required: false
input "disabled", "capability.switch", title: "Switch to disable trigger when ON", required: false, multiple: false
}
} else if(state.isRule) { // old Rule
section() {
label title: "Name the Rule", required: true
def condLabel = conditionLabel()
href "selectConditions", title: "Select Conditions", description: condLabel ? (condLabel) : "Tap to set", required: true, state: condLabel ? "complete" : null, submitOnChange: true
href "defineRule", title: "Define the Rule", description: state.str ? (state.str) : "Tap to set", state: state.str ? "complete" : null, submitOnChange: true
href "selectActionsTrue", title: "Select Actions for True", description: state.actsTrue ? state.actsTrue : "Tap to set", state: state.actsTrue ? "complete" : null, submitOnChange: true
href "selectActionsFalse", title: "Select Actions for False", description: state.actsFalse ? state.actsFalse : "Tap to set", state: state.actsFalse ? "complete" : null, submitOnChange: true
}
section(title: "Restrictions", hidden: hideOptionsSection(), hideable: true) {
input "modesZ", "mode", title: "Evaluate only when mode is", multiple: true, required: false
input "disabled", "capability.switch", title: "Switch to disable rule when ON", required: false, multiple: false
}
}
else if(state.howMany > 1 && state.howManyT in [null, 1]) getRule() // Existing Rule
else if(state.howManyT > 1 && state.howMany in [null, 1]) getTrigger() // Existing Trigger
else if(state.howManyT > 1) getCTrigger() // Existing Conditional Trigger
else if(app.label != "Rule" && app.label != null) getActions() // Existing Actions
else { // New Rule, Trigger, Conditional Trigger or Actions
section("A Rule uses events for conditions and then\ntests a rule to run actions") {href "selectRule", title: "Define a Rule", description: "Tap to set"}
section("A Trigger uses events to run actions") {href "selectTrig", title: "Define a Trigger", description: "Tap to set"}
section("A Conditional Trigger uses events to run actions\nbased on conditions tested under a rule") {href "selectCTrig", title: "Define a Conditional Trigger", description: "Tap to set"}
section("Other Rules can run these Actions") {href "selectActions", title: "Define Actions", description: "Tap to set"}
}
}
}
def selectRule() {
dynamicPage(name: "selectRule", title: "Select Conditions, Rule and Actions", uninstall: true, install: true) {
getRule()
}
}
def selectTrig() {
dynamicPage(name: "selectTrig", title: "Select Trigger Events and Actions", uninstall: true, install: true) {
getTrigger()
}
}
def selectCTrig() {
dynamicPage(name: "selectCTrig", title: "Select Triggers, Conditions, Rule and Actions", uninstall: true, install: true) {
getCTrigger()
}
}
def selectActions() {
dynamicPage(name: "selectActions", title: "Select Actions", uninstall: true, install: true) {
getActions()
}
}
def getRule() {
section() {
label title: "Name the Rule", required: true
def condLabel = conditionLabel()
href "selectConditions", title: "Select Conditions ", description: condLabel ? (condLabel) : "Tap to set", state: condLabel ? "complete" : null, submitOnChange: true
def ruleLabel = rulLabl()
href "defineRule", title: "Define Rule", description: ruleLabel ? (ruleLabel) : "Tap to set", state: ruleLabel ? "complete" : null, submitOnChange: true
href "selectActionsTrue", title: "Select Actions for True", description: state.actsTrue ? state.actsTrue : "Tap to set", state: state.actsTrue ? "complete" : null, submitOnChange: true
href "selectActionsFalse", title: "Select Actions for False", description: state.actsFalse ? state.actsFalse : "Tap to set", state: state.actsFalse ? "complete" : null, submitOnChange: true
}
getMoreOptions()
}
def getTrigger() {
section() {
label title: "Name the Trigger", required: true
def trigLabel = triggerLabel()
href "selectTriggers", title: "Select Trigger Events", description: trigLabel ? (trigLabel) : "Tap to set", state: trigLabel ? "complete" : null, submitOnChange: true
href "selectActionsTrue", title: "Select Actions", description: state.actsTrue ? state.actsTrue : "Tap to set", state: state.actsTrue ? "complete" : null, submitOnChange: true
}
getMoreOptions()
}
def getCTrigger() {
section() {
label title: "Name the Conditional Trigger", required: true
def trigLabel = triggerLabel()
href "selectTriggers", title: "Select Trigger Events", description: trigLabel ? (trigLabel) : "Tap to set", state: trigLabel ? "complete" : null, submitOnChange: true
def condLabel = conditionLabel()
href "selectConditions", title: "Select Conditions ", description: condLabel ? (condLabel) : "Tap to set", state: condLabel ? "complete" : null, submitOnChange: true
def ruleLabel = rulLabl()
href "defineRule", title: "Define Rule", description: ruleLabel ? (ruleLabel) : "Tap to set", state: ruleLabel ? "complete" : null, submitOnChange: true
href "selectActionsTrue", title: "Select Actions for True", description: state.actsTrue ? state.actsTrue : "Tap to set", state: state.actsTrue ? "complete" : null, submitOnChange: true
href "selectActionsFalse", title: "Select Actions for False", description: state.actsFalse ? state.actsFalse : "Tap to set", state: state.actsFalse ? "complete" : null, submitOnChange: true
}
getMoreOptions()
}
def getActions() {
section() {
label title: "Name the Actions", required: true
href "selectActionsTrue", title: "Select Actions", description: state.actsTrue ? state.actsTrue : "Tap to set", state: state.actsTrue ? "complete" : null, submitOnChange: true
}
getMoreOptions()
}
def getMoreOptions() {
section(title: "Restrictions", hidden: hideOptionsSection(), hideable: true) {
def timeLabel = timeIntervalLabel()
href "certainTime", title: "Only between two times", description: timeLabel ?: "Tap to set", state: timeLabel ? "complete" : null
input "daysY", "enum", title: "Only on certain days of the week", multiple: true, required: false,
options: ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
input "modesY", "mode", title: "Only when mode is", multiple: true, required: false
input "disabled", "capability.switch", title: "Switch to disable Rule", required: false, multiple: false
if(disabled) input "disabledOff", "bool", title: "Disable when Off? On is default", required: false, defaultValue: false
def privy = state.private
input "usePrivateDisable", "bool", title: "Enable/Disable with private Boolean? [$privy]", required: false
input "enableLogging", "bool", title: "Enable/Disable Logging", required: false, defaultValue: true
}
}
def certainTime() {
dynamicPage(name: "certainTime", title: "Between two times", uninstall: false) {
section() {
input "startingX", "enum", title: "Starting at", options: ["A specific time", "Sunrise", "Sunset"], defaultValue: "A specific time", submitOnChange: true, required: false
if(startingX in [null, "A specific time"]) input "starting", "time", title: "Start time", required: false
else {
if(startingX == "Sunrise") input "startSunriseOffset", "number", range: "*..*", title: "Offset in minutes (+/-)", required: false
else if(startingX == "Sunset") input "startSunsetOffset", "number", range: "*..*", title: "Offset in minutes (+/-)", required: false
}
}
section() {
input "endingX", "enum", title: "Ending at", options: ["A specific time", "Sunrise", "Sunset"], defaultValue: "A specific time", submitOnChange: true, required: false
if(endingX in [null, "A specific time"]) input "ending", "time", title: "End time", required: false
else {
if(endingX == "Sunrise") input "endSunriseOffset", "number", range: "*..*", title: "Offset in minutes (+/-)", required: false
else if(endingX == "Sunset") input "endSunsetOffset", "number", range: "*..*", title: "Offset in minutes (+/-)", required: false
}
}
}
}
// Trigger and Condition input code follows
def selectTriggers() {
selectTrigCond(true)
}
def selectConditions() {
selectTrigCond(false)
}
def selectTrigCond(isTrig) {
def ctStr = isTrig ? "tCapab" : "rCapab"
def ct = settings.findAll{it.key.startsWith(ctStr)}
def howMany = ct.size() + 1
if(isTrig) state.howManyT = howMany
else state.howMany = howMany
def excludes = (state.isTrig || isTrig) ? ["Certain Time", "Periodic", "Mode", "Routine", "Button", "Smart Home Monitor", "Private Boolean"] : ["Time of day", "Days of week", "Mode", "Smart Home Monitor", "Private Boolean"]
def pageName = isTrig ? "selectTriggers" : "selectConditions"
dynamicPage(name: pageName, title: (state.isTrig || isTrig) ? "Select Trigger Events (ANY will trigger)" : "Select Conditions", uninstall: false) {
for (int i = 1; i <= howMany; i++) {
def thisCapab = isTrig ? "tCapab$i" : "rCapab$i"
section((state.isTrig || isTrig) ? "Event Trigger #$i" : "Condition #$i") {
getCapab(thisCapab, isTrig, i < howMany)
def myCapab = settings.find {it.key == thisCapab}
if(myCapab) {
def xCapab = myCapab.value
if(!(xCapab in excludes)) {
def thisDev = isTrig ? "tDev$i" : "rDev$i"
getDevs(xCapab, thisDev, true)
def myDev = settings.find {it.key == thisDev}
if(myDev) if(myDev.value.size() > 1 && !isTrig) getAnyAll(thisDev)
if(xCapab in ["Temperature", "Humidity", "Illuminance", "Dimmer level", "Energy meter", "Power meter", "Battery"]) getRelational(thisDev)
} else if(xCapab == "Button") getButton(isTrig ? "tDev$i" : "rDev$i")
getState(xCapab, i, isTrig)
}
}
}
}
}
def getCapab(myCapab, isTrig, isReq) {
def myOptions = null
if(state.isRule || !isTrig) myOptions = ["Acceleration", "Battery", "Carbon monoxide detector", "Contact", "Days of week", "Dimmer level", "Energy meter", "Garage door", "Humidity", "Illuminance", "Lock",
"Mode", "Motion", "Power meter", "Presence", "Rule truth", "Smart Home Monitor", "Smoke detector", "Switch", "Temperature", "Private Boolean", "Door",
"Thermostat Mode", "Thermostat State", "Time of day", "Water sensor", "Music player"]
if(state.isTrig || isTrig) myOptions = ["Acceleration", "Battery", "Button", "Carbon monoxide detector", "Certain Time", "Periodic", "Contact", "Dimmer level", "Energy meter", "Garage door", "Humidity", "Illuminance",
"Lock", "Mode", "Motion", "Physical Switch", "Power meter", "Presence", "Routine", "Rule truth", "Smart Home Monitor", "Smoke detector", "Switch", "Temperature", "Door",
"Thermostat Mode", "Thermostat State", "Water sensor", "Private Boolean", "Music player"]
def result = input myCapab, "enum", title: "Select capability", required: isReq, options: myOptions.sort(), submitOnChange: true
}
def getDevs(myCapab, dev, multi) {
def thisName = ""
def thisCapab = ""
switch(myCapab) {
case "Switch":
thisName = "Switches"
thisCapab = "switch"
break
case "Physical Switch":
thisName = "Switches"
thisCapab = "switch"
break
case "Motion":
thisName = "Motion sensors"
thisCapab = "motionSensor"
break
case "Acceleration":
thisName = "Acceleration sensors"
thisCapab = "accelerationSensor"
break
case "Contact":
thisName = "Contact sensors"
thisCapab = "contactSensor"
break
case "Presence":
thisName = "Presence sensors"
thisCapab = "presenceSensor"
break
case "Garage door":
thisName = "Garage doors"
thisCapab = "garageDoorControl"
break
case "Door":
thisName = "Doors"
thisCapab = "doorControl"
break
case "Lock":
thisName = "Locks"
thisCapab = "lock"
break
case "Dimmer level":
thisName = "Dimmer" + (multi ? "s" : "")
thisCapab = "switchLevel"
break
case "Temperature":
thisName = "Temperature sensor" + (multi ? "s" : "")
thisCapab = "temperatureMeasurement"
break
case "Thermostat Mode":
thisName = "Thermostat" + (multi ? "s" : "")
thisCapab = "thermostat"
break
case "Thermostat State":
thisName = "Thermostat" + (multi ? "s" : "")
thisCapab = "thermostat"
break
case "Humidity":
thisName = "Humidity sensor" + (multi ? "s" : "")
thisCapab = "relativeHumidityMeasurement"
break
case "Illuminance":
thisName = "Illuminance sensor" + (multi ? "s" : "")
thisCapab = "illuminanceMeasurement"
break
case "Energy meter":
thisName = "Energy meter" + (multi ? "s" : "")
thisCapab = "energyMeter"
break
case "Power meter":
thisName = "Power meter" + (multi ? "s" : "")
thisCapab = "powerMeter"
break
case "Carbon monoxide detector":
thisName = "CO detector" + (multi ? "s" : "")
thisCapab = "carbonMonoxideDetector"
break
case "Smoke detector":
thisName = "Smoke detector" + (multi ? "s" : "")
thisCapab = "smokeDetector"
break
case "Water sensor":
thisName = "Water sensors"
thisCapab = "waterSensor"
break
case "Music player":
thisName = "Music player"
thisCapab = "musicPlayer"
break
case "Rule truth":
def theseRules = parent.ruleList(app.label)
def result = input dev, "enum", title: "Rules", required: true, multiple: multi, submitOnChange: true, options: theseRules.sort()
return result
case "Battery":
thisName = multi ? "Batteries" : "Battery"
thisCapab = "battery"
}
def result = input dev, "capability.$thisCapab", title: thisName, required: true, multiple: multi, submitOnChange: true
}
def getAnyAll(myDev) {
def result = input "All$myDev", "bool", title: "All of these?", required: false
}
def getRelational(myDev) {
def result = input "Rel$myDev", "enum", title: "Choose comparison", required: true, options: ["=", "!=", "<", ">", "<=", ">="]
}
def getButton(dev) {
def numNames = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty"]
def result = input "$dev", "capability.button", title: "Button Device", required: true, multiple: false, submitOnChange: true
def thisDev = settings.find{it.key == "$dev"}
if(thisDev) {
input "numButtons$dev", "number", title: "Number of buttons? (Default 4)", range: "1..20", required: false, submitOnChange: true, description: "4"
def numButtons = settings.find{it.key == "numButtons$dev"}
numButtons = numButtons ? numButtons.value : 4
def butOpts = ["one"]
if(numButtons > 1) {
for (int i = 1; i < numButtons; i++) butOpts[i] = numNames[i]
input "Button$dev", "enum", title: "Button number", required: true, multiple: false, submitOnChange: true, options: butOpts
}
}
}
def getState(myCapab, n, isTrig) {
def result = null
def param = [n: n]
def myState = isTrig ? "tstate$n" : "state$n"
def myIsDev = isTrig ? "istDev$n" : "isDev$n"
def myRelDev = isTrig ? "reltDevice$n" : "relDevice$n"
def isRule = state.isRule || (state.howMany > 1 && !isTrig)
def phrase = isRule ? "state" : "becomes"
def presPhrase = isRule ? "state" : " ..."
def swphrase = isRule ? "state" : "turns"
def presoptions = isRule ? ["present", "not present"] : ["arrives", "leaves"]
def presdefault = isRule ? "present" : "arrives"
def lockphrase = isRule ? "state" : "is"
def days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
if (myCapab == "Switch") result = input myState, "enum", title: "Switch $swphrase", options: ["on", "off"], defaultValue: "on"
else if(myCapab == "Physical Switch") result = input myState, "enum", title: "Switch turns ", options: ["on", "off"], defaultValue: "on"
else if(myCapab == "Motion") result = input myState, "enum", title: "Motion $phrase", options: ["active", "inactive"], defaultValue: "active"
else if(myCapab == "Acceleration") result = input myState, "enum", title: "Acceleration $phrase", options: ["active", "inactive"], defaultValue: "active"
else if(myCapab == "Contact") result = input myState, "enum", title: "Contact $phrase", options: ["open", "closed"], defaultValue: "open"
else if(myCapab == "Presence") result = input myState, "enum", title: "Presence $presPhrase", options: presoptions, defaultValue: presdefault
else if(myCapab == "Garage door") result = input myState, "enum", title: "Garage door $phrase", options: ["closed", "open", "opening", "closing", "unknown"], defaultValue: "open"
else if(myCapab == "Door") result = input myState, "enum", title: "Door $phrase", options: ["closed", "open", "opening", "closing", "unknown"], defaultValue: "open"
else if(myCapab == "Lock") result = input myState, "enum", title: "Lock $lockphrase", options: ["locked", "unlocked"], defaultValue: "unlocked"
else if(myCapab == "Thermostat Mode") result = input myState, "enum", title: "Thermostat mode ", options: ["heat", "cool", "auto", "off", "emergency heat"], defaultValue: "heat"
else if(myCapab == "Thermostat State") result = input myState, "enum", title: "Thermostat state ", options: ["heating", "idle", "pending cool", "vent economizer", "cooling", "pending heat", "fan only"], defaultValue: "heating"
else if(myCapab == "Carbon monoxide detector") result = input myState, "enum", title: "CO $phrase ", options: ["clear", ,"detected", "tested"], defaultValue: "detected"
else if(myCapab == "Smoke detector") result = input myState, "enum", title: "Smoke $phrase ", options: ["clear", ,"detected", "tested"], defaultValue: "detected"
else if(myCapab == "Water sensor") result = input myState, "enum", title: "Water $phrase", options: ["dry", "wet"], defaultValue: "wet"
else if(myCapab == "Button") result = input myState, "enum", title: "Button pushed or held ", options: ["pushed", "held"], defaultValue: "pushed"
else if(myCapab == "Rule truth") result = input myState, "enum", title: "Rule truth $phrase ", options: ["true", "false"], defaultValue: "true"
else if(myCapab == "Music player") result = input myState, "enum", title: "Playing state", options: ["playing", "paused","stopped"], defaultValue: "playing"
else if(myCapab == "Private Boolean") result = input myState, "enum", title: "Private Boolean $phrase ", options: ["true", "false"], defaultValue: "true"
else if(myCapab == "Smart Home Monitor") result = input myState, "enum", title: "SHM $phrase", options: ["away" : "Arm (away)", "stay" : "Arm (stay)", "off" : "Disarm"]
else if(myCapab in ["Temperature", "Humidity", "Illuminance", "Energy meter", "Power meter", "Battery", "Dimmer level"]) {
input myIsDev, "bool", title: "Relative to another device?", multiple: false, required: false, submitOnChange: true, defaultValue: false
def myDev = settings.find {it.key == myIsDev}
if(myDev && myDev.value) {
getDevs(myCapab, myRelDev, false)
if (myCapab == "Temperature") result = input myState, "decimal", title: "Temperature offset ", range: "*..*", defaultValue: 0
else if(myCapab == "Humidity") result = input myState, "number", title: "Humidity offset", range: "-100..100", defaultValue: 0
else if(myCapab == "Illuminance") result = input myState, "number", title: "Illuminance offset", range: "*..*", defaultValue: 0
else if(myCapab == "Dimmer level") result = input myState, "number", title: "Dimmer offset", range: "-100..100", defaultValue: 0
else if(myCapab == "Energy meter") result = input myState, "decimal", title: "Energy level offset", range: "*..*", defaultValue: 0
else if(myCapab == "Power meter") result = input myState, "decimal", title: "Power level offset", range: "*..*", defaultValue: 0
else if(myCapab == "Battery") result = input myState, "number", title: "Battery level offset", range: "-100..100", defaultValue: 0
}
else if(myCapab == "Temperature") result = input myState, "decimal", title: "Temperature becomes ", range: "*..*"
else if(myCapab == "Humidity") result = input myState, "number", title: "Humidity becomes", range: "0..100"
else if(myCapab == "Illuminance") result = input myState, "number", title: "Illuminance becomes", range: "0..*"
else if(myCapab == "Dimmer level") result = input myState, "number", title: "Dimmer level", range: "0..100"
else if(myCapab == "Energy meter") result = input myState, "decimal", title: "Energy level becomes", range: "0..*"
else if(myCapab == "Power meter") result = input myState, "decimal", title: "Power level becomes", range: "*..*"
else if(myCapab == "Battery") result = input myState, "number", title: "Battery level becomes", range: "0..100"
} else if(myCapab == "Days of week") result = input "days", "enum", title: "On certain days of the week", multiple: true, required: false, options: days
else if(myCapab == "Mode") {
def myModes = []
location.modes.each {myModes << "$it"}
def modeVar = (state.isRule || state.howMany > 1) ? "modes" : "modesX"
result = input modeVar, "enum", title: "Select mode(s)", multiple: true, required: false, options: myModes.sort()
} else if(myCapab == "Time of day") {
def timeLabel = timeIntervalLabelX()
href "certainTimeX", title: "Between two times", description: timeLabel ?: "Tap to set", state: timeLabel ? "complete" : null
} else if(myCapab == "Certain Time") {
def atTimeLabel = atTimeLabel()
href "atCertainTime", title: "At a certain time", description: atTimeLabel ?: "Tap to set", state: atTimeLabel ? "complete" : null
} else if(myCapab == "Periodic") {
state.thisN = n
def periodLabel = periodicLabel(n)
href "periodic", title: "Periodic schedule", description: periodLabel ?: "Tap to set", state: periodLabel ? "complete" : null, params: param
} else if(myCapab == "Routine") {
def phrases = location.helloHome?.getPhrases()*.label
result = input myState, "enum", title: "When this routine runs", multiple: false, required: false, options: phrases
}
def whatState = settings.find {it.key == myState}
}
def certainTimeX() {
dynamicPage(name: "certainTimeX", title: "Between two times", uninstall: false) {
section() {
input "startingXX", "enum", title: "Starting at", options: ["A specific time", "Sunrise", "Sunset"], defaultValue: "A specific time", submitOnChange: true
if(startingXX in [null, "A specific time"]) input "startingA", "time", title: "Start time", required: false
else {
if(startingXX == "Sunrise") input "startSunriseOffsetX", "number", range: "*..*", title: "Offset in minutes (+/-)", required: false
else if(startingXX == "Sunset") input "startSunsetOffsetX", "number", range: "*..*", title: "Offset in minutes (+/-)", required: false
}
}
section() {
input "endingXX", "enum", title: "Ending at", options: ["A specific time", "Sunrise", "Sunset"], defaultValue: "A specific time", submitOnChange: true
if(endingXX in [null, "A specific time"]) input "endingA", "time", title: "End time", required: false
else {
if(endingXX == "Sunrise") input "endSunriseOffsetX", "number", range: "*..*", title: "Offset in minutes (+/-)", required: false
else if(endingXX == "Sunset") input "endSunsetOffsetX", "number", range: "*..*", title: "Offset in minutes (+/-)", required: false
}
}
}
}
def atCertainTime() {
dynamicPage(name: "atCertainTime", title: "At a certain time", uninstall: false) {
section() {
input "timeX", "enum", title: "At time or sunrise/sunset?", options: ["A specific time", "Sunrise", "Sunset"], defaultValue: "A specific time", submitOnChange: true
if(timeX in [null, "A specific time"]) input "atTime", "time", title: "At this time", required: false
else {
if(timeX == "Sunrise") input "atSunriseOffset", "number", range: "*..*", title: "Offset in minutes (+/-)", required: false
else if(timeX == "Sunset") input "atSunsetOffset", "number", range: "*..*", title: "Offset in minutes (+/-)", required: false
}
}
}
}
def periodic(param) {
dynamicPage(name: "periodic", title: "Periodic schedule", uninstall: false) {
// def n = param.n
if(param.n != null) state.thisN = param.n
def n = state.thisN
section() {
input "whichPeriod$n", "enum", title: "Select periodic frequency", submitOnChange: true, required: true, options: ["Minutes", "Hourly", "Daily", "Weekly", "Monthly", "Yearly"]
// log.debug "periodic: ${settings["whichPeriod$n"]}"
switch(settings["whichPeriod$n"]) {
case "Minutes":
if(!settings["selectMinutesC$n"]) input "everyNMinutesC$n", "bool", title: " > Every n minutes?", submitOnChange: true, required: false
if(settings["everyNMinutesC$n"]) input "everyNC$n", "number", title: " > number of minutes", range: "1..59", submitOnChange: true, required: false, defaultValue: 1
if(!settings["everyNMinutesC$n"]) input "selectMinutesC$n", "enum", title: " > Each selected minute", submitOnChange: true, required: false, multiple: true,
options: [ "0", "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"]
break
case "Hourly":
if(!settings["selectHoursC$n"]) input "everyNHoursC$n", "bool", title: " > Every n hours?", submitOnChange: true, required: false
if(settings["everyNHoursC$n"]) {
input "everyNHC$n", "number", title: " > number of hours", range: "1..23", submitOnChange: true, required: false, defaultValue: 1
input "startingHC$n", "time", title: " > Starting at", submitOnChange: true, required: false, defaultValue: "2016-03-23T12:00:00.000" + gmtOffset()
}
if(!settings["everyNHoursC$n"]) {
input "selectHoursC$n", "enum", title: " > Each selected hour", submitOnChange: true, required: false, multiple: true,
options: [ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "11", "12", "13", "14", "15", "16", "17", "18", "19",
"20", "21", "22", "23"]
input "startingHCX$n", "number", title: " > Starting minutes after the hour", submitOnChange: true, required: false, range: "0..59", defaultValue: 0
}
break
case "Daily":
if(!settings["selectDoMC$n"] && !settings["everyWeekDay$n"]) input "everyNDoMC$n", "bool", title: " > Every n days?", submitOnChange: true, required: false
if(settings["everyNDoMC$n"]) input "everyNDC$n", "number", title: " > number of days", range: "1..31", submitOnChange: true, required: false, defaultValue: 1
if(!settings["selectDoMC$n"] && !settings["everyNDoMC$n"]) input "everyWeekDay$n", "bool", title: " > Every weekday?", submitOnChange: true, required: false
if(!settings["everyNDoMC$n"] && !settings["everyWeekDay$n"]) input "selectDoMC$n", "enum", title: " > Each selected day of the month", submitOnChange: true, required: false, multiple: true,
options: [ "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"]
input "startingDC$n", "time", title: " > At time", submitOnChange: true, required: false, defaultValue: "2016-03-23T12:00:00.000" + gmtOffset()
break
case "Weekly":
if(!settings["everyDoWC$n"]) input "selectDoWC$n", "enum", title: " > Each selected day of the week", submitOnChange: true, required: false, multiple: true,
options: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
input "startingWC$n", "time", title: " > Starting at", submitOnChange: true, required: false, defaultValue: "2016-03-23T12:00:00.000" + gmtOffset()
break
case "Monthly":
if(!settings["weeklyMC$n"]) input "dayMC$n", "number", title: " > On day number", range: "1..31", submitOnChange: true, required: false
if(!settings["selectMonthC$n"] && !settings["weeklyMC$n"]) input "everyNMC$n", "number", title: " > Of every n months", range: "1..12", submitOnChange: true, required: false
if(!settings["everyNMC$n"] && !settings["weeklyMC$n"]) input "selectMonthC$n", "enum", title: " > Of each selected month", submitOnChange: true, required: false, multiple: true,
options: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
if(!settings["selectMonthC$n"] && !settings["everyNMC$n"]) {
input "weeklyMC$n", "enum", title: " > In the week of month ...", submitOnChange: true, required: false, options: ["First", "Second", "Third", "Fourth"]
if(settings["weeklyMC$n"]) {
input "dailyMC$n", "enum", title: " > On day of week ...", submitOnChange: true, required: false, options: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
input "everyNMCX$n", "number", title: " > Of every n months", range: "1..12", submitOnChange: true, required: false, defaultValue: 1
}
}
input "startingMC$n", "time", title: " > Starting at", submitOnChange: true, required: false, defaultValue: "2016-03-23T12:00:00.000" + gmtOffset()
break
case "Yearly":
if(!settings["weeklyYC$n"]) input "yearlyMonthC$n", "enum", title: " > In the month of ...", submitOnChange: true, required: false, multiple: false,
options: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
if(settings["yearlyMonthC$n"]) input "yearlyDayC$n", "number", title: " > On this day of month ...", description: "1..31", range: "1..31", submitOnChange: true, required: false, multiple: false
if(!settings["yearlyMonthC$n"]) input "weeklyYC$n", "enum", title: " > In the week of month ...", submitOnChange: true, required: false, options: ["First", "Second", "Third", "Fourth"]
if(settings["weeklyYC$n"]) {
input "dailyYC$n", "enum", title: " > On day of week ...", submitOnChange: true, required: false, options: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
input "yearlyMonthCX$n", "enum", title: " > In the month of ...", submitOnChange: true, required: false, multiple: false,
options: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
}
input "startingYC$n", "time", title: " > Starting at", submitOnChange: true, required: false, defaultValue: "2016-03-23T12:00:00.000" + gmtOffset()
break
}
}
}
}
def periodicLabel(n) {
def result
switch(settings["whichPeriod$n"]) {
case "Minutes":
if(settings["everyNMinutesC$n"]) result = "Every ${settings["everyNC$n"]} minute" + (settings["everyNC$n"] > 1 ? "s" : "")
if(settings["selectMinutesC$n"]) {
def str = settings["selectMinutesC$n"][1..-2]
result = "Each of these minutes\n $str"
}
break
case "Hourly":
if(settings["everyNHoursC$n"]) result = "Every ${settings["everyNHC$n"]} hour" + (settings["everyNHC$n"] > 1 ? "s" : "") + " starting at ${hhmm(settings["startingHC$n"])}"
if(settings["selectHoursC$n"]) {
def str = settings["selectHoursC$n"][1..-2]
def str2 = ""
for(int i = 0; i < str.size(); i++) {
if(str[i] == ",") str2 = str2 + ":00"
str2 = str2 + str[i]
}
result = "Each of these hours\n $str2:00\n At ${settings["startingHCX$n"]} minutes after the hour"
}
break
case "Daily":
if(settings["everyNDoMC$n"]) result = "Every " + (settings["everyNDC$n"] == 1 ? "" : "${settings["everyNDC$n"]} ") + "day" + (settings["everyNDC$n"] > 1 ? "s" : "") + " at ${hhmm(settings["startingDC$n"])}"
if(settings["everyWeekDay$n"]) result = "Every weekday at ${hhmm(settings["startingDC$n"])}"
if(settings["selectDoMC$n"]) {
def str = settings["selectDoMC$n"][1..-2]
result = "Each of these days of the month\n $str: At ${hhmm(settings["startingDC$n"])}"
}
break
case "Weekly":
if(settings["selectDoWC$n"]) {
def str = settings["selectDoWC$n"][1..-2]
result = "Each of these days of the week\n $str: At ${hhmm(settings["startingWC$n"])}"
}
break
case "Monthly":
if(settings["everyNMC$n"]) result = "On day ${settings["dayMC$n"]} of every ${settings["everyNMC$n"]} month" + (settings["everyNMC$n"] > 1 ? "s" : "") + " At ${hhmm(settings["startingMC$n"])}"
if(settings["selectMonthC$n"]) {
def str = settings["selectMonthC$n"][1..-2]
result = "On day ${settings["dayMC$n"]} of each of these months\n $str: At ${hhmm(settings["startingMC$n"])}"
}
if(settings["weeklyMC$n"]) result = "On the ${settings["weeklyMC$n"]} ${settings["dailyMC$n"]} of every " + (settings["everyNMCX$n"] > 1 ? "${settings["everyNMCX$n"]} month" : "month") +
(settings["everyNMCX$n"] > 1 ? "s" : "") + " at ${hhmm(settings["startingMC$n"])}"
break
case "Yearly":
if(settings["yearlyMonthC$n"]) result = "Every year on ${settings["yearlyDayC$n"]} of ${settings["yearlyMonthC$n"]}" + (settings["everyNMC$n"] > 1 ? "s" : "") + "\n At ${hhmm(settings["startingYC$n"])}"
if(settings["weeklyYC$n"]) result = "On the ${settings["weeklyYC$n"]} ${settings["dailyYC$n"]} of ${settings["yearlyMonthCX$n"]} at ${hhmm(settings["startingYC$n"])}"
break
}
return result
}
def cronString(n) {
def dayOrd = ["Sunday" : "SUN", "Monday" : "MON", "Tuesday" : "TUE", "Wednesday" : "WED", "Thursday" : "THU", "Friday" : "FRI", "Saturday" : "SAT"]
def monthOrd = ["January" : 1, "February" : 2, "March" : 3, "April" : 4, "May" : 5, "June" : 6, "July" : 7, "August" : 8, "September" : 9, "October" : 10, "November" : 11, "December" : 12]
def weekOrd = ["First" : 1, "Second" : 2, "Third" : 3, "Fourth" : 4]
def result
switch(settings["whichPeriod$n"]) {
case "Minutes":
if(settings["everyNMinutesC$n"]) result = "11 */${settings["everyNC$n"]} * * * ?"
if(settings["selectMinutesC$n"]) {
def str = stripBrackSpace("${settings["selectMinutesC$n"]}")
result = "11 $str * * * ?"
}
break
case "Hourly":
if(settings["everyNHoursC$n"]) result = "11 0 */${settings["everyNHC$n"]} * * ?"
if(settings["selectHoursC$n"]) {
def str = stripBrackSpace("${settings["selectHoursC$n"]}") as String
result = "11 ${settings["startingHCX$n"]} $str 1/1 * ?"
}
break
case "Daily":
def hrmn = hhmm(settings["startingDC$n"], "HH:mm")
def hr = hrmn[0..1]
def mn = hrmn[3..4]
if(settings["everyNDoMC$n"]) result = "11 $mn $hr */${settings["everyNDC$n"]} * ?"
if(settings["everyWeekDay$n"]) result = "11 $mn $hr ? * 1,2,3,4,5"
if(settings["selectDoMC$n"]) {
def str = stripBrackSpace("${settings["selectDoMC$n"]}")
result = "11 $mn $hr $str * ?"
}
break
case "Weekly":
def hrmn = hhmm(settings["startingWC$n"], "HH:mm")
def hr = hrmn[0..1]
def mn = hrmn[3..4]
if(settings["selectDoWC$n"]) {
def str = ""
settings["selectDoWC$n"].each {str = str + (str ? "," : "") + "${dayOrd["$it"]}"}
result = "11 $mn $hr ? * $str"
}
break
case "Monthly":
def hrmn = hhmm(settings["startingMC$n"], "HH:mm")
def hr = hrmn[0..1]
def mn = hrmn[3..4]
if(settings["everyNMC$n"]) result = "11 $mn $hr ${settings["dayMC$n"]} */${settings["everyNMC$n"]} ?"
if(settings["selectMonthC$n"]) {
def str = ""
settings["selectMonthC$n"].each {str = str + (str ? "," : "") + "${monthOrd["$it"]}"}
result = "11 $mn $hr ${settings["dayMC$n"]} $str ?"
}
if(settings["weeklyMC$n"]) result = "11 $mn $hr ? */${settings["everyNMCX$n"]} ${dayOrd[settings["dailyMC$n"]]}#${weekOrd[settings["weeklyMC$n"]]}"
break
case "Yearly":
def hrmn = hhmm(settings["startingYC$n"], "HH:mm")
def hr = hrmn[0..1]
def mn = hrmn[3..4]
if(settings["yearlyMonthC$n"]) result = "11 $mn $hr ${settings["yearlyDayC$n"]} ${settings["yearlyMonthC$n"]} ?"
if(settings["weeklyYC$n"]) result = "11 $mn $hr ? ${monthOrd[settings["yearlyMonthCX$n"]]} ${dayOrd[settings["dailyYC$n"]]}#${weekOrd[settings["weeklyYC$n"]]}"
break
}
return result + " *"
}
def triggerLabel() {
def howMany = state.howManyT
def result = ""
if(howMany) {
for (int i = 1; i < howMany; i++) {
def thisCapab = settings.find {it.key == "tCapab$i"}
if(!thisCapab) return result
result = result + (i > 1 ? "OR\n" : "") + conditionLabelN(i, true)
if(i < howMany - 1) result = result + "\n"
}
}
return result
}
def conditionLabel() {
def howMany = state.howMany
def result = ""
if(howMany) {
for (int i = 1; i < howMany; i++) {
def thisCapab = settings.find {it.key == "rCapab$i"}
if(!thisCapab) return result
result = result + conditionLabelN(i, false) + ((state.isRule || state.isRule == null) ? (getOperand(i, true) ? " [TRUE]" : " [FALSE]") : "")
if(i < howMany - 1) result = result + "\n"
}
if((state.isRule || state.isRule == null) && howMany == 2 && state.eval in [null, [], [1]]) {
state.str = result[0..-8]
state.eval = [1]
}
}
return result
}
def conditionLabelN(i, isTrig) {
def result = ""
def SHMphrase = isTrig ? "becomes" : ((state.isRule || state.howMany > 1) ? "is" : "becomes")
def phrase = isTrig ? "becomes" : ((state.isRule || state.howMany > 1) ? "of" : "becomes")
def thisCapab = settings.find {it.key == (isTrig ? "tCapab$i" : "rCapab$i")}
if(thisCapab.value == "Time of day") result = "Time between " + timeIntervalLabelX()
else if(thisCapab.value == "Certain Time") result = "When time is " + atTimeLabel()
else if(thisCapab.value == "Periodic") result = periodicLabel(i)
else if(thisCapab.value == "Smart Home Monitor") {
def thisState = (settings.find {it.key == (isTrig ? "tstate$i" : "state$i")}).value
result = "SHM state $SHMphrase " + (thisState in ["away", "stay"] ? "Arm ($thisState)" : "Disarm")
} else if(thisCapab.value == "Days of week") result = "Day i" + (days.size() > 1 ? "n " + days : "s " + days[0])
else if(thisCapab.value == "Mode") {
if((state.isTrig || isTrig) && modesX) result = "Mode becomes " + (modesX.size() > 1 ? modesX : modesX[0])
else if((state.isRule || state.howMany > 1) && modes) result = "Mode i" + (modes.size() > 1 ? "n " + modes : "s " + modes[0])
} else if(thisCapab.value == "Routine") {
result = "Routine "
def thisState = settings.find {it.key == (isTrig ? "tstate$i" : "state$i")}
result = result + "'" + thisState.value + "' runs"
} else if(thisCapab.value == "Private Boolean") {
def thisState = settings.find {it.key == (isTrig ? "tstate$i" : "state$i")}
result = "Private Boolean $SHMphrase $thisState.value"
} else {
def thisDev = settings.find {it.key == (isTrig ? "tDev$i" : "rDev$i")}
if(!thisDev) return result
def thisAll = settings.find {it.key == (isTrig ? "AlltDev$i" : "AllrDev$i")}
// def myAny = thisAll && thisDev.value.size() > 1 ? "any " : ""
def myButton = settings.find {it.key == (isTrig ? "ButtontDev$i" : "ButtonrDev$i")}
def myAny = ""
// if((thisAll || !myButton) && thisDev.size() > 1) myAny = "any "
if((thisAll || !(thisCapab.value == "Button")) && thisDev.value.size() > 1) myAny = "any "
if (thisCapab.value == "Temperature") result = "Temperature $phrase "
else if(thisCapab.value == "Humidity") result = "Humidity $phrase "
else if(thisCapab.value == "Illuminance") result = "Illuminance $phrase "
else if(thisCapab.value == "Dimmer level") result = "Dimmer level $phrase "
else if(thisCapab.value == "Energy meter") result = "Energy level $phrase "
else if(thisCapab.value == "Power meter") result = "Power level $phrase "
else if(thisCapab.value == "Battery") result = "Battery level $phrase "
else if(thisCapab.value == "Rule truth") result = "Rule truth $phrase "
else if(thisCapab.value == "Button") {
result = "$thisDev.value " + (myButton ? "button $myButton.value " : "")
def thisState = settings.find {it.key == (isTrig ? "tstate$i" : "state$i")}
result = result + thisState.value
return result
}
result = result + (myAny ? thisDev.value : thisDev.value[0]) + " " + ((thisAll ? thisAll.value : false) ? "all " : myAny)
def thisRel = settings.find {it.key == (isTrig ? "ReltDev$i" : "RelrDev$i")}
if(thisCapab.value in ["Temperature", "Humidity", "Illuminance", "Dimmer level", "Energy meter", "Power meter", "Battery"]) result = result + " " + thisRel.value + " "
if(thisCapab.value == "Physical Switch") result = result + "physical "
def thisState = settings.find {it.key == (isTrig ? "tstate$i" : "state$i")}
def thisRelDev = settings.find {it.key == (isTrig ? "reltDevice$i" : "relDevice$i")}
if(thisRelDev) {
result = result + thisRelDev.value
if(thisState) result = result + (thisState.value > 0 ? " +" : " ") + (thisState.value != 0 ? thisState.value : "")
}
else result = result + thisState.value
if(thisCapab.value == "Presence" && thisDev.value.size() > 1 && isTrig) result = result[0..-2]
}
return result
}
// Rule definition code follows
def defineRule() {
dynamicPage(name: "defineRule", title: "Define the Rule", uninstall: false) {
section() {
paragraph "Turn on to enable parenthesized sub-rules"
input "advanced", "bool", title: "Complex Rule Input", required: false, submitOnChange: true
}
state.n = 0
state.str = ""
state.eval = []
section() {inputLeftAndRight(false)}
}
}
def rulLabl() {
def result = state.str
if(state.eval && state.str) {
state.token = 0
def truth = eval()
result = result + "\n[" + (truth ? "TRUE" : "FALSE") + "]"
}
}
def inputLeft(sub) {
def conds = []
for (int i = 1; i < state.howMany; i++) conds << conditionLabelN(i, false)
input "condNotL$state.n", "bool", title: "NOT ?", submitOnChange: true
if(settings["condNotL$state.n"]) {
state.str = state.str + "NOT "
state.eval << "NOT"
paragraph(state.str)
}
if(advanced) input "subCondL$state.n", "bool", title: "Enter subrule for left?", submitOnChange: true
if(settings["subCondL$state.n"]) {
state.str = state.str + "( "
state.eval << "("
paragraph(state.str)
inputLeftAndRight(true)
} else {
input "condL$state.n", "enum", title: "Which condition?", options: conds, submitOnChange: true
if(settings["condL$state.n"]) {
state.str = state.str + settings["condL$state.n"]
def myCond = 0
for (int i = 1; i < state.howMany; i++) if(conditionLabelN(i, false) == settings["condL$state.n"]) myCond = i
state.eval << myCond
paragraph(state.str)
}
}
}
def inputRight(sub) {
if(sub) {
input "endOfSubL$state.n", "bool", title: "End of sub-rule?", submitOnChange: true
if(settings["endOfSubL$state.n"]) {
state.str = state.str + " )"
state.eval << ")"
paragraph(state.str)
return
}
}
state.n = state.n + 1
input "operator$state.n", "enum", title: "AND or OR", options: ["AND", "OR"], submitOnChange: true, required: false
if(settings["operator$state.n"]) {
state.str = state.str + "\n" + settings["operator$state.n"] + "\n"
state.eval << settings["operator$state.n"]
paragraph(state.str)
def conds = []
for (int i = 1; i < state.howMany; i++) conds << conditionLabelN(i, false)
input "condNotR$state.n", "bool", title: "NOT ?", submitOnChange: true
if(settings["condNotR$state.n"]) {
state.str = state.str + "NOT "
state.eval << "NOT"
paragraph(state.str)
}
if(advanced) input "subCondR$state.n", "bool", title: "Enter subrule for right?", submitOnChange: true
if(settings["subCondR$state.n"]) {
state.str = state.str + "( "
state.eval << "("
paragraph(state.str)
inputLeftAndRight(true)
inputRight(sub)
// if(sub) {
// input "endOfSub$state.n", "bool", title: "End of sub-rule?", submitOnChange: true
// if(settings["endOfSub$state.n"]) {
// state.str = state.str + " )"
// state.eval << ")"
// paragraph(state.str)
// return
// }
// }
} else {
input "condR$state.n", "enum", title: "Which condition?", options: conds, submitOnChange: true
if(settings["condR$state.n"]) {
state.str = state.str + settings["condR$state.n"]
def myCond = 0
for (int i = 1; i < state.howMany; i++) if(conditionLabelN(i, false) == settings["condR$state.n"]) myCond = i
state.eval << myCond
paragraph(state.str)
}
if(sub) {
input "endOfSub$state.n", "bool", title: "End of sub-rule?", submitOnChange: true
if(settings["endOfSub$state.n"]) {
state.str = state.str + " )"
state.eval << ")"
paragraph(state.str)
return
}
}
inputRight(sub)
}
}
}
def inputLeftAndRight(sub) {
state.n = state.n + 1
inputLeft(sub)
inputRight(sub)
}
// Action selection code follows
def selectActionsTrue() {
def isRule = state.isRule || state.howMany > 1
dynamicPage(name: "selectActionsTrue", title: "Select Actions" + (isRule ? " for True" : ""), uninstall: false) {
state.actsTrue = ""
getActions(true)
if(state.actsTrue) state.actsTrue = state.actsTrue[0..-2]
}
}
def selectActionsFalse() {
dynamicPage(name: "selectActionsFalse", title: "Select Actions for False", uninstall: false) {
state.actsFalse = ""
getActions(false)
if(state.actsFalse) state.actsFalse = state.actsFalse[0..-2]
}
}
def getActions(trufal) {
def thisStr = trufal ? "True" : "False"
section("") {