Update README.md
[smartapps.git] / official / humidity-alert.groovy
1 /**
2  *  Its too humid!
3  *
4  *  Copyright 2014 Brian Critchlow
5  *  Based on Its too cold code by 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:
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: "Humidity Alert!",
18     namespace: "docwisdom",
19     author: "Brian Critchlow",
20     description: "Notify me when the humidity rises above or falls below the given threshold. It will turn on a switch when it rises above the first threshold and off when it falls below the second threshold.",
21     category: "Convenience",
22     iconUrl: "https://graph.api.smartthings.com/api/devices/icons/st.Weather.weather9-icn",
23     iconX2Url: "https://graph.api.smartthings.com/api/devices/icons/st.Weather.weather9-icn?displaySize=2x"
24 )
25
26
27 preferences {
28         section("Monitor the humidity of:") {
29                 input "humiditySensor1", "capability.relativeHumidityMeasurement"
30         }
31         section("When the humidity rises above:") {
32                 input "humidity1", "number", title: "Percentage ?"
33         }
34     section("When the humidity falls below:") {
35                 input "humidity2", "number", title: "Percentage ?"
36         }
37     section( "Notifications" ) {
38         input "sendPushMessage", "enum", title: "Send a push notification?", metadata:[values:["Yes","No"]], required:false
39         input "phone1", "phone", title: "Send a Text Message?", required: false
40     }
41         section("Control this switch:") {
42                 input "switch1", "capability.switch", required: false
43         }
44 }
45
46 def installed() {
47         subscribe(humiditySensor1, "humidity", humidityHandler)
48 }
49
50 def updated() {
51         unsubscribe()
52         subscribe(humiditySensor1, "humidity", humidityHandler)
53 }
54
55 def humidityHandler(evt) {
56         log.trace "humidity: ${evt.value}"
57     log.trace "set point: ${humidity1}"
58
59         def currentHumidity = Double.parseDouble(evt.value.replace("%", ""))
60         def tooHumid = humidity1 
61     def notHumidEnough = humidity2
62         def mySwitch = settings.switch1
63         def deltaMinutes = 10 
64     
65     def timeAgo = new Date(now() - (1000 * 60 * deltaMinutes).toLong())
66         def recentEvents = humiditySensor1.eventsSince(timeAgo)
67         log.trace "Found ${recentEvents?.size() ?: 0} events in the last ${deltaMinutes} minutes"
68         def alreadySentSms = recentEvents.count { Double.parseDouble(it.value.replace("%", "")) >= tooHumid } > 1 || recentEvents.count { Double.parseDouble(it.value.replace("%", "")) <= notHumidEnough } > 1
69     
70         if (currentHumidity >= tooHumid) {
71                 log.debug "Checking how long the humidity sensor has been reporting >= ${tooHumid}"
72
73                 // Don't send a continuous stream of text messages
74                 
75
76
77                 if (alreadySentSms) {
78                         log.debug "Notification already sent within the last ${deltaMinutes} minutes"
79                         
80                 } else {
81                         log.debug "Humidity Rose Above ${tooHumid}:  sending SMS and activating ${mySwitch}"
82                         send("${humiditySensor1.label} sensed high humidity level of ${evt.value}")
83                         switch1?.on()
84                 }
85         }
86
87     if (currentHumidity <= notHumidEnough) {
88                 log.debug "Checking how long the humidity sensor has been reporting <= ${notHumidEnough}"
89
90                 if (alreadySentSms) {
91                         log.debug "Notification already sent within the last ${deltaMinutes} minutes"
92                         
93                 } else {
94                         log.debug "Humidity Fell Below ${notHumidEnough}:  sending SMS and activating ${mySwitch}"
95                         send("${humiditySensor1.label} sensed high humidity level of ${evt.value}")
96                         switch1?.off()
97                 }
98         }
99 }
100
101 private send(msg) {
102     if ( sendPushMessage != "No" ) {
103         log.debug( "sending push message" )
104         sendPush( msg )
105     }
106
107     if ( phone1 ) {
108         log.debug( "sending text message" )
109         sendSms( phone1, msg )
110     }
111
112     log.debug msg
113 }