Guest User

Untitled

a guest
Nov 30th, 2015
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 7.70 KB | None | 0 0
  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.  
  29.     section("Monitor the temperature of which outdoor sensor?") {
  30.         input "temperatureSensor1", "capability.temperatureMeasurement"
  31.     }
  32.     section("Monitor the humidity of which indoor sensor?") {
  33.         input "humiditySensor1", "capability.relativeHumidityMeasurement"
  34.     }
  35.     section("Control Humidifier:") {
  36.         input "switch1", "capability.switch"
  37.     }
  38.     section( "Notifications" ) {
  39.         input "sendPushMessage", "enum", title: "Send a push notification?", metadata:[values:["Yes","No"]], required:false
  40.         input "phone1", "phone", title: "Send a Text Message?", required: false
  41.     }
  42. }
  43.  
  44. def installed() {
  45.     subscribe(humiditySensor1, "humidity", humidityHandler)
  46.     subscribe(temperatureSensor1, "temperature", temperatureHandler)
  47. }
  48.  
  49. def updated() {
  50.     unsubscribe()
  51.     subscribe(humiditySensor1, "humidity", humidityHandler)
  52.     subscribe(temperatureSensor1, "temperature", temperatureHandler)
  53. }
  54.  
  55. /**def temperatureHandler(val) {
  56.         log.trace "temperature: $evt.value, $evt"
  57.         def currentTemp = val.doubleValue
  58.     }
  59. **/
  60. def humidityHandler(evt) { 
  61.     def currentHumidity = Double.parseDouble(evt.value.replace("%", ""))
  62.     def currentTemp = 0.0 as BigDecimal
  63.     currentTemp = temperatureSensor1.currentState("temperature")
  64.     log.trace "Reporting current humidity of $currentHumidity"
  65.     log.trace "Reporting current outdoor temp of $currentTemp"
  66.     def mySwitch = settings.switch1
  67.     if (currentTemp >= 20){
  68.         def optimalInsideHumidity = 40
  69.         if (currentHumidity > optimalInsideHumidity){
  70.             log.trace "Inside currentTemp >= 20 IF statement"
  71.             log.trace "Reporting current humidity of $currentHumidity"
  72.             log.trace "Reporting current outdoor temp of $currentTemp and optimal humidity target of $optimalInsideHumidity, so switching humidifier off"
  73.             switch1?.off()
  74.             state.lastStatus = "off"
  75.         }
  76.         else{
  77.             log.trace "Inside currentTemp >= 20 ELSE statement"
  78.             log.trace "Reporting current humidity of $currentHumidity"
  79.             log.trace "Reporting current outdoor temp of $currentTemp and optimal humidity target of $optimalInsideHumidity, so switching humidifier on"
  80.             switch1?.on()
  81.             state.lastStatus = "on"
  82.         }
  83.     }
  84.    
  85.     else if (currentTemp >= 10 && currentTemp < 20) {
  86.         def optimalInsideHumidity = 35
  87.         if (currentHumidity > optimalInsideHumidity){
  88.             log.trace "Inside currentTemp >= 10 and < 20 IF statement"
  89.             log.trace "Reporting current humidity of $currentHumidity"
  90.             log.trace "Reporting current outdoor temp of $currentTemp and optimal humidity target of $optimalInsideHumidity, so switching humidifier off"
  91.             switch1?.off()
  92.             state.lastStatus = "off"
  93.         }
  94.         else{
  95.             log.trace "Inside currentTemp >= 10 and < 20 ELSE statement"
  96.             log.trace "Reporting current humidity of $currentHumidity"
  97.             log.trace "Reporting current outdoor temp of $currentTemp and optimal humidity target of $optimalInsideHumidity, so switching humidifier on"
  98.             switch1?.on()
  99.             state.lastStatus = "on"
  100.         }
  101.     }
  102.     else if (currentTemp >= 0 && currentTemp < 10) {
  103.         def optimalInsideHumidity = 30
  104.         if (currentHumidity > optimalInsideHumidity){
  105.             log.trace "Inside currentTemp >= 0 and < 10 IF statement"
  106.             log.trace "Reporting current humidity of $currentHumidity"
  107.             log.trace "Reporting current outdoor temp of $currentTemp and optimal humidity target of $optimalInsideHumidity, so switching humidifier off"
  108.             switch1?.off()
  109.             state.lastStatus = "off"
  110.         }
  111.         else{
  112.             log.trace "Inside currentTemp >= 10 and < 20 ELSE statement"
  113.             log.trace "Reporting current humidity of $currentHumidity"
  114.             log.trace "Reporting current outdoor temp of $currentTemp and optimal humidity target of $optimalInsideHumidity, so switching humidifier on"
  115.             switch1?.on()
  116.             state.lastStatus = "on"
  117.         }
  118.     }
  119.        
  120.  
  121.        
  122.     }
  123.    
  124. /** if (currentHumidity >= humidityHigh1) {
  125.         log.debug "Checking how long the humidity sensor has been reporting >= $humidityHigh1"
  126.  
  127.         // Don't send a continuous stream of text messages
  128.         def deltaMinutes = 10
  129.         def timeAgo = new Date(now() - (1000 * 60 * deltaMinutes).toLong())
  130.         def recentEvents = humiditySensor1.eventsSince(timeAgo)
  131.         log.trace "Found ${recentEvents?.size() ?: 0} events in the last $deltaMinutes minutes"
  132.         def alreadySentSms1 = recentEvents.count { Double.parseDouble(it.value.replace("%", "")) >= humidityHigh1 } > 1
  133.  
  134.         if (alreadySentSms1) {
  135.             log.debug "Notification already sent within the last $deltaMinutes minutes"
  136.  
  137.         } else {
  138.             if (state.lastStatus != "off") {
  139.                 log.debug "Humidity Rose Above $humidityHigh1:  sending SMS to $phone1 and deactivating $mySwitch"
  140.                 send("${humiditySensor1.label} sensed high humidity level of ${evt.value}, turning off ${switch1.label}")
  141.                 switch1?.off()
  142.                 state.lastStatus = "off"
  143.             }
  144.         }
  145.     }
  146.     else if (currentHumidity <= humidityLow1) {
  147.         log.debug "Checking how long the humidity sensor has been reporting <= $humidityLow1"
  148.  
  149.         // Don't send a continuous stream of text messages
  150.         def deltaMinutes = 10
  151.         def timeAgo = new Date(now() - (1000 * 60 * deltaMinutes).toLong())
  152.         def recentEvents = humiditySensor1.eventsSince(timeAgo)
  153.         log.trace "Found ${recentEvents?.size() ?: 0} events in the last $deltaMinutes minutes"
  154.         def alreadySentSms2 = recentEvents.count { Double.parseDouble(it.value.replace("%", "")) <= humidityLow1 } > 1
  155.  
  156.         if (alreadySentSms2) {
  157.             log.debug "Notification already sent within the last $deltaMinutes minutes"
  158.  
  159.         } else {
  160.             if (state.lastStatus != "on") {
  161.                 log.debug "Humidity Dropped Below $humidityLow1:  sending SMS to $phone1 and activating $mySwitch"
  162.                 send("${humiditySensor1.label} sensed low humidity level of ${evt.value}, turning on ${switch1.label}")
  163.                 switch1?.on()
  164.                 state.lastStatus = "on"
  165.             }
  166.         }
  167.     }
  168.     else {
  169.         //log.debug "Humidity remained in threshold:  sending SMS to $phone1 and activating $mySwitch"
  170.         //send("${humiditySensor1.label} sensed humidity level of ${evt.value} is within threshold, keeping on ${switch1.label}")
  171.         //switch1?.on()
  172.     }
  173. } //end humidityhandler
  174.  
  175. private send(msg) {
  176.     if ( sendPushMessage != "No" ) {
  177.         log.debug( "sending push message" )
  178.         sendPush( msg )
  179.     }
  180.  
  181.     if ( phone1 ) {
  182.         log.debug( "sending text message" )
  183.         sendSms( phone1, msg )
  184.     }
  185.  
  186.     log.debug msg
  187. }
  188.  
  189. **/
Advertisement
Add Comment
Please, Sign In to add comment