Adding more official apps gotten from https://github.com/SmartThingsCommunity/Code
[smartapps.git] / official / energy-alerts.groovy
1 /**
2  *  Energy Saver
3  *
4  *  Copyright 2014 SmartThings
5  *
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:
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
14  *
15  */
16 definition(
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"
25 )
26
27 preferences {
28         section {
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.")
32         }
33     section {
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)
37         }
38     }
39 }
40
41 def installed() {
42         log.debug "Installed with settings: ${settings}"
43         initialize()
44 }
45
46 def updated() {
47         log.debug "Updated with settings: ${settings}"
48         unsubscribe()
49         initialize()
50 }
51
52 def initialize() {
53         subscribe(meter, "power", meterHandler)
54 }
55
56 def meterHandler(evt) {
57
58     def meterValue = evt.value as double
59
60     if (!atomicState.lastValue) {
61         atomicState.lastValue = meterValue
62     }
63
64     def lastValue = atomicState.lastValue as double
65     atomicState.lastValue = meterValue
66
67     def dUnit = evt.unit ?: "Watts"
68
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}."
73             sendMessage(msg)
74         } else {
75 //              log.debug "not sending notification for ${evt.description} because the threshold (${aboveThreshold}) has already been crossed"
76         }
77     }
78
79
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}."
84             sendMessage(msg)
85         } else {
86 //              log.debug "not sending notification for ${evt.description} because the threshold (${belowThreshold}) has already been crossed"
87         }
88     }
89 }
90
91 def sendMessage(msg) {
92     if (location.contactBookEnabled) {
93         sendNotificationToContacts(msg, recipients)
94     }
95     else {
96         if (sms) {
97             sendSms(sms, msg)
98         }
99         if (pushNotification) {
100             sendPush(msg)
101         }
102     }
103 }