forked from bravenel/SmartThings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Power Alert
49 lines (41 loc) · 1.53 KB
/
Power Alert
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
definition(
name: "Power Alert",
namespace: "public",
author: "SmartThings and B Ravenel",
description: "Notify if power exceeds a threshold",
category: "Convenience",
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png",
iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png")
preferences {
section ("Power Alert") {
input(name: "meter", type: "capability.powerMeter", title: "When This Power Meter...", required: true, multiple: false, description: null)
input(name: "threshold", type: "number", title: "Reports Above...", required: true, description: "in either watts or kw.")
}
section("Text me at (optional, sends a push notification if not specified)...") {
input "phone", "phone", title: "Phone number?", required: false
}
}
def installed() {
initialize()
}
def updated() {
unsubscribe()
initialize()
}
def initialize() {
subscribe(meter, "power", meterHandler)
state.notNotified = true
}
// notifies on first incursion above threshold, but not again until power falls below threshold and then rises again
def meterHandler(evt) {
def meterValue = evt.value as double
def thresholdValue = threshold as int
if(meterValue > thresholdValue) {
if(state.notNotified) {
def msg = "Power on $evt.displayName is $meterValue"
if(phone) sendSms(phone, msg) else sendNotification(msg)
state.notNotified = false
}
} else state.notNotified = true
}