Update medicine-management-temp-motion.groovy
[smartapps.git] / third-party / BetterLaundryMonitor.groovy
1 /**
2  *  Alert on Power Consumption
3  *
4  *  Copyright 2014 George Sudarkoff
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
17 import groovy.time.*
18
19 definition(
20   name: "Better Laundry Monitor",
21   namespace: "com.sudarkoff",
22   author: "George Sudarkoff",
23   description: "Using a switch with powerMonitor capability, monitor the laundry cycle and alert when it's done.",
24   category: "Green Living",
25   iconUrl: "https://s3.amazonaws.com/smartthings-device-icons/Appliances/appliances8-icn.png",
26   iconX2Url: "https://s3.amazonaws.com/smartthings-device-icons/Appliances/appliances8-icn@2x.png")
27
28
29 preferences {
30   section ("When this device stops drawing power") {
31     input "meter", "capability.powerMeter", multiple: false, required: true
32     input "cycle_start_power_threshold", "number", title: "Start cycle when power consumption goes above (W)", required: true
33     input "cycle_end_power_threshold", "number", title: "Stop cycle when power consumption drops below (W) ...", required: true
34     input "cycle_end_wait", "number", title: "... for at least this long (min)", required: true
35   }
36
37   section ("Send this message") {
38     input "message", "text", title: "Notification message", description: "Laudry is done!", required: true
39   }
40
41   section ("Notification method") {
42     input "sendPushMessage", "bool", title: "Send a push notification?"
43   }
44
45   section ("Additionally", hidden: hideOptionsSection(), hideable: true) {
46     input "phone", "phone", title: "Send a text message to:", required: false
47     input "switches", "capability.switch", title: "Turn on this switch", required:false, multiple:true
48     input "hues", "capability.colorControl", title: "Turn these hue bulbs", required:false, multiple:true
49     input "color", "enum", title: "This color", required: false, multiple:false, options: ["White", "Red","Green","Blue","Yellow","Orange","Purple","Pink"]
50     input "lightLevel", "enum", title: "This light Level", required: false, options: [10,20,30,40,50,60,70,80,90,100]
51     input "speech", "capability.speechSynthesis", title:"Speak message via: ", multiple: true, required: false
52   }
53 }
54
55 def installed() {
56   log.debug "Installed with settings: ${settings}"
57
58   initialize()
59 }
60
61 def updated() {
62   log.debug "Updated with settings: ${settings}"
63
64   unsubscribe()
65   initialize()
66 }
67
68 def initialize() {
69   subscribe(meter, "power", handler)
70 }
71
72 def handler(evt) {
73     def latestPower = meter.currentValue("power")
74     log.trace "Power: ${latestPower}W"
75
76     if (!state.cycleOn && latestPower > cycle_start_power_threshold) {
77         cycleOn(evt)
78     }
79     // If power drops below threshold, wait for a few minutes.
80     else if (state.cycleOn && latestPower <= cycle_end_power_threshold) {
81         runIn(cycle_end_wait * 60, cycleOff)
82     }
83 }
84
85 private cycleOn(evc) {
86     state.cycleOn = true
87     log.trace "Cycle started."
88 }
89
90 private cycleOff(evt) {
91     def latestPower = meter.currentValue("power")
92     log.trace "Power: ${latestPower}W"
93
94     // If power is still below threshold, end cycle.
95     if (state.cycleOn && latestPower <= cycle_end_power_threshold) {
96         state.cycleOn = false
97         log.trace "Cycle ended."
98
99         send(message)
100         lightAlert(evt)
101         speechAlert(evt)
102     }
103 }
104
105 private lightAlert(evt) {
106   def hueColor = 0
107   def saturation = 100
108
109   if (hues) {
110       switch(color) {
111           case "White":
112               hueColor = 52
113               saturation = 19
114               break;
115           case "Daylight":
116               hueColor = 53
117               saturation = 91
118               break;
119           case "Soft White":
120               hueColor = 23
121               saturation = 56
122               break;
123           case "Warm White":
124               hueColor = 20
125               saturation = 80 //83
126               break;
127           case "Blue":
128               hueColor = 70
129               break;
130           case "Green":
131               hueColor = 39
132               break;
133           case "Yellow":
134               hueColor = 25
135               break;
136           case "Orange":
137               hueColor = 10
138               break;
139           case "Purple":
140               hueColor = 75
141               break;
142           case "Pink":
143               hueColor = 83
144               break;
145           case "Red":
146               hueColor = 100
147               break;
148       }
149
150       state.previous = [:]
151
152       hues.each {
153           state.previous[it.id] = [
154               "switch": it.currentValue("switch"),
155               "level" : it.currentValue("level"),
156               "hue": it.currentValue("hue"),
157               "saturation": it.currentValue("saturation")
158           ]
159       }
160
161       log.debug "current values = $state.previous"
162
163       def newValue = [hue: hueColor, saturation: saturation, level: lightLevel as Integer ?: 100]
164       log.debug "new value = $newValue"
165
166       if (switches) {
167           switches*.on()
168       }
169       hues*.setColor(newValue)
170   }
171 }
172
173 private speechAlert(msg) {
174   speech.speak(msg)
175 }
176
177 private send(msg) {
178   if (sendPushMessage) {
179     sendPush(msg)
180   }
181
182   if (phone) {
183     sendSms(phone, msg)
184   }
185
186   log.debug msg
187 }
188
189 private hideOptionsSection() {
190     true
191 }
192
193