This repository has been archived by the owner on Jan 25, 2023. It is now read-only.
/
UPSMonitor.groovy
224 lines (193 loc) · 7.99 KB
/
UPSMonitor.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
/**
* Smart UPS apcupsd Monitor DeviceType
*
* Copyright 2016 Steve White
*
* 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.
*
*/
import groovy.json.JsonSlurper
metadata
{
definition (name: "SmartUPS", namespace: "shackrat", author: "Steve White")
{
capability "Battery"
capability "Voltage Measurement"
capability "Power Source"
capability "Power Meter"
capability "Timed Session"
capability "Refresh"
attribute "loadPercent", "string"
attribute "model", "string"
attribute "serial", "string"
attribute "upsStatus", "string"
attribute "batteryVoltage", "string"
attribute "loadPercent", "string"
attribute "timeOnBattery", "string"
attribute "lifetimeOnBattery", "string"
attribute "lowTransVolts", "string"
attribute "highTransVolts", "string"
attribute "nomPower", "string"
attribute "sensitivity", "string"
attribute "lastUpdate", "string"
attribute "batteryDate", "string"
attribute "lastPowerFail", "string"
attribute "lastPowerRestore", "string"
attribute "lastPowerFailReason", "string"
attribute "batteryRuntime", "string"
command "refresh"
}
preferences
{
section("Device")
{
input("ip", "string", title:"IP Address", description: "IP Address of the Raspberry Pi", defaultValue: "192.168.7." ,required: true, displayDuringSetup: true)
}
section
{
input "enableDebug", "bool", title: "Enable debug logging?", description: "Show detailed responses to commands?", defaultValue: false, required: false, multiple: false
}
}
}
def installed()
{
log.info "Shackrats apcupsd UPS Monitor - UPS Installed"
initialize()
}
def updated()
{
log.info "Shackrats apcupsd UPS Monitor - UPS Updated"
initialize()
refresh()
}
def initialize()
{
unschedule()
log.info "Shackrats apcupsd UPS Monitor - UPS Initialized."
if (enableDebug)
{
log.info "Verbose logging has been enabled for the next 30 minutes."
runIn(1800, logsOff)
}
}
def reset()
{
initialize()
}
def refresh()
{
// Initiate local LAN request
def hubAction = new hubitat.device.HubAction(
method: "GET",
path: "/apc.php?p=1",
headers: [
HOST: ip + ":80",
"Content-Type":"application/json",
Accept: "*/*",
],
body: query
)
if (enableDebug) log.debug "Requesting update from APC service..."
sendHubCommand(hubAction)
}
def parse(String description)
{
def msg = parseLanMessage(description)
//log.trace "PARSED LAN EVENT: " + msg
// Update DNI if changed
if (msg?.mac && msg.mac != device.deviceNetworkId)
{
if (enableDebug) log.debug "Updating DNI to MAC ${msg.mac}..."
device.deviceNetworkId = msg.mac
}
// Response to a push notification
if (msg?.headers?.Referer == "apcupsd" && msg?.body)
{
if (enableDebug) log.trace "Processing LAN event notification..."
def body = msg?.body
def sluper = new JsonSlurper();
def json = sluper.parseText(body)
// Response to an event notification
if (json?.data?.event)
{
log.info "Push notification for UPS event [${json?.data?.event}] detected."
// Update the child device if it's monitored
updatePowerStatus(json.data.event)
}
// Response to a getUPSstatus call
else if (json?.data?.device)
{
if (enableDebug) log.info "Device update received."
updateDeviceStatus(json.data.device)
}
else log.error "ABORTING DUE TO UNKNOWN EVENT"
}
}
def updatePowerStatus(status)
{
def powerSource =
status == "mainsback" ? "mains" :
status == "powerout" ? "battery" :
status == "onbattery" ? "battery" :
status == "offbattery" ? "mains" :
"mains"
sendEvent(name: "powerSource", value: powerSource, displayed: this.currentPowerSource != powerSource ? true : false)
if (powerSource == "mains") sendEvent(name: "sessionStatus", value: "stopped", displayed: this.currentSessionStatus != sessionStatus ? true : false)
else if (powerSource == "battery") sendEvent(name: "sessionStatus", value: "running", displayed: this.currentSessionStatus != sessionStatus ? true : false)
}
def updateDeviceStatus(data)
{
def power = 0
def timeLeft = Math.round(Float.parseFloat(data.timeleft.replace(" Minutes", "")))
def battery = Math.round(Float.parseFloat(data.bcharge.replace(" Percent", "")))
def voltage = Float.parseFloat(data.linev.replace(" Volts", ""))
def batteryVoltage = Float.parseFloat(data.battv.replace(" Volts", ""))
def lowTransVolts = Float.parseFloat(data.lotrans.replace(" Volts", ""))
def highTransVolts = Float.parseFloat(data.hitrans.replace(" Volts", ""))
def loadPercent = Float.parseFloat(data.loadpct.replace(" Percent", ""))
def nomPower = Float.parseFloat(data.nompower.replace(" Watts", ""))
def powerSource =
data.status == "ONLINE" ? "mains" :
data.status == "ONBATT" ? "battery" :
"mains"
// Calculate wattage as a percentage of nominal load
power = ((loadPercent / 100) * nomPower)
sendEvent(name: "powerSource", value: powerSource, displayed: this.currentPowerSource != powerSource ? true : false)
sendEvent(name: "timeRemaining", value: timeLeft, displayed: false)
sendEvent(name: "upsStatus", value: data.status.toLowerCase(), displayed: this.currentUpsStatus != data.status ? true : false)
sendEvent(name: "model", value: data.model, displayed: this.currentModel != data.model ? true : false)
sendEvent(name: "serial", value: data.serialno, displayed: this.currentSerial != data.serialno ? true : false)
sendEvent(name: "battery", value: battery, displayed: this.currentBattery != battery ? true : false)
sendEvent(name: "batteryRuntime", value: data.timeleft, displayed: this.currentRunTimeRemain != data.timeleft ? true : false)
sendEvent(name: "batteryVoltage", value: batteryVoltage, displayed: this.currentBatteryVoltage != batteryVoltage ? true : false)
sendEvent(name: "voltage", value: voltage, descriptionText: "Line voltage is ${voltage} volts.", displayed: this.currentVoltage != voltage ? true : false)
sendEvent(name: "loadPercent", value: loadPercent, displayed: this.currentLoadPercent != loadPercent ? true : false)
sendEvent(name: "timeOnBattery", value: data.tonbatt, displayed: this.currentTimeOnBattery != data.tonbatt ? true : false)
sendEvent(name: "lifetimeOnBattery", value: data.cumonbatt, displayed: this.currentLifetimeOnBattery != data.cumonbatt ? true : false)
sendEvent(name: "lowTransVolts", value: lowTransVolts, displayed: this.currentLowTransVolts != lowTransVolts ? true : false)
sendEvent(name: "highTransVolts", value: highTransVolts, displayed: this.currentHighTransVolts != highTransVolts ? true : false)
sendEvent(name: "sensitivity", value: data.sense, displayed: this.currentSensitivity != data.sense ? true : false)
sendEvent(name: "batteryDate", value: data.battdate, displayed: this.currentBatteryDate != data.battdate ? true : false)
sendEvent(name: "lastUpdate", value: data.date, displayed: false)
sendEvent(name: "nomPower", value: nomPower, displayed: this.currentNomPower != nomPower ? true : false)
sendEvent(name: "power", value: power, displayed: this.currentPower != power ? true : false)
sendEvent(name: "lastPowerFail", value: data.xonbatt, displayed: this.currentNomPower != data.xonbatt ? true : false)
sendEvent(name: "lastPowerRestore", value: data.xoffbatt, displayed: this.currentPower != data.xoffbatt ? true : false)
sendEvent(name: "lastPowerFailReason", value: data.lastxfer, displayed: this.currentLastPowerFailReason != data.lastxfer ? true : false)
}
/*
logsOff
Disables debug logging.
*/
def logsOff()
{
log.warn "debug logging disabled..."
device.updateSetting("enableDebug", [value:"false",type:"bool"])
}