-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathRachio Zone
756 lines (660 loc) · 31.4 KB
/
Rachio Zone
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
/**
* Rachio Zone Device Handler
*
* Copyright\u00A9 2018 Anthony Santilli
* Copyright\u00A9 2024 JustinL
*
* V1.1.1 written by Anthony Santilli (@tonesto7)
* Ported to Hubitat and Enhanced to V2.0 by Justin Leonard (@JustinL)
*
* 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.
*
* Version info in Rachio Community App
*/
import java.text.SimpleDateFormat
import groovy.transform.Field
String devVer() { return "2.0.2" }
Integer statusRefresh() { return 60 }
metadata {
definition (name: "Rachio Zone", namespace: "lnjustin", author: "Anthony Santilli, Justin Leonard") {
capability "Refresh"
capability "Switch"
capability "Actuator"
capability "Valve"
capability "Sensor"
capability "Polling"
capability "Health Check"
attribute "zoneNumber", "number"
attribute "zoneName", "string"
attribute "watering", "string"
attribute "zoneSquareFeet", "number"
attribute "zoneWaterTime", "number"
attribute "zoneTotalDuration", "number"
attribute "rootZoneDepth", "number"
attribute "availableWater", "string"
attribute "efficiency", "string"
attribute "maxRuntime", "number"
attribute "saturatedDepthOfWater", "string"
attribute "depthOfWater", "string"
//current_schedule data
attribute "scheduleType", "string"
attribute "zoneDuration", "number"
attribute "zoneElapsed", "number"
attribute "zoneStartDate", "string"
attribute "zoneCycleCount", "number"
attribute "lastRunStr", "string"
attribute "lastRun", "string"
attribute "lastRunEndTime", "string"
attribute "nextRunStr", "string"
attribute "nextRun", "string"
//custom nozzle data
attribute "nozzleName", "string"
//custom soil data
attribute "soilName", "string"
//custom slope data
attribute "slopeName", "string"
//custom crop data
attribute "cropName", "string"
//custom shade data
attribute "shadeName", "string"
attribute "inStandby", "string"
attribute "lastUpdatedDt", "string"
attribute "notificationMessage", "string"
attribute "zoneRunStatus", "string"
attribute "isCycling", "string"
attribute "DeviceWatch-DeviceStatus", "string"
command "stopWatering"
command "decZoneWaterTime"
command "incZoneWaterTime"
command "setZoneWaterTime", ["number"]
command "startZone"
command "open"
command "close"
}
simulator {
// TODO: define status and reply messages here
}
tiles (scale: 2){
multiAttributeTile(name: "valveTile", type: "generic", width: 6, height: 4) {
tileAttribute("device.watering", key: "PRIMARY_CONTROL" ) {
attributeState "on", label: 'Watering', action: "close", icon: "st.valves.water.open", backgroundColor: "#00A7E1", nextState: "updating"
attributeState "off", label: 'Off', action: "open", icon: "st.valves.water.closed", backgroundColor: "#7e7d7d", nextState:"updating"
attributeState "offline", label: 'Offline', icon: "st.valves.water.closed", backgroundColor: "#FE2E2E"
attributeState "standby", label: 'Standby Mode', icon: "st.valves.water.closed", backgroundColor: "#FFAE42"
attributeState "updating", label:"Working"
}
tileAttribute("device.zoneRunStatus", key: "SECONDARY_CONTROL") {
attributeState("default", label:'${currentValue}')
}
}
valueTile("zoneName", "device.zoneName", inactiveLabel: true, width: 3, height: 1, decoration: "flat", wordWrap: true) {
state("default", label: 'Zone:\n${currentValue}')
}
valueTile("blank11", "device.blank", width: 1, height: 1, decoration: "flat") {
state("default", label: '')
}
//zone Water time control
valueTile("scheduleType", "device.scheduleType", width: 2, height: 1, decoration: "flat", wordWrap: true) {
state("default", label: 'Schedule Type:\n${currentValue}')
}
valueTile("efficiency", "device.efficiency", width: 2, height: 1, decoration: "flat", wordWrap: true) {
state("default", label: 'Efficiency:\n${currentValue}')
}
valueTile("zoneSquareFeet", "device.zoneSquareFeet", inactiveLabel: false, width: 2 , height: 1, decoration: "flat") {
state "default", label: 'Zone Area\n${currentValue} sq ft'
}
standardTile("leftZoneTimeButton", "device.zoneWaterTime", inactiveLabel: false, decoration: "flat") {
state "default", action:"decZoneWaterTime", icon:"st.thermostat.thermostat-left"
}
valueTile("zoneWaterTime", "device.zoneWaterTime", width: 2, height: 1, decoration: "flat") {
state "default", label:'Manual Zone Time:\n${currentValue} Minutes'
}
controlTile("zoneWaterTimeSliderTile", "device.zoneWaterTime", "slider", width: 4, height: 1, range:'(0..60)') {
state "default", label: 'Manual Zone Time', action:"setZoneWaterTime"
}
standardTile("rightZoneTimeButton", "device.zoneWaterTime", inactiveLabel: false, decoration: "flat") {
state "default", action:"incZoneWaterTime", icon:"st.thermostat.thermostat-right"
}
valueTile("zoneWaterTimeVal", "device.zoneWaterTime", inactiveLabel: false, width: 2 , height: 1, decoration: "flat") {
state "default", label: 'Water Time\n${currentValue} Minutes'
}
valueTile("startZoneTile", "device.zoneWaterTime", inactiveLabel: false, width: 2 , height: 1, decoration: "flat") {
state "default", label: 'Run This Zone\n${currentValue} Minutes', action:'startZone'
}
//nozzle Tiles
valueTile("nozzleName", "device.nozzleName", inactiveLabel: true, width: 2, height: 1, decoration: "flat") {
state "default", label: 'Nozzle:\n${currentValue}'
}
//Soil Tiles
valueTile("soilName", "device.soilName", inactiveLabel: true, width: 2, height: 1, decoration: "flat") {
state "default", label: 'Soil:\n${currentValue}'
}
//Slope Tiles
valueTile("slopeName", "device.slopeName", inactiveLabel: true, width: 2, height: 1, decoration: "flat") {
state "default", label: 'Slope:\n${currentValue}'
}
//Crop Tiles
valueTile("cropName", "device.cropName", inactiveLabel: true, width: 2, height: 1, decoration: "flat") {
state "default", label: 'Crop:\n${currentValue}'
}
//Shade Tiles
valueTile("shadeName", "device.shadeName", inactiveLabel: true, width: 2, height: 1, decoration: "flat") {
state "default", label: 'Shade:\n${currentValue}'
}
standardTile("refresh", "device.power", inactiveLabel: false, decoration: "flat", width: 2, height: 2) {
state "default", label:'', action:"refresh.refresh", icon:"st.secondary.refresh"
}
standardTile("zoneImage", "device.zoneImage", inactiveLabel: false, width: 1, height: 1, decoration: "flat") {
state "default", label: '', icon: "http://media.rach.io/images/zone/default/default_zone.jpg"
}
}
main "valveTile"
details(["valveTile", "zoneImage", "zoneName", "scheduleType", "nozzleName", "soilName", "slopeName", "cropName", "shadeName", "zoneSquareFeet", "leftZoneTimeButton", "zoneWaterTime", "rightZoneTimeButton", "startZoneTile", "lastUpdatedDt", "refresh"])
}
// parse events into attributes
def parse(String description) {
LOG("Parsing '${description}'", 5, "trace")
}
def initialize() {
sendEvent(name: "DeviceWatch-DeviceStatus", value: "online", displayed: false, isStateChange: true)
sendEvent(name: "DeviceWatch-Enroll", value: groovy.json.JsonOutput.toJson(["protocol":"cloud", "scheme":"untracked"]), displayed: false)
verifyDataAttr()
}
def verifyDataAttr() {
updateDataValue("HealthEnrolled", "true")
updateDataValue("manufacturer", "Rachio")
def gen = state?.deviceId ? parent?.getDevGeneration(state?.deviceId) : null
updateDataValue("model", "${device?.name}${gen ? " ($gen)" : ""}")
}
void installed() {
state.isInstalled = true
initialize()
}
void updated() {
initialize()
}
def generateEvent(Map results) {
if(!state?.swVersion || state?.swVersion != devVer()) {
initialize()
state.swVersion = devVer()
}
if(results) {
LOG("---------------START OF API RESULTS DATA FOR ${device?.currentValue('zoneName')}----------------", 3, "debug")
LOG("results:" + results, 3, "debug")
state?.pauseInStandby = (results?.pauseInStandby == true ? true : false)
state?.zoneId = results?.data?.id
state?.deviceId = results?.devId ? results?.devId.toString() : null
state?.zoneNum = results?.data?.zoneNumber
zoneNameEvent(results?.data?.name)
zoneNumEvent(results?.data?.zoneNumber)
stateDataEvent(results?.stateData)
state?.zoneImageUrl = results?.data?.imageUrl
def isOnline = results?.status == "ONLINE" ? true : false
state?.isOnline = isOnline
if(!isOnline) {
markOffLine()
} else {
state?.inStandby = results?.standby
if(isStateChange(device, "inStandby", results?.standby.toString())) {
sendEvent(name: 'inStandby', value: results?.standby, displayed: true, isStateChange: true)
}
def isOn = isWatering(results?.schedData)
if(results?.standby == true && results?.pauseInStandby == true) {
markStandby()
} else {
isWateringEvent(results?.schedData?.status, results?.schedData?.zoneId)
}
}
availableWaterEvent(results?.data?.availableWater)
rootZoneDepthEvent(results?.data?.rootZoneDepth)
zoneSquareFeetEvent(results?.data?.yardAreaSquareFeet)
zoneTotalDurationEvent(results?.data?.runtime)
saturatedDepthOfWaterEvent(results?.data?.saturatedDepthOfWater)
depthOfWaterEvent(results?.data?.depthOfWater)
maxRuntimeEvent(results?.data?.maxRuntime)
efficiencyEvent(results?.data?.efficiency)
if(!device?.currentState("zoneWaterTime")?.value) {
setZoneWaterTime(parent?.getZoneRunTimeDefaultSetting())
}
scheduleDataEvent(results?.schedData)
customNozzleDataEvent(results?.data?.customNozzle)
customSoilDataEvent(results?.data?.customSoil)
customSlopeDataEvent(results?.data?.customSlope)
customCropDataEvent(results?.data?.customCrop)
customShadeDataEvent(results?.data?.customShade)
if(isOnline) { lastUpdatedEvent() }
}
return "hello from zone"
}
def getDurationDesc(long secondsCnt) {
int seconds = secondsCnt %60
secondsCnt -= seconds
long minutesCnt = secondsCnt / 60
long minutes = minutesCnt % 60
minutesCnt -= minutes
long hoursCnt = minutesCnt / 60
return "${minutes} min ${(seconds >= 0 && seconds < 10) ? "0${seconds}" : "${seconds}"} sec"
}
def getDurationMinDesc(long secondsCnt) {
int seconds = secondsCnt %60
secondsCnt -= seconds
long minutesCnt = secondsCnt / 60
long minutes = minutesCnt % 60
minutesCnt -= minutes
long hoursCnt = minutesCnt / 60
return "${minutes}"
}
def lastUpdatedEvent() {
def lastDt = formatDt(new Date())
def lastUpd = device?.currentState("lastUpdatedDt")?.stringValue
state?.lastUpdatedDt = lastDt?.toString()
if(isStateChange(device, "lastUpdatedDt", lastDt.toString())) {
LOG("${device?.displayName} is (${state?.isOnline ? "Online and ${state?.inStandby ? "in Standby Mode" : "Active"}" : "OFFLINE"}) - Last Updated: (${lastDt})", 4, "info")
sendEvent(name: 'lastUpdatedDt', value: lastDt?.toString(), displayed: false)
}
}
def markOffLine() {
if(isStateChange(device, "watering", "offline")) {
LOG("UPDATED: Watering (Offline)", 5, "trace")
sendEvent(name: 'watering', value: "offline", displayed: true, isStateChange: true)
sendEvent(name: 'valve', value: "closed", displayed: false, isStateChange: true)
sendEvent(name: 'switch', value: "off", displayed: false, isStateChange: true)
}
if(isStateChange(device, "zoneRunStatus", "Device is Offline")) {
sendEvent(name: 'zoneRunStatus', value: "Device is Offline", displayed: false, isStateChange: true)
}
}
def markStandby() {
if(isStateChange(device, "watering", "standby")) {
LOG("UPDATED: Watering (Standby Mode)", 5, "trace")
sendEvent(name: 'watering', value: "standby", displayed: true, isStateChange: true)
sendEvent(name: 'valve', value: "closed", displayed: false, isStateChange: true)
sendEvent(name: 'switch', value: "off", displayed: false, isStateChange: true)
}
if(isStateChange(device, "zoneRunStatus", "Device in Standby Mode")) {
sendEvent(name: 'zoneRunStatus', value: "Device in Standby Mode", displayed: false, isStateChange: true)
}
}
def isWateringEvent(status, zoneId) {
LOG("isWateringEvent...", 5, "trace")
def curState = device?.currentState("watering")?.value.toString()
def isOn = (status == "PROCESSING" && device?.deviceNetworkId == zoneId) ? true : false
def newState = isOn ? "on" : "off"
if(isStateChange(device, "watering", newState.toString())) {
LOG("UPDATED: Watering (${newState}) | Previous: (${curState})", 3, "debug")
sendEvent(name: 'watering', value: newState, displayed: true, isStateChange: true)
sendEvent(name: 'switch', value: (isOn ? "on" : "off"), displayed: false, isStateChange: true)
sendEvent(name: 'valve', value: (isOn ? "open" : "closed"), displayed: false, isStateChange: true)
}
}
def zoneTotalDurationEvent(val) {
def curState = device?.currentState("zoneTotalDuration")?.value.toString()
def newState = val ? val.toInteger() : 0
if(isStateChange(device, "zoneTotalDuration", newState.toString())) {
LOG("UPDATED: Zone Total Duration Value (${newState}) | Previous: (${curState})", 3, "debug")
sendEvent(name:'zoneTotalDuration', value: newState, displayed: true)
}
}
def saturatedDepthOfWaterEvent(val) {
def curState = device?.currentState("saturatedDepthOfWater")?.value.toString()
def newState = val ? val.toDouble() : 0.0
if(isStateChange(device, "saturatedDepthOfWater", newState.toString())) {
LOG("UPDATED: Saturated Depth Of Water Value (${newState}) | Previous: (${curState})", 3, "debug")
sendEvent(name:'saturatedDepthOfWater', value: newState, displayed: true)
}
}
def depthOfWaterEvent(val) {
def curState = device?.currentState("depthOfWater")?.value.toString()
def newState = val ? val.toDouble() : 0.0
if(isStateChange(device, "depthOfWater", newState.toString())) {
LOG("UPDATED: Depth Of Water Value (${newState}) | Previous: (${curState})", 3, "debug")
sendEvent(name:'depthOfWater', value: newState, displayed: true)
}
}
def efficiencyEvent(val) {
def curState = device?.currentState("efficiency")?.value.toString()
def newState = val ? val.toDouble() : 0.0
if(isStateChange(device, "efficiency", newState.toString())) {
LOG("UPDATED: Efficiency Value (${newState}) | Previous: (${curState})", 3, "debug")
sendEvent(name:'efficiency', value: newState, displayed: false)
}
}
def maxRuntimeEvent(val) {
def curState = device?.currentState("maxRuntime")?.value.toString()
def newState = val ? val.toInteger() : 0
if(isStateChange(device, "maxRuntime", newState.toString())) {
LOG("UPDATED: Max Runtime Value (${newState}) | Previous: (${curState})", 3, "debug")
sendEvent(name:'maxRuntime', value: newState, displayed: false)
}
}
def availableWaterEvent(val) {
def curState = device?.currentState("availableWater")?.value.toString()
def newState = val ? val.toDouble() : 0.0
if(isStateChange(device, "availableWater", newState.toString())) {
LOG("UPDATED: Available Water Value (${newState}) | Previous: (${curState})", 3, "debug")
sendEvent(name:'availableWater', value: newState, displayed: true)
}
}
def zoneSquareFeetEvent(val) {
def curState = device?.currentState("zoneSquareFeet")?.value.toString()
def newState = val ? val.toInteger() : 0
if(isStateChange(device, "zoneSquareFeet", newState.toString())) {
LOG("UPDATED: Zone Area Square Feet (${newState}) | Previous: (${curState})", 3, "debug")
sendEvent(name:'zoneSquareFeet', value: newState, displayed: true)
}
}
def rootZoneDepthEvent(val) {
def curState = device?.currentState("rootZoneDepth")?.value.toString()
def newState = val ? val.toInteger() : 0
if(isStateChange(device, "rootZoneDepth", newState.toString())) {
LOG("UPDATED: Root Zone Depth Value (${newState}) | Previous: (${curState})", 3, "debug")
sendEvent(name:'rootZoneDepth', value: newState, displayed: false)
}
}
def zoneNumEvent(val) {
def curState = device?.currentState("zoneNumber")?.value.toString()
def newState = val ? val.toInteger() : 0
if(isStateChange(device, "zoneNumber", newState.toString())) {
LOG("UPDATED: Zone Number (${newState}) | Previous: (${curState})", 3, "debug")
sendEvent(name:'zoneNumber', value: newState, displayed: true)
}
}
def zoneNameEvent(val) {
def curState = device?.currentState("zoneName")?.value.toString()
def newState = val ? val.toString() : "unknown"
if(isStateChange(device, "zoneName", newState.toString())) {
LOG("UPDATED: Zone Name (${newState}) | Previous: (${curState})", 3, "debug")
sendEvent(name:'zoneName', value: newState, displayed: true)
}
}
def stateDataEvent(stateData) {
state?.stateData = stateData
if (stateData?.lastRun) sendEvent(name:'lastRunStr', value: formatUtcDt(stateData?.lastRun))
if (stateData?.lastRun) sendEvent(name:'lastRun', value: getUnixDtFromUTCDt(stateData?.lastRun))
if (stateData?.lastRunEndTime) sendEvent(name:'lastRunEndTime', value: getUnixDtFromUTCDt(stateData?.lastRunEndTime))
// def lastRunSecs = getTimeDiffSecondsUtc(stateData?.lastRun, stateData?.lastRunEndTime)
// sendEvent(name:'lastRunDurationStr', value: convertSecstoTimeStr(lastRunSecs))
if (stateData?.nextRun) sendEvent(name:'nextRunStr', value: formatUtcDt(stateData?.nextRun))
if (stateData?.nextRun) sendEvent(name:'nextRun', value: getUnixDtFromUTCDt(stateData?.nextRun))
}
def setZoneWaterTime(timeVal) {
def curState = device?.currentState("valveWaterTime")?.value.toString()
def newVal = timeVal ? timeVal.toInteger() : parent?.getValveRunTimeDefaultSetting()
if(isStateChange(device, "zoneWaterTime", newVal.toString())) {
LOG("UPDATED: Manual Zone Water Time (${newVal}) | Previous: (${curState})", 3, "debug")
sendEvent(name: 'zoneWaterTime', value: newVal, displayed: true)
}
}
def fmtString(str) {
if(!str) { return null }
def out = []
def tmp = str?.replaceAll("_", " ")?.toLowerCase()?.split(" ")
tmp?.each { out.push(it?.toString().capitalize()) }
return out.join(" ")
}
def scheduleDataEvent(data) {
LOG("scheduleDataEvent($data)...", 5, "trace")
state?.curSchedData = data
def curSchedType = !data?.type ? "off" : data?.type?.toString().toLowerCase()
state.curSchedType = curSchedType
state?.curScheduleId = !data?.scheduleId ? null : data?.scheduleId
state?.curScheduleRuleId = !data?.scheduleRuleId ? null : data?.scheduleRuleId
def zoneId = !data?.zoneId ? null : data?.zoneId
def zoneStartDate = (zoneId == device?.deviceNetworkId && data?.zoneStartDate) ? data?.zoneStartDate : null
def zoneDuration = (zoneId == device?.deviceNetworkId && data?.zoneDuration) ? data?.zoneDuration : null
def timeDiff = data?.zoneStartDate ? GetTimeValDiff(data?.zoneStartDate.toLong()) : 0
def elapsedDuration = data?.zoneStartDate ? getDurationMinDesc(Math.round(timeDiff)) : 0
def wateringDuration = zoneDuration ? getDurationMinDesc(zoneDuration) : 0
def zoneRunStatus = ((!zoneStartDate && !zoneDuration) || (zoneId != device?.deviceNetworkId)) ? "Status: Idle" : "${curSchedType == "automatic" ? "Scheduled " : "Manual "}Watering: ${elapsedDuration} of ${wateringDuration} Minutes"
def zoneCycleCount = (zoneId != device?.deviceNetworkId && !data?.totalCycleCount) ? 0 : data?.totalCycleCount
def isCycling = (zoneId == device?.deviceNetworkId && data?.cycling) ? true : false
def wateringVal = device?.currentState("watering")?.value
if(isStateChange(device, "scheduleType", curSchedType?.toString().toLowerCase()?.capitalize())) {
sendEvent(name: 'scheduleType', value: curSchedType?.toString().toLowerCase()?.capitalize(), displayed: true, isStateChange: true)
}
def zoneDurationValue = zoneDuration ?: 0
if(isStateChange(device, "zoneDuration", zoneDurationValue.toString())) {
sendEvent(name: 'zoneDuration', value: zoneDurationValue.toString(), displayed: true, isStateChange: true)
}
def zoneElapsed = zoneDuration && timeDiff && timeDiff > 0 ? timeDiff : 0
if(isStateChange(device, "zoneElapsed", zoneElapsed.toString())) {
sendEvent(name: 'zoneElapsed', value: zoneElapsed.toString(), displayed: false, isStateChange: true)
}
if(!state?.inStandby && wateringVal != "offline" && isStateChange(device, "zoneRunStatus", zoneRunStatus.toString())) {
log.info("UPDATED: ZoneRunStatus (${zoneRunStatus})")
sendEvent(name: 'zoneRunStatus', value: zoneRunStatus.toString(), displayed: true, isStateChange: true)
}
if(isStateChange(device, "zoneCycleCount", zoneCycleCount.toString())) {
sendEvent(name: 'zoneCycleCount', value: zoneCycleCount, displayed: true, isStateChange: true)
}
if(isStateChange(device, "isCycling", isCycling?.toString().capitalize())) {
sendEvent(name: 'isCycling', value: isCycling?.toString().capitalize(), displayed: true, isStateChange: true)
}
if(isStateChange(device, "zoneStartDate", (zoneStartDate ? epochToDt(zoneStartDate).toString() : "Not Active"))) {
sendEvent(name: 'zoneStartDate', value: (zoneStartDate ? epochToDt(zoneStartDate).toString() : "Not Active"), displayed: true, isStateChange: true)
}
}
def isWatering(data) {
def zoneId = !data?.zoneId ? null : data?.zoneId
def zoneStartDate = (zoneId == device?.deviceNetworkId && data?.zoneStartDate) ? data?.zoneStartDate : null
def zoneDuration = (zoneId == device?.deviceNetworkId && data?.zoneDuration) ? data?.zoneDuration : null
return zoneStartDate && zoneDuration;
}
def customNozzleDataEvent(data) {
LOG("customNozzleDataEvent($data)", 5, "trace")
if(data && data?.name && isStateChange(device, "nozzleName", fmtString(data?.name).toString())) {
sendEvent(name:'nozzleName', value: fmtString(data?.name).toString(), displayed: true, isStateChange: true)
}
}
def customSoilDataEvent(data) {
LOG("customSoilDataEvent($data)", 5, "trace")
if(data && data?.name && isStateChange(device, "soilName", fmtString(data?.name).toString())) {
sendEvent(name:'soilName', value: fmtString(data?.name).toString(), displayed: true, isStateChange: true)
}
}
def customSlopeDataEvent(data) {
LOG("customSlopeDataEvent($data)", 5, "trace")
def slope = data?.name ? fmtString(data?.name).toString() : "N/A"
if(slope && isStateChange(device, "slopeName", slope.toString())) {
sendEvent(name:'slopeName', value: slope.toString(), displayed: true, isStateChange: true)
}
}
def customCropDataEvent(data) {
LOG("customCropDataEvent($data)", 5, "trace")
if(data && data?.name && isStateChange(device, "cropName", fmtString(data?.name).toString())) {
sendEvent(name:'cropName', value: fmtString(data?.name).toString(), displayed: true, isStateChange: true)
}
}
def customShadeDataEvent(data) {
LOG("customShadeDataEvent($data)", 5, "trace")
if(data && data?.name && isStateChange(device, "shadeName", fmtString(data?.name).toString())) {
sendEvent(name:'shadeName', value: fmtString(data?.name).toString(), displayed: true, isStateChange: true)
}
}
def refresh() {
LOG("refresh...", 5, "trace")
poll()
}
void poll() {
LOG("Requested Parent Poll...", 5, "trace");
parent?.poll(this, "irrigationDevice")
}
def incZoneWaterTime() {
LOG("Decrease Zone Runtime", 5, "trace")
def value = device.latestValue('zoneWaterTime')
setZoneWaterTime(value + 1)
}
def decZoneWaterTime() {
LOG("Increase Zone Runtime", 5, "trace")
def value = device.latestValue('zoneWaterTime')
setZoneWaterTime(value - 1)
}
def isCmdOk2Run() {
LOG("isCmdOk2Run...", 5, "trace")
if(state?.isOnline == false) {
LOG("Skipping the request... Because the zone is unable to send commands while it's in an Offline State.", 1, "warn")
return false
}
if(state?.pauseInStandby == true && state?.inStandby == true) {
LOG("Skipping the request... Because the zone is unable to send commands while the controller is in standby mode.", 1, "warn")
return false
} else { return true }
}
def startZone() {
LOG("startZone()...", 5, "trace")
if(!isCmdOk2Run()) { return }
def zoneNum = device?.latestValue('zoneNumber').toInteger()
def waterTime = device?.latestValue('zoneWaterTime')
LOG("Starting Watering for Zone (${zoneNum}) for (${waterTime}) Minutes", 3, "debug")
def res = parent?.startZone(this, state?.deviceId, zoneNum, waterTime)
if (res) {
log.debug "runThisZone was Sent Successfully: ${res}"
sendEvent(name:'watering', value: "on", displayed: true, isStateChange: true)
sendEvent(name:'switch', value: "on", displayed: false, isStateChange: true)
sendEvent(name:'valve', value: "open", displayed: false, isStateChange: true)
}
else {
markOffLine()
}
}
def on() {
LOG("zone on...", 5, "trace")
if(!isCmdOk2Run()) { return }
def isOn = device?.currentState("switch")?.value.toString() == "on" ? true : false
if (!isOn) { open() }
else { LOG("Zone is Already ON... Ignoring...", 4, "info") }
}
def off() {
LOG("zone off...", 5, "trace")
//if(!isCmdOk2Run()) { return }
def isOff = device?.currentState("switch")?.value.toString() == "off" ? true : false
if (!isOff) { close() }
else { LOG("Zone is Already OFF... Ignoring...", 4, "info") }
}
def open() {
LOG("Zone open()...", 5, "trace")
if(!isCmdOk2Run()) { return }
def isOpen = device?.currentState("valve")?.value.toString() == "open" ? true : false
if (!isOpen) {
startZone()
}
else { LOG("Valve is Already Open... Ignoring...", 4, "info") }
}
def close() {
LOG("Zone close()...", 5, "trace")
//if(!isCmdOk2Run()) { return }
def isClosed = device?.currentState("valve")?.value.toString() == "closed" ? true : false
if (!isClosed) {
def res = parent?.off(this, state?.deviceId)
if (res) {
LOG("Zone was Stopped Successfully...", 4, "info")
sendEvent(name:'watering', value: "off", displayed: true, isStateChange: true)
sendEvent(name:'switch', value: "off", displayed: false, isStateChange: true)
sendEvent(name:'valve', value: "closed", displayed: false, isStateChange: true)
}
} else { LOG("Valve is Already Closed... Ignoring...", 4, "info") }
}
// To be used directly by smart apps
def stopWatering() {
LOG("stopWatering", 5, "trace")
close()
}
def getDtNow() {
def now = new Date()
return formatDt(now, false)
}
def epochToDt(val) {
if(val) { return formatDt(new Date(val)) }
}
def formatDt(dt, mdy = true) {
def formatVal = mdy ? "MMM d, yyyy - h:mm:ss a" : "E MMM dd HH:mm:ss z yyyy"
def tf = new SimpleDateFormat(formatVal)
if(location?.timeZone) { tf.setTimeZone(location?.timeZone) }
return tf.format(dt)
}
//Returns time differences is seconds
def GetTimeValDiff(timeVal) {
try {
def start = new Date(timeVal).getTime()
def now = new Date().getTime()
def diff = (int) (long) (now - start) / 1000
LOG("diff: $diff", 3, "debug")
return diff
}
catch (ex) {
LOG("GetTimeValDiff Exception: ${ex}", 1, "error")
return 1000
}
}
def getTimeDiffSeconds(strtDate, stpDate=null) {
if((strtDate && !stpDate) || (strtDate && stpDate)) {
def now = new Date()
def stopVal = stpDate ? stpDate.toString() : formatDt(now, false)
def start = Date.parse("E MMM dd HH:mm:ss z yyyy", strtDate).getTime()
def stop = Date.parse("E MMM dd HH:mm:ss z yyyy", stopVal).getTime()
def diff = (int) (long) (stop - start) / 1000
return diff
} else { return null }
}
def getTimeDiffSecondsUtc(strtDate, stpDate) {
if(strtDate && stpDate) {
def start = getUnixDtFromUTCDt(strtDate)
def stop = getUnixDtFromUTCDt(stpDate)
def diff = (int) (long) (stop - start) / 1000
return diff
} else { return null }
}
def getUnixDtFromUTCDt(utcDt) {
return Date.parse("yyyy-MM-dd'T'HH:mm:ss'Z'", utcDt).getTime()
}
def formatUtcDt(utcDt, mdy = true) {
def inFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")
inFormat.setTimeZone(TimeZone.getTimeZone("UTC"))
LOG("formatDt($dt, $mdy)...", 5, "trace")
def formatVal = mdy ? "MMM d, yyyy - h:mm:ss a" : "E MMM dd HH:mm:ss z yyyy"
def outFormat = new SimpleDateFormat(formatVal)
if(location?.timeZone) { outFormat.setTimeZone(location?.timeZone) }
return outFormat.format(inFormat.parse(utcDt))
}
def convertSecstoTimeStr(int secondsToConvert) {
long hours = TimeUnit.SECONDS.toHours(secondsToConvert)
long minutes = TimeUnit.SECONDS.toMinutes(secondsToConvert) % TimeUnit.HOURS.toMinutes(1)
long seconds = TimeUnit.SECONDS.toSeconds(secondsToConvert) % TimeUnit.MINUTES.toSeconds(1)
if (hours == 0) return String.format("%02d:%02d", Math.abs(minutes), Math.abs(seconds))
else return String.format("%02d:%02d:%02d", Math.abs(hours), Math.abs(minutes), Math.abs(seconds))
}
// generate custom mobile activity feeds event
void generateActivityFeedsEvent(notificationMessage) {
sendEvent(name: "notificationMessage", value: "${device.name} ${notificationMessage}", descriptionText: "${device.name} ${notificationMessage}", isStateChange: true)
}
static String getTimestamp() {
return new Date().format("yyyy-MM-dd HH:mm:ss z")
}
@Field static final List<String> lLOGTYPES = ['error', 'debug', 'info', 'trace', 'warn']
void LOG(String message, Integer level=3, String logType="debug") {
String dbg=parent.getDebugLevel() == null ? "2" : parent.getDebugLevel()
Integer dbgLevel = dbg.toInteger()
if (logType == "error") {
String a=getTimestamp()
state.lastLOGerror="${message} @ "+a
state.LastLOGerrorDate=a
} else {
if(level > dbgLevel) return
}
if(!lLOGTYPES.contains(logType)) {
logerror("LOG() - Received logType (${logType}) which is not in the list of allowed types ${lLOGTYPES}, message: ${message}, level: ${level}")
logType="debug"
}
if (logType == "debug") log.debug(message)
else if (logType == "error") log.error(message)
else if (logType == "info") log.info(message)
else if (logType == "trade") log.trace(message)
else if (logType == "warn") log.warn(message)
}