-
Notifications
You must be signed in to change notification settings - Fork 0
/
low_battery_notification.app.groovy
83 lines (57 loc) · 1.87 KB
/
low_battery_notification.app.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
/**
* Battery Warning
*
*/
// Automatically generated. Make future change here.
definition(
name: "Low Battery Notification",
namespace: "jscgs350",
author: "SmartThings",
description: "Alerts if any battery powered device falls below a specified percent.",
category: "My Apps",
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience%402x.png"
)
preferences {
section("Battery Alarm Level") {
input "alarmAt", "number", title: "Alert when below...", required: true
input "batteryDevices", "capability.battery", title: "Which devices?", multiple: true
}
section( "Notifications" ) {
input "phoneNumber", "phone", title: "Send a text message?", required: false
}
}
def installed() {
log.debug "Installed with settings: ${settings}"
initialize()
}
def updated() {
log.debug "Updated with settings: ${settings}"
unschedule()
initialize()
}
def initialize() {
//schedule the job
schedule("0 0 10am 1,15 * ?", doBatteryCheck)
//run at install too
doBatteryCheck()
}
def doBatteryCheck() {
def belowLevelCntr = 0
def pushMsg = ""
for (batteryDevice in batteryDevices) {
def batteryLevel = batteryDevice.currentValue("battery")
if ( batteryLevel <= settings.alarmAt.toInteger() ) {
pushMsg += "${batteryDevice.name} named ${batteryDevice.label} is at: ${batteryLevel}% \n"
belowLevelCntr++
}
}
if ( belowLevelCntr ){
pushMsg = "You have ${belowLevelCntr} devices below the set alarm level. \n" + pushMsg
} else {
pushMsg = "Battery Check App executed with no devices below alarm level"
}
log.debug(pushMsg)
/* sendPush(pushMsg) */
sendSms(phoneNumber,pushMsg)
}