forked from lgkahn/hubitat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsprucescheduler.groovy
5328 lines (4741 loc) · 225 KB
/
sprucescheduler.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
/**
* Spruce Scheduler Pre-release V2.55 - Updated 8/2019
* lgkahn [email protected] v2.56 add weather device option to get precip from
*
* Copyright 2015 Plaid Systems
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at:/**
* Spruce Scheduler Pre-release V2.55 - Updated 8/2019
* lgkahn [email protected] v2.56 add weather device option to get precip from
*
* Copyright 2015 Plaid Systems
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
* for the specific language governing permissions and limitations under the License.
*
-------v2.55-------------------
-update weather zipcode field to find lat&long if no zipcode found, works in Canada
-remove zipcode page and move entry to weatherpage
-correct behavior when no days selected
-add additional checks for missing location
-------v2.54-------------------
-update weather to use new weather api
-------v2.53.1-------------------
-ln 210: enableManual string modified
-ln 496: added code for old ST app zoneNumber number to convert to enum for app update compatibility
-ln 854: unschedule if NOT running to clear/correct manual subscription
-ln 863: weather scheduled if rain OR seasonal enabled, both off is no weather check scheduled
-ln 1083: added sync check to manual start
-ln 1538: corrected contact delay minimum fro 5s to 10s
-------v2.52---------------------
-Major revision by BAB
* ported to hubitat 9/4 lgk [email protected]
*/
definition(
name: "Spruce Scheduler",
namespace: "plaidsystems",
author: "Plaid Systems",
description: "Setup schedules for Spruce irrigation controller",
category: "Green Living",
iconUrl: "http://www.plaidsystems.com/smartthings/st_spruce_leaf_250f.png",
iconX2Url: "http://www.plaidsystems.com/smartthings/st_spruce_leaf_250f.png",
iconX3Url: "http://www.plaidsystems.com/smartthings/st_spruce_leaf_250f.png",
pausable: true
)
preferences {
page(name: 'startPage')
page(name: 'autoPage')
page(name: 'weatherPage')
page(name: 'globalPage')
page(name: 'contactPage')
page(name: 'delayPage')
page(name: 'zonePage')
page(name: 'zoneSettingsPage')
page(name: 'zoneSetPage')
page(name: 'plantSetPage')
page(name: 'sprinklerSetPage')
page(name: 'optionSetPage')
//found at bottom - transition pages
page(name: 'zoneSetPage1')
page(name: 'zoneSetPage2')
page(name: 'zoneSetPage3')
page(name: 'zoneSetPage4')
page(name: 'zoneSetPage5')
page(name: 'zoneSetPage6')
page(name: 'zoneSetPage7')
page(name: 'zoneSetPage8')
page(name: 'zoneSetPage9')
page(name: 'zoneSetPage10')
page(name: 'zoneSetPage11')
page(name: 'zoneSetPage12')
page(name: 'zoneSetPage13')
page(name: 'zoneSetPage14')
page(name: 'zoneSetPage15')
page(name: 'zoneSetPage16')
}
def startPage(){
dynamicPage(name: 'startPage', title: 'Spruce Smart Irrigation setup', install: true, uninstall: true)
{
section(''){
href(name: 'globalPage', title: 'Schedule settings', required: false, page: 'globalPage',
image: 'http://www.plaidsystems.com/smartthings/st_settings.png',
description: "Schedule: ${enableString()}\nWatering Time: ${startTimeString()}\nDays:${daysString()}\nNotifications:${notifyString()}"
)
}
section(''){
href(name: 'weatherPage', title: 'Weather Settings', required: false, page: 'weatherPage',
image: 'http://www.plaidsystems.com/smartthings/st_rain_225_r.png',
description: "Weather from: ${zipString()}\nRain Delay: ${isRainString()}\nSeasonal Adjust: ${seasonalAdjString()}"
)
}
section(''){
href(name: 'zonePage', title: 'Zone summary and setup', required: false, page: 'zonePage',
image: 'http://www.plaidsystems.com/smartthings/st_zone16_225.png',
description: "${getZoneSummary()}"
)
}
section(''){
href(name: 'delayPage', title: 'Valve delays & Pause controls', required: false, page: 'delayPage',
image: 'http://www.plaidsystems.com/smartthings/st_timer.png',
description: "Valve Delay: ${pumpDelayString()} s\n${waterStoppersString()}\nSchedule Sync: ${syncString()}"
)
}
section(''){
href(title: 'Spruce Irrigation Knowledge Base', //page: 'customPage',
description: 'Explore our knowledge base for more information on Spruce and Spruce sensors. Contact form is ' +
'also available here.',
required: false, style:'embedded',
image: 'http://www.plaidsystems.com/smartthings/st_spruce_leaf_250f.png',
url: 'http://support.spruceirrigation.com'
)
}
section(''){
href(title: 'Scheduler Version 2.56',
description: "Updated June 2019"
)
}
}
}
def globalPage() {
dynamicPage(name: 'globalPage', title: '') {
section('Spruce schedule Settings') {
label title: 'Schedule Name:', description: 'Name this schedule', required: false
input 'switches', 'capability.switch', title: 'Spruce Irrigation Controller:', description: 'Select a Spruce controller', required: true, multiple: false
}
section('Program Scheduling'){
input 'enable', 'bool', title: 'Enable watering:', defaultValue: 'true', metadata: [values: ['true', 'false']]
input 'enableManual', 'bool', title: 'Enable this schedule for manual start, only 1 schedule should be enabled for manual start at a time!', defaultValue: 'true', metadata: [values: ['true', 'false']]
input 'startTime', 'time', title: 'Watering start time', required: true
paragraph(image: 'http://www.plaidsystems.com/smartthings/st_calander.png',
title: 'Selecting watering days',
'Selecting watering days is optional. Spruce will optimize your watering schedule automatically. ' +
'If your area has water restrictions or you prefer set days, select the days to meet your requirements. ')
input (name: 'days', type: 'enum', title: 'Water only on these days...', required: false, multiple: true, options: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', 'Even', 'Odd'])
}
section('Push Notifications') {
input(name: 'notify', type: 'enum', title: 'Select what push notifications to receive.', required: false,
multiple: true, options: ['Daily', 'Delays', 'Warnings', 'Weather', 'Moisture', 'Events'])
input "sendPushMessage", "capability.notification", title: "Notification Devices: Hubitat PhoneApp or Pushover", multiple: true, required: false
input(name: 'logAll', type: 'bool', title: 'Log all notices to Hello Home?', defaultValue: 'false', options: ['true', 'false'])
}
}
}
def weatherPage() {
dynamicPage(name: 'weatherPage', title: 'Weather settings') {
section("Location to get weather forecast and conditions:") {
input(name: 'zipcode', type: 'text', title: "Zipcode default location: ${getDefaultLocation()}", required: false, submitOnChange: true)
input "weatherDevice", "capability.relativeHumidityMeasurement", title: "Weather Device (ie darksky or openweather) to get rain forecase from?", required: false
input 'isRain', 'bool', title: 'Enable Rain check:', metadata: [values: ['true', 'false']]
input 'rainDelay', 'decimal', title: 'inches of rain that will delay watering, default: 0.2', required: false
input 'isSeason', 'bool', title: 'Enable Seasonal Weather Adjustment:', metadata: [values: ['true', 'false']]
}
}
}
private String getDefaultLocation() {
String DefaultLocation = "Not set"
if(location?.zipCode) DefaultLocation = location.zipCode
if (!location?.zipCode?.isNumber() && location?.latitude && location?.longitude) DefaultLocation = "${location.latitude.floatValue()},${location.longitude.floatValue()}"
return DefaultLocation
}
private String startTimeString(){
if (!startTime) return 'Please set!' else return hhmm(startTime)
}
private String enableString(){
if(enable && enableManual) return 'On & Manual Set'
else if (enable) return 'On & Manual Off'
else if (enableManual) return 'Off & Manual Set'
else return 'Off'
}
private String waterStoppersString(){
String stoppers = 'Contact Sensor'
if (settings.contacts) {
if (settings.contacts.size() != 1) stoppers += 's'
stoppers += ': '
int i = 1
settings.contacts.each {
if ( i > 1) stoppers += ', '
stoppers += it.displayName
i++
}
stoppers = "${stoppers}\nPause: When ${settings.contactStop}\n"
}
else {
stoppers += ': None\n'
}
stoppers += "Switch"
if (settings.toggles) {
if (settings.toggles.size() != 1) stoppers += 'es'
stoppers += ': '
int i = 1
settings.toggles.each {
if ( i > 1) stoppers += ', '
stoppers += it.displayName
i++
}
stoppers = "${stoppers}\nPause: When switched ${settings.toggleStop}\n"
}
else {
stoppers += ': None\n'
}
int cd = 10
if (settings.contactDelay && settings.contactDelay.toInteger() > 10) cd = settings.contactDelay.toInteger()
stoppers += "Restart Delay: ${cd} secs"
return stoppers
}
private String isRainString(){
if (settings.isRain && !settings.rainDelay) return '0.2' as String
if (settings.isRain) return settings.rainDelay as String else return 'Off'
}
private String seasonalAdjString(){
if(settings.isSeason) return 'On' else return 'Off'
}
private String syncString(){
if (settings.sync) return "${settings.sync.displayName}" else return 'None'
}
private String notifyString(){
String notifyStr = ''
if(settings.notify) {
if (settings.notify.contains('Daily')) notifyStr += ' Daily'
//if (settings.notify.contains('Weekly')) notifyStr += ' Weekly'
if (settings.notify.contains('Delays')) notifyStr += ' Delays'
if (settings.notify.contains('Warnings')) notifyStr += ' Warnings'
if (settings.notify.contains('Weather')) notifyStr += ' Weather'
if (settings.notify.contains('Moisture')) notifyStr += ' Moisture'
if (settings.notify.contains('Events')) notifyStr += ' Events'
}
if (notifyStr == '') notifyStr = ' None'
if (settings.logAll) notifyStr += '\nSending all Notifications to Hello Home log'
return notifyStr
}
private String daysString(){
String daysString = ''
if (days){
if(days.contains('Even') || days.contains('Odd')) {
if (days.contains('Even')) daysString += ' Even'
if (days.contains('Odd')) daysString += ' Odd'
}
else {
if (days.contains('Monday')) daysString += ' M'
if (days.contains('Tuesday')) daysString += ' Tu'
if (days.contains('Wednesday')) daysString += ' W'
if (days.contains('Thursday')) daysString += ' Th'
if (days.contains('Friday')) daysString += ' F'
if (days.contains('Saturday')) daysString += ' Sa'
if (days.contains('Sunday')) daysString += ' Su'
}
}
if(daysString == '') return ' Any'
else return daysString
}
private String hhmm(time, fmt = 'h:mm a'){
def t = timeToday(time, location.timeZone)
def f = new java.text.SimpleDateFormat(fmt)
f.setTimeZone(location.timeZone ?: timeZone(time))
return f.format(t)
}
private String pumpDelayString(){
if (!pumpDelay) return '0' else return pumpDelay as String
}
def delayPage() {
dynamicPage(name: 'delayPage', title: 'Additional Options') {
section(''){
paragraph image: 'http://www.plaidsystems.com/smartthings/st_timer.png',
title: 'Pump and Master valve delay',
required: false,
'Setting a delay is optional, default is 0. If you have a pump that feeds water directly into your valves, ' +
'set this to 0. To fill a tank or build pressure, you may increase the delay.\n\nStart->Pump On->delay->Valve ' +
'On->Valve Off->delay->...'
input name: 'pumpDelay', type: 'number', title: 'Set a delay in seconds?', defaultValue: '0', required: false
}
section(''){
paragraph(image: 'http://www.plaidsystems.com/smartthings/st_pause.png',
title: 'Pause Control Contacts & Switches',
required: false,
'Selecting contacts or control switches is optional. When a selected contact sensor is opened or switch is ' +
'toggled, water immediately stops and will not resume until all of the contact sensors are closed and all of ' +
'the switches are reset.\n\nCaution: if all contacts or switches are left in the stop state, the dependent ' +
'schedule(s) will never run.')
input(name: 'contacts', title: 'Select water delay contact sensors', type: 'capability.contactSensor', multiple: true,
required: false, submitOnChange: true)
if (contacts)
input(name: 'contactStop', title: 'Stop watering when sensors are...', type: 'enum', required: (settings.contacts != null),
options: ['open', 'closed'], defaultValue: 'open')
input(name: 'toggles', title: 'Select water delay switches', type: 'capability.switch', multiple: true, required: false,
submitOnChange: true)
if (toggles)
input(name: 'toggleStop', title: 'Stop watering when switches are...', type: 'enum',
required: (settings.toggles != null), options: ['on', 'off'], defaultValue: 'off')
input(name: 'contactDelay', type: 'number', title: 'Restart watering how many seconds after all contacts and switches ' +
'are reset? (minimum 10s)', defaultValue: '10', required: false)
}
section(''){
paragraph image: 'http://www.plaidsystems.com/smartthings/st_spruce_controller_250.png',
title: 'Controller Sync',
required: false,
'For multiple controllers only. This schedule will wait for the selected controller to finish before ' +
'starting. Do not set with a single controller!'
input name: 'sync', type: 'capability.switch', title: 'Select Master Controller', description: 'Only use this setting with multiple controllers', required: false, multiple: false
}
}
}
def zonePage() {
dynamicPage(name: 'zonePage', title: 'Zone setup', install: false, uninstall: false) {
section('') {
href(name: 'hrefWithImage', title: 'Zone configuration', page: 'zoneSettingsPage',
description: "${zoneString()}",
required: false,
image: 'http://www.plaidsystems.com/smartthings/st_spruce_leaf_250f.png')
}
if (zoneActive('1')){
section(''){
href(name: 'z1Page', title: "1: ${getname("1")}", required: false, page: 'zoneSetPage1',
image: "${getimage("1")}",
description: "${display("1")}" )
}
}
if (zoneActive('2')){
section(''){
href(name: 'z2Page', title: "2: ${getname("2")}", required: false, page: 'zoneSetPage2',
image: "${getimage("2")}",
description: "${display("2")}" )
}
}
if (zoneActive('3')){
section(''){
href(name: 'z3Page', title: "3: ${getname("3")}", required: false, page: 'zoneSetPage3',
image: "${getimage("3")}",
description: "${display("3")}" )
}
}
if (zoneActive('4')){
section(''){
href(name: 'z4Page', title: "4: ${getname("4")}", required: false, page: 'zoneSetPage4',
image: "${getimage("4")}",
description: "${display("4")}" )
}
}
if (zoneActive('5')){
section(''){
href(name: 'z5Page', title: "5: ${getname("5")}", required: false, page: 'zoneSetPage5',
image: "${getimage("5")}",
description: "${display("5")}" )
}
}
if (zoneActive('6')){
section(''){
href(name: 'z6Page', title: "6: ${getname("6")}", required: false, page: 'zoneSetPage6',
image: "${getimage("6")}",
description: "${display("6")}" )
}
}
if (zoneActive('7')){
section(''){
href(name: 'z7Page', title: "7: ${getname("7")}", required: false, page: 'zoneSetPage7',
image: "${getimage("7")}",
description: "${display("7")}" )
}
}
if (zoneActive('8')){
section(''){
href(name: 'z8Page', title: "8: ${getname("8")}", required: false, page: 'zoneSetPage8',
image: "${getimage("8")}",
description: "${display("8")}" )
}
}
if (zoneActive('9')){
section(''){
href(name: 'z9Page', title: "9: ${getname("9")}", required: false, page: 'zoneSetPage9',
image: "${getimage("9")}",
description: "${display("9")}" )
}
}
if (zoneActive('10')){
section(''){
href(name: 'z10Page', title: "10: ${getname("10")}", required: false, page: 'zoneSetPage10',
image: "${getimage("10")}",
description: "${display("10")}" )
}
}
if (zoneActive('11')){
section(''){
href(name: 'z11Page', title: "11: ${getname("11")}", required: false, page: 'zoneSetPage11',
image: "${getimage("11")}",
description: "${display("11")}" )
}
}
if (zoneActive('12')){
section(''){
href(name: 'z12Page', title: "12: ${getname("12")}", required: false, page: 'zoneSetPage12',
image: "${getimage("12")}",
description: "${display("12")}" )
}
}
if (zoneActive('13')){
section(''){
href(name: 'z13Page', title: "13: ${getname("13")}", required: false, page: 'zoneSetPage13',
image: "${getimage("13")}",
description: "${display("13")}" )
}
}
if (zoneActive('14')){
section(''){
href(name: 'z14Page', title: "14: ${getname("14")}", required: false, page: 'zoneSetPage14',
image: "${getimage("14")}",
description: "${display("14")}" )
}
}
if (zoneActive('15')){
section(''){
href(name: 'z15Page', title: "15: ${getname("15")}", required: false, page: 'zoneSetPage15',
image: "${getimage("15")}",
description: "${display("15")}" )
}
}
if (zoneActive('16')){
section(''){
href(name: 'z16Page', title: "16: ${getname("16")}", required: false, page: 'zoneSetPage16',
image: "${getimage("16")}",
description: "${display("16")}" )
}
}
}
}
//code change for ST update file -> change input to zoneNumberEnum
private boolean zoneActive(z){
if (!zoneNumberEnum && zoneNumber && zoneNumber >= z.toInteger()) return true
else if (!zoneNumberEnum && zoneNumber && zoneNumber != z.toInteger()) return false
else if (zoneNumberEnum && zoneNumberEnum.contains(z)) return true
return false
}
private String zoneString() {
String numberString = 'Add zones to setup'
if (zoneNumber) numberString = "Zones enabled: ${zoneNumber}"
if (learn) numberString = "${numberString}\nSensor mode: Adaptive"
else numberString = "${numberString}\nSensor mode: Delay"
return numberString
}
def zoneSettingsPage() {
dynamicPage(name: 'zoneSettingsPage', title: 'Zone Configuration') {
section(''){
//input (name: "zoneNumber", type: "number", title: "Enter number of zones to configure?",description: "How many valves do you have? 1-16", required: true)//, defaultValue: 16)
// input 'zoneNumberEnum', 'enum', title: 'Select zones to configure', multiple: true, metadata: [values: ['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16']]
input 'zoneNumberEnum', 'enum', title: 'Select zones to configure', multiple: true, options: ['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16']
input 'gain', 'number', title: 'Increase or decrease all water times by this %, enter a negative or positive value, Default: 0', required: false, range: '-99..99'
paragraph image: 'http://www.plaidsystems.com/smartthings/st_sensor_200_r.png',
title: 'Moisture sensor adapt mode',
'Adaptive mode enabled: Watering times will be adjusted based on the assigned moisture sensor.\n\nAdaptive mode ' +
'disabled (Delay): Zones with moisture sensors will water on any available days when the low moisture setpoint has ' +
'been reached.'
input 'learn', 'bool', title: 'Enable Adaptive Moisture Control (with moisture sensors)', metadata: [values: ['true', 'false']]
}
}
}
def zoneSetPage() {
dynamicPage(name: 'zoneSetPage', title: "Zone ${state.app} Setup") {
section(''){
paragraph image: "http://www.plaidsystems.com/smartthings/st_${state.app}.png",
title: 'Current Settings',
"${display("${state.app}")}"
input "name${state.app}", 'text', title: 'Zone name?', required: false, defaultValue: "Zone ${state.app}"
}
section(''){
href(name: 'tosprinklerSetPage', title: "Sprinkler type: ${setString('zone')}", required: false, page: 'sprinklerSetPage',
image: "${getimage("${settings."zone${state.app}"}")}",
//description: "Set sprinkler nozzle type or turn zone off")
description: 'Sprinkler type descriptions')
input "zone${state.app}", 'enum', title: 'Sprinkler Type', multiple: false, required: false, defaultValue: 'Off', submitOnChange: true, options: ['Off', 'Spray', 'Rotor', 'Drip', 'Master Valve', 'Pump']
}
section(''){
href(name: 'toplantSetPage', title: "Landscape Select: ${setString('plant')}", required: false, page: 'plantSetPage',
image: "${getimage("${settings["plant${state.app}"]}")}",
//description: "Set landscape type")
description: 'Landscape type descriptions')
input "plant${state.app}", 'enum', title: 'Landscape', multiple: false, required: false, submitOnChange: true, options: ['Lawn', 'Garden', 'Flowers', 'Shrubs', 'Trees', 'Xeriscape', 'New Plants']
}
section(''){
href(name: 'tooptionSetPage', title: "Options: ${setString('option')}", required: false, page: 'optionSetPage',
image: "${getimage("${settings["option${state.app}"]}")}",
//description: "Set watering options")
description: 'Watering option descriptions')
input "option${state.app}", 'enum', title: 'Options', multiple: false, required: false, defaultValue: 'Cycle 2x', submitOnChange: true,options: ['Slope', 'Sand', 'Clay', 'No Cycle', 'Cycle 2x', 'Cycle 3x']
}
section(''){
paragraph image: 'http://www.plaidsystems.com/smartthings/st_sensor_200_r.png',
title: 'Moisture sensor settings',
'Select a soil moisture sensor to monitor and control watering. The soil moisture target value is set to a default value but can be adjusted to tune watering'
input "sensor${state.app}", 'capability.relativeHumidityMeasurement', title: 'Select moisture sensor?', required: false, multiple: false
input "sensorSp${state.app}", 'number', title: "Minimum moisture sensor target value, Setpoint: ${getDrySp(state.app)}", required: false
}
section(''){
paragraph image: 'http://www.plaidsystems.com/smartthings/st_timer.png',
title: 'Optional: Enter total watering time per week',
'This value will replace the calculated time from other settings'
input "minWeek${state.app}", 'number', title: 'Water time per week.\nDefault: 0 = autoadjust', description: 'minutes per week', required: false
input "perDay${state.app}", 'number', title: 'Guideline value for time per day, this divides minutes per week into watering days. Default: 20', defaultValue: '20', required: false
}
}
}
private String setString(String type) {
switch (type) {
case 'zone':
if (settings."zone${state.app}") return settings."zone${state.app}" else return 'Not Set'
break
case 'plant':
if (settings."plant${state.app}") return settings."plant${state.app}" else return 'Not Set'
break
case 'option':
if (settings."option${state.app}") return settings."option${state.app}" else return 'Not Set'
break
default:
return '????'
}
}
def plantSetPage() {
dynamicPage(name: 'plantSetPage', title: "${settings["name${state.app}"]} Landscape Select") {
section(''){
paragraph image: 'http://www.plaidsystems.com/img/st_${state.app}.png',
title: "${settings["name${state.app}"]}",
"Current settings ${display("${state.app}")}"
//input "plant${state.app}", "enum", title: "Landscape", multiple: false, required: false, submitOnChange: true,options: [values: ['Lawn', 'Garden', 'Flowers', 'Shrubs', 'Trees', 'Xeriscape', 'New Plants']
}
section(''){
paragraph image: 'http://www.plaidsystems.com/smartthings/st_lawn_200_r.png',
title: 'Lawn',
'Select Lawn for typical grass applications'
paragraph image: 'http://www.plaidsystems.com/smartthings/st_garden_225_r.png',
title: 'Garden',
'Select Garden for vegetable gardens'
paragraph image: 'http://www.plaidsystems.com/smartthings/st_flowers_225_r.png',
title: 'Flowers',
'Select Flowers for beds with smaller seasonal plants'
paragraph image: 'http://www.plaidsystems.com/smartthings/st_shrubs_225_r.png',
title: 'Shrubs',
'Select Shrubs for beds with larger established plants'
paragraph image: 'http://www.plaidsystems.com/smartthings/st_trees_225_r.png',
title: 'Trees',
'Select Trees for deep rooted areas without other plants'
paragraph image: 'http://www.plaidsystems.com/smartthings/st_xeriscape_225_r.png',
title: 'Xeriscape',
'Reduces water for native or drought tolorent plants'
paragraph image: 'http://www.plaidsystems.com/smartthings/st_newplants_225_r.png',
title: 'New Plants',
'Increases watering time per week and reduces automatic adjustments to help establish new plants. No weekly seasonal adjustment and moisture setpoint set to 40.'
}
}
}
def sprinklerSetPage(){
dynamicPage(name: 'sprinklerSetPage', title: "${settings["name${state.app}"]} Sprinkler Select") {
section(''){
paragraph image: "http://www.plaidsystems.com/img/st_${state.app}.png",
title: "${settings["name${state.app}"]}",
"Current settings ${display("${state.app}")}"
//input "zone${state.app}", "enum", title: "Sprinkler Type", multiple: false, required: false, defaultValue: 'Off', options: [values: ['Off', 'Spray', 'Rotor', 'Drip', 'Master Valve', 'Pump']
}
section(''){
paragraph image: 'http://www.plaidsystems.com/smartthings/st_spray_225_r.png',
title: 'Spray',
'Spray sprinkler heads spray a fan of water over the lawn. The water is applied evenly and can be turned on for a shorter duration of time.'
paragraph image: 'http://www.plaidsystems.com/smartthings/st_rotor_225_r.png',
title: 'Rotor',
'Rotor sprinkler heads rotate, spraying a stream over the lawn. Because they move back and forth across the lawn, they require a longer water period.'
paragraph image: 'http://www.plaidsystems.com/smartthings/st_drip_225_r.png',
title: 'Drip',
'Drip lines or low flow emitters water slowely to minimize evaporation, because they are low flow, they require longer watering periods.'
paragraph image: 'http://www.plaidsystems.com/smartthings/st_master_225_r.png',
title: 'Master',
'Master valves will open before watering begins. Set the delay between master opening and watering in delay settings.'
paragraph image: 'http://www.plaidsystems.com/smartthings/st_pump_225_r.png',
title: 'Pump',
'Attach a pump relay to this zone and the pump will turn on before watering begins. Set the delay between pump start and watering in delay settings.'
}
}
}
def optionSetPage(){
dynamicPage(name: 'optionSetPage', title: "${settings["name${state.app}"]} Options") {
section(''){
paragraph image: "http://www.plaidsystems.com/img/st_${state.app}.png",
title: "${settings["name${state.app}"]}",
"Current settings ${display("${state.app}")}"
//input "option${state.app}", "enum", title: "Options", multiple: false, required: false, defaultValue: 'Cycle 2x', options: ['Slope', 'Sand', 'Clay', 'No Cycle', 'Cycle 2x', 'Cycle 3x']
}
section(''){
paragraph image: 'http://www.plaidsystems.com/smartthings/st_slope_225_r.png',
title: 'Slope',
'Slope sets the sprinklers to cycle 3x, each with a short duration to minimize runoff'
paragraph image: 'http://www.plaidsystems.com/smartthings/st_sand_225_r.png',
title: 'Sand',
'Sandy soil drains quickly and requires more frequent but shorter intervals of water'
paragraph image: 'http://www.plaidsystems.com/smartthings/st_clay_225_r.png',
title: 'Clay',
'Clay sets the sprinklers to cycle 2x, each with a short duration to maximize absorption'
paragraph image: 'http://www.plaidsystems.com/smartthings/st_cycle1x_225_r.png',
title: 'No Cycle',
'The sprinklers will run for 1 long duration'
paragraph image: 'http://www.plaidsystems.com/smartthings/st_cycle2x_225_r.png',
title: 'Cycle 2x',
'Cycle 2x will break the water period up into 2 shorter cycles to help minimize runoff and maximize adsorption'
paragraph image: 'http://www.plaidsystems.com/smartthings/st_cycle3x_225_r.png',
title: 'Cycle 3x',
'Cycle 3x will break the water period up into 3 shorter cycles to help minimize runoff and maximize adsorption'
}
}
}
def setPage(i){
if (i) state.app = i
return state.app
}
private String getaZoneSummary(int zone){
// log.debug "in get a zone summary zone = $zone"
if (!settings."zone${zone}" || (settings."zone${zone}" == 'Off')) return "${zone}: Off"
String daysString = ''
int tpw = initTPW(zone)
int dpw = initDPW(zone)
int runTime = calcRunTime(tpw, dpw)
if ( !learn && (settings."sensor${zone}")) {
daysString = 'if Moisture is low on: '
dpw = daysAvailable()
}
if (days && (days.contains('Even') || days.contains('Odd'))) {
if (dpw == 1) daysString = 'Every 8 days'
if (dpw == 2) daysString = 'Every 4 days'
if (dpw == 4) daysString = 'Every 2 days'
if (days.contains('Even') && days.contains('Odd')) daysString = 'any day'
}
else {
def int[] dpwMap = [0,0,0,0,0,0,0]
dpwMap = getDPWDays(dpw)
// log.debug "passing to getrundays $dpwMap"
daysString += getRunDays(dpwMap)
}
return "${zone}: ${runTime} min, ${daysString}"
}
private String getZoneSummary(){
String summary = ''
if (learn) summary = 'Moisture Learning enabled' else summary = 'Moisture Learning disabled'
int zone = 1
createDPWMap()
while(zone <= 16) {
if (nozzle(zone) == 4) summary = "${summary}\n${zone}: ${settings."zone${zone}"}"
else if ( (initDPW(zone) != 0) && zoneActive(zone.toString())) summary = "${summary}\n${getaZoneSummary(zone)}"
zone++
}
if (summary) return summary else return zoneString() //"Setup all 16 zones"
}
private String display(String i){
//log.trace "display(${i})"
String displayString = ''
int tpw = initTPW(i.toInteger())
int dpw = initDPW(i.toInteger())
int runTime = calcRunTime(tpw, dpw)
if (settings."zone${i}") displayString += settings."zone${i}" + ' : '
if (settings."plant${i}") displayString += settings."plant${i}" + ' : '
if (settings."option${i}") displayString += settings."option${i}" + ' : '
int j = i.toInteger()
if (settings."sensor${i}") {
displayString += settings."sensor${i}"
displayString += "=${getDrySp(j)}% : "
}
if ((runTime != 0) && (dpw != 0)) displayString = "${displayString}${runTime} minutes, ${dpw} days per week"
return displayString
}
private String getimage(String image){
String imageStr = image
if (image.isNumber()) {
String zoneStr = settings."zone${image}"
if (zoneStr) {
if (zoneStr == 'Off') return 'http://www.plaidsystems.com/smartthings/off2.png'
if (zoneStr == 'Master Valve') return 'http://www.plaidsystems.com/smartthings/master.png'
if (zoneStr == 'Pump') return 'http://www.plaidsystems.com/smartthings/pump.png'
if (settings."plant${image}") imageStr = settings."plant${image}" // default assume asking for the plant image
}
}
// OK, lookup the requested image
switch (imageStr) {
case "null":
case null:
return 'http://www.plaidsystems.com/smartthings/off2.png'
case 'Off':
return 'http://www.plaidsystems.com/smartthings/off2.png'
case 'Lawn':
return 'http://www.plaidsystems.com/smartthings/st_lawn_200_r.png'
case 'Garden':
return 'http://www.plaidsystems.com/smartthings/st_garden_225_r.png'
case 'Flowers':
return 'http://www.plaidsystems.com/smartthings/st_flowers_225_r.png'
case 'Shrubs':
return 'http://www.plaidsystems.com/smartthings/st_shrubs_225_r.png'
case 'Trees':
return 'http://www.plaidsystems.com/smartthings/st_trees_225_r.png'
case 'Xeriscape':
return 'http://www.plaidsystems.com/smartthings/st_xeriscape_225_r.png'
case 'New Plants':
return 'http://www.plaidsystems.com/smartthings/st_newplants_225_r.png'
case 'Spray':
return 'http://www.plaidsystems.com/smartthings/st_spray_225_r.png'
case 'Rotor':
return 'http://www.plaidsystems.com/smartthings/st_rotor_225_r.png'
case 'Drip':
return 'http://www.plaidsystems.com/smartthings/st_drip_225_r.png'
case 'Master Valve':
return "http://www.plaidsystems.com/smartthings/st_master_225_r.png"
case 'Pump':
return 'http://www.plaidsystems.com/smartthings/st_pump_225_r.png'
case 'Slope':
return 'http://www.plaidsystems.com/smartthings/st_slope_225_r.png'
case 'Sand':
return 'http://www.plaidsystems.com/smartthings/st_sand_225_r.png'
case 'Clay':
return 'http://www.plaidsystems.com/smartthings/st_clay_225_r.png'
case 'No Cycle':
return 'http://www.plaidsystems.com/smartthings/st_cycle1x_225_r.png'
case 'Cycle 2x':
return 'http://www.plaidsystems.com/smartthings/st_cycle2x_225_r.png'
case "Cycle 3x":
return 'http://www.plaidsystems.com/smartthings/st_cycle3x_225_r.png'
default:
return 'http://www.plaidsystems.com/smartthings/off2.png'
}
}
private String getname(String i) {
if (settings."name${i}") return settings."name${i}" else return "Zone ${i}"
}
private String zipString() {
if (settings?.zipcode) return settings.zipcode
if (location?.zipCode?.isNumber()) return "${location.zipCode}"
if (location?.latitude && location?.longitude) return "${location.latitude.floatValue()},${location.longitude.floatValue()}"
return "not set"
}
//app install
def installed() {
state.dpwMap = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
state.tpwMap = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
state.Rain = [0,0,0,0,0,0,0]
state.daycount = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
atomicState.run = false // must be atomic - used to recover from crashes
state.pauseTime = null
atomicState.startTime = null
atomicState.finishTime = null // must be atomic - used to recover from crashes
log.debug "Installed with settings: ${settings}"
installSchedule()
}
def updated() {
log.debug "Updated with settings: ${settings}"
installSchedule()
}
def installSchedule(){
if (!state.seasonAdj) state.seasonAdj = 100.0
if (!state.weekseasonAdj) state.weekseasonAdj = 0
if (state.daysAvailable != 0) state.daysAvailable = 0 // force daysAvailable to be initialized by daysAvailable()
state.daysAvailable = daysAvailable() // every time we save the schedule
if (atomicState.run) {
attemptRecovery() // clean up if we crashed earlier
}
else {
unsubscribe() //added back in to reset manual subscription
resetEverything()
}
subscribe(app, appTouch) // enable the "play" button for this schedule
Random rand = new Random()
long randomOffset = 0
// always collect rainfall
int randomSeconds = rand.nextInt(59)
// if (settings.isRain || settings.isSeason) schedule("${randomSeconds} 57 23 1/1 * ? *", getRainToday) // capture today's rainfall just before midnight
if (settings.isRain || settings.isSeason)
{
log.debug "scheduling getraintoday"
runIn(10,getRainToday)
schedule("${randomSeconds} 57 23 1/1 * ? *", getRainToday ) // capture today's rainfall just before midnight
}
if (settings.switches && settings.startTime && settings.enable){
//randomOffset = rand.nextInt(60000) + 20000
// log.debug "starttime = $settings.startTime, tz = $location.timeZone time = $getTime() starttimestring = $startTimeString()"
// def checktime = timeToday(settings.startTime, location.timeZone).getTime() + randomOffset
//log.debug "randomOffset ${randomOffset} checktime ${checktime}"
// log.debug "calling schedule checktinme $checktime"
schedule(settings.startTime, preCheck)
// schedule(checktime, preCheck) //check weather & Days
writeSettings()
note('schedule', "${app.label}: Starts at ${startTimeString()}", 'i')
}
else {
unschedule( preCheck )
note('disable', "${app.label}: Automatic watering disabled or setup is incomplete", 'a')
}
}
// Called to find and repair after crashes - called by installSchedule() and busy()
private boolean attemptRecovery() {
if (!atomicState.run) {
return false // only clean up if we think we are still running
}
else { // Hmmm...seems we were running before...
def csw = settings.switches.currentSwitch
def cst = settings.switches.currentStatus
switch (csw) {
case 'on': // looks like this schedule is running the controller at the moment
if (!atomicState.startTime) { // cycleLoop cleared the startTime, but cycleOn() didn't set it
log.debug "${app.label}: crashed in cycleLoop(), cycleOn() never started, cst is ${cst} - resetting"
resetEverything() // reset and try again...it's probably not us running the controller, though
return false
}
// We have a startTime...
if (!atomicState.finishTime) { // started, but we don't think we're done yet..so it's probably us!
runIn(15, cycleOn) // goose the cycle, just in case
note('active', "${app.label}: schedule is apparently already running", 'i')
return true
}
// hmmm...switch is on and we think we're finished...probably somebody else is running...let busy figure it out
resetEverything()
return false
break
case 'off': // switch is off - did we finish?
if (atomicState.finishTime) { // off and finished, let's just reset things
resetEverything()
return false
}
if (switches.currentStatus != 'pause') { // off and not paused - probably another schedule, let's clean up
resetEverything()
return false
}
// off and not finished, and paused, we apparently crashed while paused
runIn(15, cycleOn)
return true
break
case 'programOn': // died while manual program running?
case 'programWait': // looks like died previously before we got started, let's try to clean things up
resetEverything()
if (atomicState.finishTime) atomicState.finishTime = null
if ((cst == 'active') || atomicState.startTime) { // if we announced we were in preCheck, or made it all the way to cycleOn before it crashed
settings.switches.programOff() // only if we think we actually started (cycleOn() started)
// probably kills manual cycles too, but we'll let that go for now
}
if (atomicState.startTime) atomicState.startTime = null
note ('schedule', "Looks like ${app.label} crashed recently...cleaning up", c)
return false
break
default:
log.debug "attemptRecovery(): atomicState.run == true, and I've nothing left to do"
return true
}
}
}
// reset everything to the initial (not running) state
private def resetEverything() {
if (atomicState.run) atomicState.run = false // we're not running the controller any more
unsubAllBut() // release manual, switches, sync, contacts & toggles
// take care not to unschedule preCheck() or getRainToday()
unschedule(cycleOn)
unschedule(checkRunMap)
unschedule(writeCycles)
unschedule(subOff)
if (settings.enableManual) subscribe(settings.switches, 'switch.programOn', manualStart)
}
// unsubscribe from ALL events EXCEPT app.touch
private def unsubAllBut() {
unsubscribe(settings.switches)
unsubWaterStoppers()
if (settings.sync) unsubscribe(settings.sync)
}
// enable the "Play" button in SmartApp list
def appTouch(evt) {
log.debug "appTouch(): atomicState.run = ${atomicState.run}"
runIn(2, preCheck) // run it off a schedule, so we can see how long it takes in the app.state
}
// true if one of the stoppers is in Stop state
private boolean isWaterStopped() {
if (settings.contacts && settings.contacts.currentContact.contains(settings.contactStop)) return true
if (settings.toggles && settings.toggles.currentSwitch.contains(settings.toggleStop)) return true
return false
}
// watch for water stoppers
private def subWaterStop() {
if (settings.contacts) {
unsubscribe(settings.contacts)
subscribe(settings.contacts, "contact.${settings.contactStop}", waterStop)
}
if (settings.toggles) {
unsubscribe(settings.toggles)
subscribe(settings.toggles, "switch.${settings.toggleStop}", waterStop)
}
}
// watch for water starters
private def subWaterStart() {