forked from sudarkoff/SmartThings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBatteryLow.groovy
77 lines (67 loc) · 1.94 KB
/
BatteryLow.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
/**
* Low Battery Notification
*
*/
definition(
name: "Battery Low",
namespace: "com.sudarkoff",
author: "George Sudarkoff",
description: "Notify when battery charge drops below the specified level.",
category: "Convenience",
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 ("When battery change in these devices") {
input "devices", "capability.battery", title:"Battery Operated Devices", multiple: true
}
section ("Drops below this level") {
input "level", "number", title:"Battery Level (%)"
}
section ("Notify") {
input "sendPushMessage", "bool", title: "Send a push notification?", required: false
input "phone", "phone", title: "Send a Text Message?", required: false
}
}
def installed() {
initialize()
}
def updated() {
unsubscribe()
initialize()
}
def initialize() {
if (level < 5 || level > 90) {
sendPush("Battery level should be between 5 and 90 percent")
return false
}
subscribe(devices, "battery", batteryHandler)
state.lowBattNoticeSent = [:]
updateBatteryStatus()
}
def batteryHandler(evt) {
updateBatteryStatus()
}
private send(message) {
if (phone) {
sendSms(phone, message)
}
if (sendPushMessage) {
sendPush(message)
}
}
private updateBatteryStatus() {
for (device in devices) {
if (device.currentBattery < level) {
if (!state.lowBattNoticeSent.containsKey(device.id)) {
send("${device.displayName}'s battery is at ${device.currentBattery}%.")
}
state.lowBattNoticeSent[(device.id)] = true
}
else {
if (state.lowBattNoticeSent.containsKey(device.id)) {
state.lowBattNoticeSent.remove(device.id)
}
}
}
}