4 * Copyright 2014 SmartThings
6 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
7 * in compliance with the License. You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
12 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
13 * for the specific language governing permissions and limitations under the License.
17 name: "Energy Alerts",
18 namespace: "smartthings",
19 author: "SmartThings",
20 description: "Get notified if you're using too much energy",
21 category: "Green Living",
22 iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/text.png",
23 iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/text@2x.png",
24 iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Meta/text@2x.png"
29 input(name: "meter", type: "capability.powerMeter", title: "When This Power Meter...", required: true, multiple: false, description: null)
30 input(name: "aboveThreshold", type: "number", title: "Reports Above...", required: true, description: "in either watts or kw.")
31 input(name: "belowThreshold", type: "number", title: "Or Reports Below...", required: true, description: "in either watts or kw.")
34 input("recipients", "contact", title: "Send notifications to") {
35 input(name: "sms", type: "phone", title: "Send A Text To", description: null, required: false)
36 input(name: "pushNotification", type: "bool", title: "Send a push notification", description: null, defaultValue: true)
42 log.debug "Installed with settings: ${settings}"
47 log.debug "Updated with settings: ${settings}"
53 subscribe(meter, "power", meterHandler)
56 def meterHandler(evt) {
58 def meterValue = evt.value as double
60 if (!atomicState.lastValue) {
61 atomicState.lastValue = meterValue
64 def lastValue = atomicState.lastValue as double
65 atomicState.lastValue = meterValue
67 def dUnit = evt.unit ?: "Watts"
69 def aboveThresholdValue = aboveThreshold as int
70 if (meterValue > aboveThresholdValue) {
71 if (lastValue < aboveThresholdValue) { // only send notifications when crossing the threshold
72 def msg = "${meter} reported ${evt.value} ${dUnit} which is above your threshold of ${aboveThreshold}."
75 // log.debug "not sending notification for ${evt.description} because the threshold (${aboveThreshold}) has already been crossed"
80 def belowThresholdValue = belowThreshold as int
81 if (meterValue < belowThresholdValue) {
82 if (lastValue > belowThresholdValue) { // only send notifications when crossing the threshold
83 def msg = "${meter} reported ${evt.value} ${dUnit} which is below your threshold of ${belowThreshold}."
86 // log.debug "not sending notification for ${evt.description} because the threshold (${belowThreshold}) has already been crossed"
91 def sendMessage(msg) {
92 if (location.contactBookEnabled) {
93 sendNotificationToContacts(msg, recipients)
99 if (pushNotification) {