-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathLGKUPSStatus.groovy
1026 lines (830 loc) · 39.9 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
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
/**
* 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.
* v 3.5 cannot do quit at end of battery and cannot include in initial string as the libraries now close the telnet immediately
* change order to detstatus -all last and close on battery sku which i hope every one returns.
* v 3.6 add outputWatts = outputCurrent * outputVoltage truncate to integer.
* v 3.7 fix issue where more that 59 minutes causes error add range to parameters.
* v 3.8 was turning off debugging even if set to minimal only turn off specifically if set to maximum.
* v 3.9 in order to get all output some were repeatedly coming out more than once, there was no way around this so added variabled to not put out more than once.
* also change messages that cannot be turned off to info instead of debug.
* v 4.0 add minute offset for runtime, so with multiple device say you schedule every 15 minutes with offset 2, it will run at 2 past the hour then 17 past the hour etc
* in this way you can have multiple ups's and have them run every x minutes but stagger them so they dont all run at the same time.
* v 4.1 only put out password if max debug level set.. otherwise less info
* v 4.2 add connectStatus check if Trying in rule and if soo for a long time means it times out .. used to toggle a switch to reboot my wifi connector.
* v 4.3 add sku to differentiate between smt1500 and smt1500c
*/
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 "connectStatus", "string"
attribute "nextBatteryReplacementDate", "string"
attribute "serialNumber" , "string"
attribute "manufDate", "string"
attribute "model", "string"
attribute "firmwareVersion", "string"
attribute "batteryType", "string"
attribute "outputWatts", "number"
attribute "SKU", "string"
attribute "lastCommandResult", "string"
command "refresh"
command "UPS_Reboot"
command "UPS_Sleep"
command "UPS_RuntimeCalibrate"
command ("UPS_SetOutletGroup", [
[ "name": "outletGroup",
"description": "Outlet Group 0 or 1",
"type": "ENUM",
"constraints": ["1","2"],
"required": true,
"default": "1"
],
[
"name": "command",
"description": "Command?",
"type": "ENUM",
"constraints": ["Off","On","DelayOff","DelayOn","Reboot","DelayReboot", "Shutdown","DelayShutdown","Cancel"],
"required": true
],
[
"name": "seconds",
"description": "Delay in seconds?",
"type": "ENUM",
"constraints": ["1","2","3","4","5","10","20","30","60","120","180","240","300","600"],
"required": true
]
] )
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", "number", title: "How often to check UPS Status (in Minutes 1-59)?", required: true, defaultValue: 30, range: "1..59")
input("runOffset", "number", title: "Offset to run how many minutes past the hour, (used with multiple UPS's so they all don't run at once (0-59)?", required: true, defalutValue: 0, range: "0..59")
input("runTimeOnBattery", "number", title: "How often to check UPS Status when on Battery (in Minutes 1-59)?", required: true, defaultValue: 10,range: "1..59")
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 = "4.4"
}
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")
sendEvent(name: "connectStatus", value: "Initialized")
sendEvent(name: "lastCommandResult", value: "NA")
if ((tempUnits == null) || (tempUnits == ""))
device.tempUnits = "F"
if (getloglevel() > 1)
log.debug "ip = $UPSIP, Port = $UPSPort, Username = $Username, Password = $Password"
else
log.info "ip = $UPSIP, Port = $UPSPort"
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() > 1)
{
log.debug "Scheduling logging to turn off in 30 minutes."
runIn(1800,logsOff)
}
// make sure inputs are integers note cannot do this directly inline in the update staements as they come out as 2.0 instead.
def runTimeInt = runTime.toDouble().trunc().toInteger()
def runTimeOnBatteryInt = runTimeOnBattery.toDouble().trunc().toInteger()
def runOffsetInt = runOffset.toDouble().trunc().toInteger()
device.updateSetting("runTime", [value: runTimeInt , type:"number"])
device.updateSetting("runTimeOnBattery", [value: runTimeOnBatteryInt , type:"number"])
device.updateSetting("runOffset", [value: runOffsetInt, type: "number"])
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 ${runTimeInt.toString()} Minutes, at ${runOffsetInt.toString()} past the hour."
state.currentCheckTime = runTimeInt
sendEvent(name: "currentCheckTime", value: state.currentCheckTime)
scheduleString = "0 " + runOffsetInt.toString() + "/" + runTimeInt.toString() + " * ? * * *"
// scheduleString = "0 */" + runTimeInt.toString() + " * ? * * *"
state.CronString = scheduleString
if (getloglevel() > 0) 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 UPS_Reboot()
{
sendEvent(name: "lastCommandResult", value: "NA")
if (getloglevel() > 0) log.info "Reboot called!"
if (!disable)
{
if (getloglevel() > 0) log.debug "lgk SmartUPS Status Version ($state.version)"
sendEvent(name: "lastCommand", value: "RebootConnect")
sendEvent(name: "connectStatus", value: "Trying")
if (getloglevel() > 0) log.debug "Connecting to ${UPSIP}:${UPSPort}"
telnetClose()
telnetConnect(UPSIP, UPSPort.toInteger(), null, null)
}
else
{
log.debug "Reboot called but App is disabled. Will Not Run!"
}
}
def UPS_Sleep()
{
sendEvent(name: "lastCommandResult", value: "NA")
if (getloglevel() > 0) log.info "Sleep called!"
if (!disable)
{
if (getloglevel() > 0) log.debug "lgk SmartUPS Status Version ($state.version)"
sendEvent(name: "lastCommand", value: "SleepConnect")
sendEvent(name: "connectStatus", value: "Trying")
if (getloglevel() > 0) log.debug "Connecting to ${UPSIP}:${UPSPort}"
telnetClose()
telnetConnect(UPSIP, UPSPort.toInteger(), null, null)
}
else
{
log.debug "Sleep called but App is disabled. Will Not Run!"
}
}
def UPS_RuntimeCalibrate()
{
sendEvent(name: "lastCommandResult", value: "NA")
if (getloglevel() > 0) log.info "Runtime Calibrate called!"
if (!disable)
{
if (getloglevel() > 0) log.debug "lgk SmartUPS Status Version ($state.version)"
sendEvent(name: "lastCommand", value: "CalibrateConnect")
sendEvent(name: "connectStatus", value: "Trying")
if (getloglevel() > 0) log.debug "Connecting to ${UPSIP}:${UPSPort}"
telnetClose()
telnetConnect(UPSIP, UPSPort.toInteger(), null, null)
}
else
{
log.debug "Calibrate called but App is disabled. Will Not Run!"
}
}
def UPS_SetOutletGroup(p1, p2, p3)
{
def goOn = true
state.outlet = ""
state.commmand = ""
state.seconds = ""
sendEvent(name: "lastCommandResult", value: "NA")
log.info "Set Outlet Group called! [$p1 $p2 $p3]"
if (p1 == null)
{
log.error "Outlet is required"
goOn = false
}
else state.outlet = p1
if (p2 == null)
{
log.error "Command is required!"
goOn = false
}
else state.command = p2
if (p3 == null)
{
// default delay to 0 if null
state.seconds = "0"
}
else state.seconds = p3
if (goOn)
{
if (!disable)
{
if (getloglevel() > 0) log.debug "lgk SmartUPS Status Version ($state.version)"
sendEvent(name: "lastCommand", value: "SetOutletGroupConnect")
sendEvent(name: "connectStatus", value: "Trying")
if (getloglevel() > 0) log.debug "Connecting to ${UPSIP}:${UPSPort}"
telnetClose()
telnetConnect(UPSIP, UPSPort.toInteger(), null, null)
} // not disable
else
{
log.debug "SetOutletGroup called but App is disabled. Will Not Run!"
} // disable
} //go on
}
def refresh() {
if (!disable)
{
state.upsBattery = "Unknown"
state.runtime = "Unknown"
state.upsStatus = "Unknown"
state.nextRunTime = "Unknown"
if (getloglevel() > 0) log.debug "lgk SmartUPS Status Version ($state.version)"
sendEvent(name: "lastCommand", value: "initialConnect")
sendEvent(name: "connectStatus", value: "Trying")
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 == "RebootConnect")
{
sendEvent(name: "connectStatus", value: "Connected")
sendEvent(name: "lastCommand", value: "Reboot")
def sndMsg =[
"$Username"
, "$Password"
, "UPS -c reboot"
]
def res1 = seqSend(sndMsg,500)
}
else if (lastCommand == "SleepConnect")
{
sendEvent(name: "connectStatus", value: "Connected")
sendEvent(name: "lastCommand", value: "Sleep")
def sndMsg =[
"$Username"
, "$Password"
, "UPS -c sleep"
]
def res1 = seqSend(sndMsg,500)
}
else if (lastCommand == "CalibrateConnect")
{
sendEvent(name: "connectStatus", value: "Connected")
sendEvent(name: "lastCommand", value: "RuntimeCalibrate")
def sndMsg =[
"$Username"
, "$Password"
, "UPS -r start"
]
def res1 = seqSend(sndMsg,500)
}
else if (lastCommand == "SetOutletGroupConnect")
{
sendEvent(name: "connectStatus", value: "Connected")
sendEvent(name: "lastCommand", value: "SetOutletGroup")
if (getloglevel() > 1) log.debug "in set outlet group"
if (getloglevel() > 1) log.debug "outlet = ${state.outlet}, command = ${state.command}, seconds = ${state.seconds}"
def sndMsg =[
"$Username"
, "$Password"
, "UPS -o ${state.outlet} ${state.command} ${state.seconds}"
]
def res1 = seqSend(sndMsg,500)
}
else if (lastCommand == "initialConnect")
{
sendEvent(name: "connectStatus", value: "Connected")
sendEvent(name: "lastCommand", value: "getStatus")
def sndMsg =[
"$Username"
, "$Password"
, "detstatus -rt"
, "detstatus -ss"
, "detstatus -soc"
, "detstatus -all"
, "detstatus -tmp"
, "upsabout"
]
def res1 = seqSend(sndMsg,500)
}
else if (lastCommand == "quit")// , "quit"
{
sendEvent(name: "lastCommand", value: "Rescheduled")
if (state.nextRunTime == "Unknown") log.info "Will run again in $state.currentCheckTime Minutes!"
state.nextRunTime = state.currentCheckTime
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 == 2)
{
def p0 = pair[0]
def p1 = pair[1]
if (getloglevel() > 1) log.debug "p1 = $p0 p1 = $p1"
if ( ((p0 == "E000:") || (p1 = "E001:")) && (p1 == "Success"))
{
if ((lastcommand == "Reboot") || (lastCommand == "Sleep") || (lastCommand == "RuntimeCalibrate") || (lastCommand == "SetOutletGroup"))
{
log.info "Command Sucessfully executed. [$p0, $p1]"
sendEvent(name: "lastCommandResult", value: "Success")
closeConnection()
sendEvent([name: "telnet", value: "Ok"])
}
}
else if ( (p0 == "E002:") || (p0 == "E100:") || (p0 == "E101:") || (p0 == "E102:") || (p0 == "E103:") || (p0 == "E107:") || (p0 == "E108:") )
{
log.error "Error: Command Returned [$p0, $p1]"
sendEvent(name: "lastCommandResult", value: "Failure")
closeConnection()
sendEvent([name: "telnet", value: "Ok"])
}
else if (p0 == "SKU:")
{
sendEvent(name: "SKU", value: p1)
if (getloglevel() > 0) log.debug "Got SKU: $p1"
}
} // end length 2
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 == "E002:") || (p0 == "E100:") || (p0 == "E101:") || (p0 == "E102:") || (p0 == "E103:") || (p0 == "E107:") || (p0 == "E108:") )
{
log.error "Error: Command Returned [$p0, $p1 $p2]"
sendEvent(name: "lastCommandResult", value: "Failure")
closeConnection()
sendEvent([name: "telnet", value: "Ok"])
}
else 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"
sendEvent(name: "lastCommand", value: "quit")
def res1 = sendData("quit",500)
}
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 == "E002:") || (p0 == "E100:") || (p0 == "E101:") || (p0 == "E102:") || (p0 == "E103:") || (p0 == "E107:") || (p0 == "E108:") )
{
log.error "Error: Command Returned [$p0, $p1 $p2 $p3]"
sendEvent(name: "lastCommandResult", value: "Failure")
closeConnection()
sendEvent([name: "telnet", value: "Ok"])
}
else if (p0 == "Output")
{
if (p1 == "Voltage:")
{
sendEvent(name: "outputVoltage", value: p2)
if (getloglevel() > 0) log.debug "Output Voltage: $p2"
state.outputVoltage = 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"
double watts = state.outputVoltage.toDouble() * p2.toDouble()
def intWatts = watts.toInteger()
sendEvent(name: "outputWatts", value: intWatts)
}
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.info "*********************************"
if (state.upsStatus == "Unknown") log.info "Got UPS Status = $thestatus!"
state.upsStatus = theStatus
if (getloglevel() > 1) log.info "*********************************"
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 " + runOffset.toString() + "/" + runTimeOnBattery.toString() + " * ? * * *"
//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 " + runOffset.toString() + "/" + runTime.toString() + " * ? * * *"
//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 == "E002:") || (p0 == "E100:") || (p0 == "E101:") || (p0 == "E102:") || (p0 == "E103:") || (p0 == "E107:") || (p0 == "E108:") )
{
log.error "Error: Command Returned [$p0, $p1 $p2 $p3 $p4 $p5 $p6]"
sendEvent(name: "lastCommandResult", value: "Failure")
closeConnection()
sendEvent([name: "telnet", value: "Ok"])
}
else 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.info "*********************************"
if (state.upsStatus == "Unknown") log.info "Got UPS Status = $thestatus!"
state.upsStatus = theStatus
if (getloglevel() > 1) log.info "*********************************"
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 " + runOffset.toString() + "/" + runTimeOnBattery.toString() + " * ? * * *"
//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 " + runOffset.toString() + "/" + runTime.toString() + " * ? * * *"
//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 == "E002:") || (p0 == "E100:") || (p0 == "E101:") || (p0 == "E102:") || (p0 == "E103:") || (p0 == "E107:") || (p0 == "E108:") )
{
log.error "Error: Command Returned [$p0, $p1 $p2 $p3 $p4 $p5]"
sendEvent(name: "lastCommandResult", value: "Failure")
closeConnection()
sendEvent([name: "telnet", value: "Ok"])
}
else
{
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.info "********************************"
if (state.upsBattery == "Unknown") log.info "UPS Battery Percentage: $p4!"
state.upsBattery = p4
if (getloglevel() > 1) log.info "*********************************"
sendEvent(name: "batteryPercentage", value: p4int)
sendEvent(name: "battery", value: p4int, unit: "%")
}
if (((p0 == "Internal") || (p0 == "Battery")) && (p1 == "Temperature:"))
{
if (getloglevel() > 1) log.info "********************************"
if (getloglevel() > 0)
{
log.debug "Got C Temp = $p2!"
log.debug "Got F Temp = $p4!"
}
if (getloglevel() > 1) log.info "********************************"
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.info "********************************"
if (state.runtime == "Unknown") log.info "Got $p2 hours Remaining!"
if (getloglevel() > 1) log.info "********************************"
sendEvent(name: "hoursRemaining", value: p2.toInteger())
state.hoursRemaining = p2.toInteger()
}
if ((p0 == "Runtime") && (p1 == "Remaining:") && (p5 == "min"))
{
if (getloglevel() > 1) log.info "********************************"
if (state.runtime == "Unknown") log.info "Got $p4 minutes Remaining!"
state.runtime = p4
if (getloglevel() > 1) log.info "********************************"
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")
}
} // length = 6
}
}
def telnetStatus(status) {
if (getloglevel() > 1) log.debug "telnetStatus: ${status}"