-
Notifications
You must be signed in to change notification settings - Fork 14
/
batterymonitor.groovey
432 lines (363 loc) · 13.6 KB
/
batterymonitor.groovey
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
// lgk change all \n to \r\n so att mail to text does not reject the email
//
def smartAppNameFull() {
return "BatteryMonitor SmartApp for Hubitat"
}
def smartAppNameShort() {
return "BatteryMonitor"
}
def smartAppVersion() {
return "Version 1.1"
}
def smartAppAuthor() {
return "Author Brandon Gordon, larry kahn"
}
def smartAppCopyright() {
return "Copyright (c) 2014 Brandon Gordon, 2024 larry kah "
}
def smartAppSource() {
return "https://github.com/notoriousbdg/SmartThings.BatteryMonitor"
}
def smartAppDescription() {
return "This SmartApp helps you monitor the status of your SmartThings devices with batteries."
}
def smartAppRevision () {
return '2014-11-14 v0.0.1\r\n' +
' * Initial release\r\n' +
'2014-11-15 v0.0.2\r\n' +
' * Moved status to main page\r\n' +
' * Removed status page\r\n' +
' * Improved formatting of status page\r\n' +
' * Added low, medium, high thresholds\r\n' +
' * Handle battery status strings of OK and Low\r\n\r\n' +
'2014-11-15 v0.0.3\r\n' +
' * Added push notifications\r\n\r\n' +
'2014-11-20 v0.0.4\r\n' +
' * Added error handling for batteries that return strings\r\n\r\n' +
'2014-12-26 v0.0.5\r\n' +
' * Move app metadata to a new about page\r\n' +
' * Changed notifications to only send at specified time daily\r\n' +
' * modified by lgkahn to send complete message whenever the schedule fires, showing the status of all devices.\r\n'
}
def smartAppLicense() {
return '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:' +
'\r\n\r\n' +
'http://www.apache.org/licenses/LICENSE-2.0' +
'\r\n\r\n' +
'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.'
}
definition(
name: "BatteryMonitor",
namespace: "notoriousbdg",
author: "Brandon Gordon",
description: "SmartApp to monitor battery levels.",
category: "Convenience",
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/[email protected]")
preferences {
page name:"pageStatus"
page name:"pageConfigure"
page name:"pageAbout"
}
// Show About Page
def pageAbout() {
def pageProperties = [
name: "pageAbout",
title: smartAppNameFull(),
nextPage: "pageConfigure",
uninstall: true
]
return dynamicPage(pageProperties) {
section() {
paragraph smartAppVersion() + "\r\n" +
smartAppAuthor() + "\r\n" +
smartAppCopyright()
}
section() {
paragraph smartAppDescription()
}
section() {
href(
name: "sourceCode",
title: "Source Code (Tap to view)",
required: false,
external: true,
style: "external",
url: smartAppSource(),
description: smartAppSource()
)
}
section() {
paragraph title: "Revision History",
smartAppRevision()
}
section() {
paragraph title: "License",
smartAppLicense()
}
}
}
// Show Status page
def pageStatus() {
def pageProperties = [
name: "pageStatus",
title: smartAppNameShort() + " Status",
nextPage: null,
install: true,
uninstall: true
]
if (settings.devices == null) {
return pageAbout()
}
def listLevel0 = ""
def listLevel1 = ""
def listLevel2 = ""
def listLevel3 = ""
def listLevel4 = ""
if (settings.level1 == null) { settings.level1 = 33 }
if (settings.level3 == null) { settings.level3 = 67 }
if (settings.pushMessage) { settings.pushMessage = true }
return dynamicPage(pageProperties) {
settings.devices.each() {
try {
if (it.currentBattery == null) {
listLevel0 += "$it.displayName\r\n"
} else if (it.currentBattery >= 0 && it.currentBattery < settings.level1.toInteger()) {
listLevel1 += "$it.currentBattery $it.displayName\r\n"
} else if (it.currentBattery >= settings.level1.toInteger() && it.currentBattery <= settings.level3.toInteger()) {
listLevel2 += "$it.currentBattery $it.displayName\r\n"
} else if (it.currentBattery > settings.level3.toInteger() && it.currentBattery < 100) {
listLevel3 += "$it.currentBattery $it.displayName\r\n"
} else if (it.currentBattery == 100) {
listLevel4 += "$it.displayName\r\n"
} else {
listLevel0 += "$it.currentBattery $it.displayName\r\n"
}
} catch (e) {
log.trace "Caught error checking battery status."
log.trace e
listLevel0 += "$it.displayName\r\n\r\n"
}
}
if (listLevel0) {
section("Batteries with errors or no status") {
paragraph listLevel0.trim()
}
}
if (listLevel1) {
section("Batteries with low charge (less than $settings.level1)") {
paragraph listLevel1.trim()
}
}
if (listLevel2) {
section("Batteries with medium charge (between $settings.level1 and $settings.level3)") {
paragraph listLevel2.trim()
}
}
if (listLevel3) {
section("Batteries with high charge (more than $settings.level3)") {
paragraph listLevel3.trim()
}
}
if (listLevel4) {
section("Batteries with full charge") {
paragraph listLevel4.trim()
}
}
section("Menu") {
href "pageStatus", title:"Refresh", description:""
href "pageConfigure", title:"Configure", description:""
href "pageAbout", title:"About", description: ""
}
}
}
// Show Configure Page
def pageConfigure() {
def helpPage =
"Select devices with batteries that you wish to monitor."
def inputBattery = [
name: "devices",
type: "capability.battery",
title: "Which devices with batteries?",
multiple: true,
required: true
]
def inputLevel1 = [
name: "level1",
type: "number",
title: "Low battery threshold?",
defaultValue: "20",
required: true
]
def inputLevel3 = [
name: "level3",
type: "number",
title: "Medium battery threshold?",
defaultValue: "70",
required: true
]
def inputTime = [
name: "time",
type: "time",
title: "Notify at what time daily?",
required: true
]
def nightlyStatusMsg = [
name: "nightlyStatus",
type: "bool",
title: "Send scheduled status message for all devices?",
defaultValue: true
]
def inputPush = [
name: "pushMessage",
type: "bool",
title: "Send push notifications?",
defaultValue: true
]
/* def inputSMS = [
name: "phoneNumber",
type: "phone",
title: "Send SMS notifications to?",
required: false
]
*/
def pageProperties = [
name: "pageConfigure",
title: smartAppNameShort() + " Configuration",
nextPage: "pageStatus",
uninstall: true
]
return dynamicPage(pageProperties) {
section("About") {
paragraph helpPage
}
section("Devices") {
input inputBattery
}
section("Settings") {
input inputLevel1
input inputLevel3
}
section("Notification") {
input inputTime
input inputPush
input "sendPushMessage", "capability.notification", title: "Notification Devices: Hubitat PhoneApp or Pushover", multiple: true, required: false
input nightlyStatusMsg
// input inputSMS
}
section([title:"Options", mobileOnly:true]) {
label title:"Assign a name", required:false
}
}
}
def installed() {
log.debug "Initialized with settings: ${settings}"
initialize()
}
def updated() {
unschedule()
unsubscribe()
initialize()
//nightlyStatus() for testing fx
// log.debug "in updated pushmessage = $settings.pushMessage"
}
def initialize() {
schedule(settings.time, updateStatus)
}
def send(msg) {
log.debug msg
if (settings.pushMessage) {
sendPushMessage.deviceNotification(msg)
}
/* } else {
sendNotificationEvent(msg)
}*/
/* if (settings.phoneNumber != null) {
sendSms(phoneNumber, msg)
}*/
}
// lgk now fx to prepare nightly message and send to get complete status not just outstanding devices.
def nightlyStatus()
{
def listLevel0 = ""
def listLevel1 = ""
def listLevel2 = ""
def listLevel3 = ""
def listLevel4 = ""
def myhub = location.hubs[0].name
def outgoingMsg = "For Hub: $myhub\r\n"
if (settings.level1 == null) { settings.level1 = 33 }
if (settings.level3 == null) { settings.level3 = 67 }
if (settings.nightlyStatus == true)
{
settings.devices.each() {
try {
if (it.currentBattery == null) {
listLevel0 += "$it.displayName\r\n"
} else if (it.currentBattery >= 0 && it.currentBattery < settings.level1.toInteger()) {
listLevel1 += "$it.currentBattery $it.displayName\r\n"
} else if (it.currentBattery >= settings.level1.toInteger() && it.currentBattery <= settings.level3.toInteger()) {
listLevel2 += "$it.currentBattery $it.displayName\r\n"
} else if (it.currentBattery > settings.level3.toInteger() && it.currentBattery < 100) {
listLevel3 += "$it.currentBattery $it.displayName\r\n"
} else if (it.currentBattery == 100) {
listLevel4 += "$it.displayName\r\n"
} else {
listLevel0 += "$it.currentBattery $it.displayName\r\n"
}
} catch (e) {
log.trace "Caught error checking battery status."
log.trace e
listLevel0 += "$it.displayName\r\n"
}
}
if (listLevel0) {
outgoingMsg = outgoingMsg + "Batteries with errors or no status\r\n\r\n" +
listLevel0.trim()
}
// prepare message
if (listLevel1) {
outgoingMsg = outgoingMsg + "\r\n\r\nBatteries with low charge (less than $settings.level1)\r\n\r\n" +
listLevel1.trim()
}
if (listLevel2) {
outgoingMsg = outgoingMsg + "\r\n\r\nBatteries with medium charge (between $settings.level1 and $settings.level3)\r\n\r\n" +
listLevel2.trim()
}
if (listLevel3) {
outgoingMsg = outgoingMsg + "\r\n\r\nBatteries with high charge (more than $settings.level3)\r\n\r\n" +
listLevel3.trim()
}
if (listLevel4) {
outgoingMsg = outgoingMsg + "\r\n\r\nBatteries with full charge\r\n\r\n" +
listLevel4.trim()
}
// lgk now send message
//log.debug "nightly message is $outgoingMsg"
send(outgoingMsg)
}
}
def updateStatus() {
settings.devices.each() {
try {
if (it.currentBattery == null) {
send("${it.displayName} battery is not reporting.")
} else if (it.currentBattery > 100) {
send("${it.displayName} battery is ${it.currentBattery}, which is over 100.")
} else if (it.currentBattery < settings.level1) {
send("${it.displayName} battery is ${it.currentBattery} (threshold ${settings.level1}.)")
}
} catch (e) {
log.trace "Caught error checking battery status."
log.trace e
send("${it.displayName} battery reported a non-integer level.")
}
}
nightlyStatus()
}