Update notify-me-with-hue.groovy
[smartapps.git] / official / energy-saver.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 Saver",
18     namespace: "smartthings",
19     author: "SmartThings",
20                 description: "Turn things off if you're using too much energy",
21     category: "Green Living",
22     iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/light_outlet.png",
23     iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/light_outlet@2x.png",
24     iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Meta/light_outlet@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: "threshold", type: "number", title: "Reports Above...", required: true, description: "in either watts or kw.")
31         }
32     section {
33         input(name: "switches", type: "capability.switch", title: "Turn Off These Switches", required: true, multiple: true, description: null)
34     }
35 }
36
37 def installed() {
38         log.debug "Installed with settings: ${settings}"
39         initialize()
40 }
41
42 def updated() {
43         log.debug "Updated with settings: ${settings}"
44         unsubscribe()
45         initialize()
46 }
47
48 def initialize() {
49         subscribe(meter, "power", meterHandler)
50 }
51
52 def meterHandler(evt) {
53     def meterValue = evt.value as double
54     def thresholdValue = threshold as int
55     if (meterValue > thresholdValue) {
56             log.debug "${meter} reported energy consumption above ${threshold}. Turning of switches."
57         switches.off()
58     }
59 }