forked from lgkahn/hubitat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLGKUPSStatus.groovy
668 lines (553 loc) · 26.2 KB
/
LGKUPSStatus.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
/**
* Device type to get ups status via telnet and set it in attributes to
* it can be used in rules.. needs to login.
*
*
* Assumptions: APC smart ups device with network card
*
* lgk.com c 2020 free for personal use.. do not post
*
* version 1.2
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* v 1.1 added desciprtive text and cleaned up debugging
* v 1.2 added changes due to varying net card firmware responses, also added some change debugging to help debug alternate smart ups net cards
* also handle issue where some ups return on line , others online
* v 1.3 apparently integer attributes dont work in rule machine . I assumed I needed them to be int to do value comparision but it wasn't working.
Changed them to number not integer.
* v 1.4 added option to enable/disable.
* v 1.5 some ups return on line other on line handle both one with 8 words one with 4
* v 1.6 Add optional runtime for on battery so that you can check the UPS status fewer times and then increase
* the times check when on battery (ie reduce the time say from 30 minutes to 10 etc.)
* v 1.7 fix.. for yet another differing version of responses to get the UPS status. It seems there are as many differnt ups and net card firmwares and responses as days in the month!
* v 1.8 dont change ups status to unknown when starting a check, This was nice but causes and extra firing of any rule.
* v 1.9 add get ups temp both in celsius and f.
* v 1.10 as well as internal temp and battery attributes added them as capabilties as well so you can use standard battery and temp tiles and standard rules on these.
* related also added a pulldown for units for temp ie C or F so the correct temp is set for the capability.
* 1.11 change for alternate etmp config
* v 2 added all kinds of new power and battery attributes. Not all UPS cards have all this info, It will report what it can.
* v 2.1 added two log levels, and auto turn off after 30 minutes.
* v 2.2 fixed typo in attribute name.
* v 2.3 cleared telnet error on close,init commands and also fixed version not passed correctly to attribute.
* v 2.4 change password input type to password from text
* v 2.5 aug 2022 apparently even if ups is charging but you set your low battery duration to a weird number like 20 minutes it will show battery status as discharged until it surpasses that
* so added discharged ups status.
* v 3.0 add new attribures model, serial number, manuf. date, firmware version and battery type.
*/
capability "Battery"
capability "Temperature Measurement"
attribute "lastCommand", "string"
attribute "hoursRemaining", "number"
attribute "minutesRemaining", "number"
attribute "UPSStatus", "string"
attribute "lastUpdate" , "string"
attribute "version", "string"
attribute "name", "string"
attribute "batteryPercentage" , "number"
attribute "currentCheckTime", "number"
attribute "CTemp", "number"
attribute "FTemp", "number"
attribute "telnet", "string"
attribute "outputVoltage", "number"
attribute "inputVoltage", "number"
attribute "outputFrequency", "number"
attribute "inputFrequency", "number"
attribute "outputWattsPercent", "number"
attribute "outputVAPercent", "number"
attribute "outputCurrent", "number"
attribute "outputEnergy", "number"
attribute "batteryVoltage", "number"
attribute "lastSelfTestResult", "string"
attribute "lastSelfTestDate", "string"
attribute "nextBatteryReplacementDate", "string"
attribute "serialNumber" , "string"
attribute "manufDate", "string"
attribute "model", "string"
attribute "firmwareVersion", "string"
attribute "batteryType", "string"
command "refresh"
preferences {
input("UPSIP", "text", title: "Smart UPS (APC only) IP Address?", description: "Enter Smart UPS IP Address?", required: true)
input("UPSPort", "integer", title: "Port #:", description: "Enter port number, default 23", defaultValue: 23)
input("Username", "text", title: "Username for Login?", required: true, defaultValue: "")
input("Password", "password", title: "Password for Login?", required: true, defaultValue: "")
input("runTime", "integer", title: "How often to check UPS Status (in Minutes)>", required: true, defaultValue: 30)
input("runTimeOnBattery", "integer", title: "How often to check UPS Status when on Battery (in Minutes)>", required: true, defaultValue: 10)
input("logLevel", "enum", title: "Logging Level (off,minimial,maximum) ?", options: ["off","minimal", "maximum"], required: true, defaultValue: "off")
input("disable", "bool", title: "Disable?", required: false, defaultValue: false)
input("tempUnits", "enum", title: "Units for Temperature Capabilty?", options: ["F","C"], required: true, defaultValue: "F")
}
metadata {
definition (name: "LGK SmartUPS Status", namespace: "lgkapps", author: "larry kahn [email protected]") {
capability "Refresh"
capability "Actuator"
capability "Telnet"
capability "Configuration"
}
}
def setversion(){
state.name = "LGK SmartUPS Status"
state.version = "3.0"
}
def installed() {
initialize()
}
def updated() {
initialize()
}
def configure()
{
initialize()
}
def getloglevel()
{
if (logLevel == "off")
return(0)
else if (logLevel == "minimal")
return(1)
else return(2)
}
def logsOff()
{
device.updateSetting("logLevel", [value:"off", type:"enum"])
log.warn "Debug logging disabled!"
}
def initialize() {
def scheduleString
setversion()
log.debug "$state.name, Version $state.version startng - IP = $UPSIP, Port = $UPSPort, debug/logging = $logLevel, Status update will run every $runTime minutes."
state.lastMsg = ""
sendEvent(name: "lastCommand", value: "")
sendEvent(name: "hoursRemaining", value: 1000)
sendEvent(name: "minutesRemaining",value: 1000)
//sendEvent(name: "UPSStatus", value: "Unknown")
sendEvent(name: "version", value: state.version)
sendEvent(name: "batteryPercentage", value: "???")
sendEvent(name: "FTemp", value: 0.0)
sendEvent(name: "CTemp", value: 0.0)
sendEvent(name: "telnet", value: "Ok")
if ((tempUnits == null) || (tempUnits == ""))
device.tempUnits = "F"
log.debug "ip = $UPSIP, Port = $UPSPort, Username = $Username, Password = $Password"
if ((UPSIP) && (UPSPort) && (Username) && (Password))
{
def now = new Date().format('MM/dd/yyyy h:mm a',location.timeZone)
sendEvent(name: "lastUpdate", value: now, descriptionText: "Last Update: $now")
unschedule()
if (getloglevel() > 0)
{
log.debug "Scheduling logging to turn off in 30 minutes."
runIn(1800,logsOff)
}
if (!disable)
{
if ((state.origAppName) && (state.origAppName != "") && (state.origAppName != device.getLabel()))
{
device.setLabel(state.origAppName)
}
if (tempUnits)
{
log.debug "Temp. Unit Currently: $tempUnits"
}
// only reset name if was not disabled
if (state.disabled != true) state.origAppName = device.getLabel()
state.disabled = false
log.debug "Scheduling to run Every $runTime Minutes!"
state.currentCheckTime = runTime
sendEvent(name: "currentCheckTime", value: state.currentCheckTime)
scheduleString = "0 */" + runTime.toString() + " * ? * * *"
if (getloglevel() > 1) log.debug "Schedule string = $scheduleString"
schedule(scheduleString, refresh)
sendEvent(name: "lastCommand", value: "Scheduled")
refresh()
}
else
{
log.debug "App. Disabled!"
unschedule()
if (getloglevel() > 0)
{
log.debug "Scheduling logging to turn off in 30 minutes."
runIn(60,logsOff)
}
if ((state.origAppName) && (state.origAppName != ""))
// change name if disbled or enabled
device.setLabel(state.origAppName + " (Disabled)")
state.disabled = true
state.currentCheckTime = 0
sendEvent(name: "currentCheckTime", value: state.currentCheckTime)
}
}
else
{
log.debug "Parameters not filled in yet!"
}
}
def refresh() {
if (!disable)
{
if (getloglevel() > 0) log.debug "lgk SmartUPS Status Version ($state.version)"
sendEvent(name: "lastCommand", value: "initialConnect")
if (getloglevel() > 0) log.debug "Connecting to ${UPSIP}:${UPSPort}"
telnetClose()
telnetConnect(UPSIP, UPSPort.toInteger(), null, null)
}
else
{
log.debug "Refresh called but App is disabled. Will Not Run!"
}
}
def sendData(String msg, Integer millsec) {
if (getloglevel() > 1) log.debug "$msg"
def hubCmd = sendHubCommand(new hubitat.device.HubAction("${msg}", hubitat.device.Protocol.TELNET))
pauseExecution(millsec)
return hubCmd
}
def parse(String msg) {
def lastCommand = device.currentValue("lastCommand")
if (getloglevel() > 1)
{
log.debug "In parse - (${msg})"
log.debug "lastCommand = $lastCommand"
}
def pair = msg.split(" ")
if (getloglevel() > 1)
{
log.debug ""
log.debug "Got server response $msg value = $value lastCommand = ($lastCommand) length = ($pair.length)"
log.debug ""
}
if (lastCommand == "initialConnect")
{
sendEvent(name: "lastCommand", value: "getStatus")
def sndMsg =[
"$Username"
, "$Password"
, "detstatus -rt"
, "detstatus -ss"
, "detstatus -soc"
, "detstatus -tmp"
, "detstatus -all"
, "upsabout"
, "quit"
]
def res1 = seqSend(sndMsg,500)
}
else if (lastCommand == "quit")
{
sendEvent(name: "lastCommand", value: "Rescheduled")
log.debug "Will run again in $state.currentCheckTime Minutes!"
closeConnection()
sendEvent([name: "telnet", value: "Ok"])
}
else
{
if (getloglevel() > 1) log.debug "In getstatus case length = $pair.length"
if (pair.length == 5)
{
def p0 = pair[0]
def p1 = pair[1]
def p2 = pair[2]
def p3 = pair[3]
def p4 = pair[4]
def firmware
if (getloglevel() > 1) log.debug "p0 = $p0 p1 = $p1 p2 = $p2 p3 = $p3 p4 = $p4"
if (p0 == "Output")
{
if ((p1 == "Watts") && (p2 == "Percent:"))
{
sendEvent(name: "outputWattsPercent", value: p3)
if (getloglevel() > 0) log.debug "Output Watts Percent: $p3"
}
else if ((p1 == "VA") && (p2 == "Percent:"))
{
sendEvent(name: "outputVAPercent", value: p3)
if (getloglevel() > 0) log.debug "Output VA Percent: $p3"
}
}
else if ((p0 == "Next") && (p1 == "Battery") && (p2 == "Replacement") && (p3 == "Date:"))
{
sendEvent(name: "nextBatteryReplacementDate", value: p4)
if (getloglevel() > 0) log.debug "Next Battery Replacment Date: $p4"
}
else if ((p0 == "Firmware") && (p1 == "Revision:"))
{
firmware = p2 + " " + p3 + " " + p4
sendEvent(name: "firmwareVersion", value: firmware)
if (getloglevel() > 0) log.debug "Got Firmware version: $firmware"
}
} // length = 5
if (pair.length == 3)
{
def p0 = pair[0]
def p1 = pair[1]
def p2 = pair[2]
def model
if (getloglevel() > 1) log.debug "p0 = $p0 p1 = $p1 p2 = $p2"
if ((p0 == "Self-Test") && (p1 == "Date:"))
{
sendEvent(name: "lastSelfTestDate", value: p2)
if (getloglevel() > 0) log.debug "Last Self Test Date: $p2"
}
else if ((p0 == "Battery") && (p1 == "SKU:"))
{
sendEvent(name: "batteryType", value: p2)
if (getloglevel() > 0) log.debug "Got Battery Type: $p2"
}
else if ((p0 == "Manufacture") && (p1 == "Date:"))
{
sendEvent(name: "manufDate", value: p2)
if (getloglevel() > 0) log.debug "Got Manufacture Date: $p2"
}
else if ((p0 == "Serial") && (p1 == "Number:"))
{
sendEvent(name: "serialNumber", value: p2)
if (getloglevel() > 0) log.debug "Got Serial Number $p2"
}
else if (p0 == "Model:")
{
model = p1 + " " + p2
sendEvent(name: "model", value: model)
if (getloglevel() > 0) log.debug "Got Model: $model"
}
} // length = 3
if (pair.length == 4)
{
def p0 = pair[0]
def p1 = pair[1]
def p2 = pair[2]
def p3 = pair[3]
if (getloglevel() > 1) log.debug "p0 = $p0 p1 = $p1 p2 = $p2 p3 = $p3"
if (p0 == "Output")
{
if (p1 == "Voltage:")
{
sendEvent(name: "outputVoltage", value: p2)
if (getloglevel() > 0) log.debug "Output Voltage: $p2"
}
else if (p1 == "Frequency:")
{
sendEvent(name: "outputFrequency", value: p2)
if (getloglevel() > 0) log.debug "Output Frequency: $p2"
}
else if (p1 == "Current:")
{
sendEvent(name: "outputCurrent", value: p2)
if (getloglevel() > 0) log.debug "Output Current: $p2"
}
else if (p1 == "Energy:")
{
sendEvent(name: "outputEnergy", value: p2)
if (getloglevel() > 0) log.debug "Output Energy: $p2"
}
}
else if (p0 == "Input")
{
if (p1 == "Voltage:")
{
sendEvent(name: "inputVoltage", value: p2)
if (getloglevel() > 0) log.debug "Input Voltage: $p2"
}
else if (p1 == "Frequency:")
{
sendEvent(name: "inputFrequency", value: p2)
if (getloglevel() > 0) log.debug "Input Frequency: $p2"
}
}
else if ((p0 == "Battery") && (p1 == "Voltage:"))
{
sendEvent(name: "batteryVoltage", value: p2)
if (getloglevel() > 0) log.debug "Battery Voltage: $p2"
}
if ((p0 == "Status") && (p1 == "of") && (p2 == "UPS:"))
{
def thestatus = p3
if (getloglevel() > 1) log.debug ""
// handle on line versus online case combiner p3 and p4
if ((p3 == "OnLine") || (p3 == "Online"))
{
thestatus = p3
}
if ((thestatus == "OnLine,") || (thestatus == "Online"))
thestatus = "OnLine"
if ((thestatus == "Discharged,") || (thestatus == "Discharged"))
thestatus = "Discharged"
if (thestatus == "OnBattery,")
thestatus = "OnBattery"
if (getloglevel() > 1) log.debug "*********************************"
log.debug "Got UPS Status = $thestatus!"
if (getloglevel() > 1) log.debug "*********************************"
sendEvent(name: "UPSStatus", value: thestatus)
if ((thestatus == "OnBattery") && (runTime != runTimeOnBattery) && (state.currentCheckTime != runTimeOnBattery))
{
log.debug "On Battery so Resetting Check time to $runTimeOnBattery Minutes!"
unschedule()
scheduleString = "0 */" + runTimeOnBattery.toString() + " * ? * * *"
if (getloglevel() > 1) log.debug "Schedule string = $scheduleString"
state.currentCheckTime = runTimeOnBattery
sendEvent(name: "currentCheckTime", value: state.currentCheckTime)
schedule(scheduleString, refresh)
}
else if ((thestatus == "OnLine") && (runTime != runTimeOnBattery) && (state.currentCheckTime != runTime))
{
log.debug "UPS Back Online, so Resetting Check time to $runTime Minutes!"
unschedule()
scheduleString = "0 */" + runTime.toString() + " * ? * * *"
if (getloglevel() > 1) log.debug "Schedule string = $scheduleString"
state.currentCheckTime = runTime
sendEvent(name: "currentCheckTime", value: state.currentCheckTime)
schedule(scheduleString, refresh)
}
}
} // length = 4
if ((pair.length == 7) || (pair.length == 8) || (pair.length == 5) || (pair.length == 11))
{
def p0 = pair[0]
def p1 = pair[1]
def p2 = pair[2]
def p3 = pair[3]
def p4 = pair[4]
if ((p0 == "Status") && (p1 == "of") && (p2 == "UPS:"))
{
def thestatus = p3
if (getloglevel() > 1) log.debug ""
// handle on line versus online case combiner p3 and p4
if ((p3 == "OnLine") || (p3 == "Online"))
{
thestatus = p3
}
else if (p3 == "On")
{
thestatus = p3 + p4
}
if ((thestatus == "OnLine,") || (thestatus == "Online"))
thestatus = "OnLine"
if (thestatus == "OnBattery,")
thestatus = "OnBattery"
if (getloglevel() > 1) log.debug "*********************************"
log.debug "Got UPS Status = $thestatus!"
if (getloglevel() > 1) log.debug "*********************************"
sendEvent(name: "UPSStatus", value: thestatus)
if ((thestatus == "OnBattery") && (runTime != runTimeOnBattery) && (state.currentCheckTime != runTimeOnBattery))
{
log.debug "On Battery so Resetting Check time to $runTimeOnBattery Minutes!"
unschedule()
scheduleString = "0 */" + runTimeOnBattery.toString() + " * ? * * *"
if (getloglevel() > 1) log.debug "Schedule string = $scheduleString"
state.currentCheckTime = runTimeOnBattery
sendEvent(name: "currentCheckTime", value: state.currentCheckTime)
schedule(scheduleString, refresh)
}
else if ((thestatus == "OnLine") && (runTime != runTimeOnBattery) && (state.currentCheckTime != runTime))
{
log.debug "UPS Back Online, so Resetting Check time to $runTime Minutes!"
unschedule()
scheduleString = "0 */" + runTime.toString() + " * ? * * *"
if (getloglevel() > 1) log.debug "Schedule string = $scheduleString"
state.currentCheckTime = runTime
sendEvent(name: "currentCheckTime", value: state.currentCheckTime)
schedule(scheduleString, refresh)
}
}
} // length = 7
if (pair.length == 6)
{
def p0 = pair[0]
def p1 = pair[1]
def p2 = pair[2]
def p3 = pair[3]
def p4 = pair[4]
def p5 = pair[5]
if (getloglevel() > 1) log.debug "p0 = $p0 p1 = $p1 p2 = $p2 p3 = $p3 p4 = $p4 p5 = $p5"
if ((p0 == "Self-Test") && (p1 == "Result:"))
{
def theResult = p2 + " " +p3 + " " + p4 + " " + p5
sendEvent(name: "lastSelfTestResult", value: theResult)
if (getloglevel() > 0) log.debug "Last Self Test Result: $theResult"
}
if ((p0 == "Battery") && (p1 == "State") && (p3 == "Charge:"))
{
def p4dec = p4.toDouble() / 100.0
int p4int = p4dec * 100
if (getloglevel() > 1) log.debug "********************************"
log.debug "UPS Battery Percentage: $p4!"
if (getloglevel() > 1) log.debug "*********************************"
sendEvent(name: "batteryPercentage", value: p4int)
sendEvent(name: "battery", value: p4int, unit: "%")
}
if (((p0 == "Internal") || (p0 == "Battery")) && (p1 == "Temperature:"))
{
if (getloglevel() > 1) log.debug "********************************"
if (getloglevel() > 0)
{
log.debug "Got C Temp = $p2!"
log.debug "Got F Temp = $p4!"
}
if (getloglevel() > 1) log.debug "********************************"
sendEvent(name: "CTemp", value: p2)
sendEvent(name: "FTemp", value: p4)
if (tempUnits == "F")
sendEvent(name: "temperature", value: p4, unit: tempUnits)
else
sendEvent(name: "temperature", value: p2, unit: tempUnits)
}
} // length = 6
if ((pair.length == 8) || (pair.length == 6))
{
def p0 = pair[0]
def p1 = pair[1]
def p2 = pair[2]
def p3 = pair[3]
def p4 = pair[4]
def p5 = pair[5]
if (getloglevel() > 1) log.debug "p0 = $p0 p1 = $p1 p2 = $p2 p3 = $p3 p4 = $p4 p5 = $p5"
// looking for hours and minutes
// Runtime Remaining: 2 hr 19 min 0 sec
if ((p0 == "Runtime") && (p1 == "Remaining:") && (p3 == "hr"))
{
if (getloglevel() > 1) log.debug "********************************"
log.debug "Got $p2 hours Remaining!"
if (getloglevel() > 1) log.debug "********************************"
sendEvent(name: "hoursRemaining", value: p2.toInteger())
state.hoursRemaining = p2.toInteger()
}
if ((p0 == "Runtime") && (p1 == "Remaining:") && (p5 == "min"))
{
if (getloglevel() > 1) log.debug "********************************"
log.debug "Got $p4 minutes Remaining!"
if (getloglevel() > 1) log.debug "********************************"
sendEvent(name: "minutesRemaining", value: p4.toInteger())
state.minutesRemaining = p4.toInteger()
def now = new Date().format('MM/dd/yyyy h:mm a',location.timeZone)
sendEvent(name: "lastUpdate", value: now, descriptionText: "Last Update: $now")
sendEvent(name: "lastCommand", value: "quit")
def res1 = sendData("quit",500)
}
} // length = 6
}
}
def telnetStatus(status) {
if (getloglevel() > 1) log.debug "telnetStatus: ${status}"
sendEvent([name: "telnet", value: "${status}"])
}
def closeConnection()
{
if (closeTelnet){
try {
telnetClose()
} catch(e) {
if (getloglevel() > 1) log.debug("Connection Closed")
}
}
}
boolean seqSend(msgs, Integer millisec)
{
if (getloglevel() > 1) log.debug "in sendData"
msgs.each {
sendData("${it}",millisec)
}
seqSent = true
return seqSent
}