Update hue-minimote.groovy
[smartapps.git] / official / smart-humidifier.groovy
1 /**
2  *  Smart Humidifier
3  *
4  *  Copyright 2014 Sheikh Dawood
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: "Smart Humidifier",
18     namespace: "Sheikhsphere",
19     author: "Sheikh Dawood",
20     description: "Turn on/off humidifier based on relative humidity from a sensor.",
21     category: "Convenience",
22    iconUrl: "https://graph.api.smartthings.com/api/devices/icons/st.Weather.weather12-icn",
23     iconX2Url: "https://graph.api.smartthings.com/api/devices/icons/st.Weather.weather12-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 "humidityHigh", "number", title: "Percentage ?"
33         }
34     section("When the humidity drops below:") {
35                 input "humidityLow", "number", title: "Percentage ?"
36         }
37     section("Control Humidifier:") {
38                 input "switch1", "capability.switch"
39         }
40     section( "Notifications" ) {
41         input "sendPushMessage", "enum", title: "Send a push notification?", metadata:[values:["Yes","No"]], required:false
42         input "phone1", "phone", title: "Send a Text Message?", 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 high point: $humidityHigh"
58     log.trace "set low point: $humidityLow"
59
60         def currentHumidity = Double.parseDouble(evt.value.replace("%", ""))
61         def humidityHigh1 = humidityHigh 
62     def humidityLow1 = humidityLow 
63         def mySwitch = settings.switch1
64
65         if (currentHumidity >= humidityHigh1) {
66                 log.debug "Checking how long the humidity sensor has been reporting >= $humidityHigh1"
67
68                 // Don't send a continuous stream of text messages
69                 def deltaMinutes = 10 
70                 def timeAgo = new Date(now() - (1000 * 60 * deltaMinutes).toLong())
71                 def recentEvents = humiditySensor1.eventsSince(timeAgo)
72                 log.trace "Found ${recentEvents?.size() ?: 0} events in the last $deltaMinutes minutes"
73                 def alreadySentSms1 = recentEvents.count { Double.parseDouble(it.value.replace("%", "")) >= humidityHigh1 } > 1
74
75                 if (alreadySentSms1) {
76                         log.debug "Notification already sent within the last $deltaMinutes minutes"
77
78                 } else {
79                 if (state.lastStatus != "off") {
80                 log.debug "Humidity Rose Above $humidityHigh1:  sending SMS and deactivating $mySwitch"
81                 send("${humiditySensor1.label} sensed high humidity level of ${evt.value}, turning off ${switch1.label}")
82                 switch1?.off()
83                 state.lastStatus = "off"
84             }
85                 }
86         }
87     else if (currentHumidity <= humidityLow1) {
88                 log.debug "Checking how long the humidity sensor has been reporting <= $humidityLow1"
89
90                 // Don't send a continuous stream of text messages
91                 def deltaMinutes = 10 
92                 def timeAgo = new Date(now() - (1000 * 60 * deltaMinutes).toLong())
93                 def recentEvents = humiditySensor1.eventsSince(timeAgo)
94                 log.trace "Found ${recentEvents?.size() ?: 0} events in the last $deltaMinutes minutes"
95                 def alreadySentSms2 = recentEvents.count { Double.parseDouble(it.value.replace("%", "")) <= humidityLow1 } > 1
96
97                 if (alreadySentSms2) {
98                         log.debug "Notification already sent within the last $deltaMinutes minutes"
99
100                 } else {
101                 if (state.lastStatus != "on") {
102                 log.debug "Humidity Dropped Below $humidityLow1:  sending SMS and activating $mySwitch"
103                 send("${humiditySensor1.label} sensed low humidity level of ${evt.value}, turning on ${switch1.label}")
104                 switch1?.on()
105                 state.lastStatus = "on"
106             }
107                 }
108         }
109     else {
110         //log.debug "Humidity remained in threshold:  sending SMS to $phone1 and activating $mySwitch"
111                 //send("${humiditySensor1.label} sensed humidity level of ${evt.value} is within threshold, keeping on ${switch1.label}")
112         //switch1?.on()
113     }
114 }
115
116 private send(msg) {
117     if ( sendPushMessage != "No" ) {
118         log.debug( "sending push message" )
119         sendPush( msg )
120     }
121
122     if ( phone1 ) {
123         log.debug( "sending text message" )
124         sendSms( phone1, msg )
125     }
126
127     log.debug msg
128 }