Need Help: When Humidity Level is reached, Turn on Fan

Hey all…

Okay…I gave it a shot (and got close…I think), but I’m in over my head on a custom SmartApp that I need to control a bathroom fan in a rental house. See, I’d like to just have the fan turn on whenever the humidity is too high. This way I don’t have to remind renters to “be sure and run the fan when you shower” and I know the property is protected. I’d also like the fan to shut back off once the humidity falls back below the “threshold” number.

Here’s how far I got. The value comparisons just don’t seem to work and I’m stumped:

/**
 *  Humidity Driven Fan
 *
 *  Author: Ross Peterson
 *  Date: 2013-09-14
 */

preferences {
	section("Monitor the humidity..."){
		input "humiditySensor1", "capability.relativeHumidityMeasurement", title: "Humidity Sensor?", required: true
    }    
	section("Choose a Switch that controls a Fan..."){
		input "fanSwitch1", "capability.switch", title: "Fan Location?", required: true
    }
    section("Turn fan on when the humidity is above:") {
		input "humidityUP", "number", title: "Humidity Upper Threshold (%)?"
	}
}

def installed() {
    subscribe(humiditySensor1, "humidity", humidityHandler)
	log.debug "Installed with settings: ${settings}"
}

def updated() {
	unsubscribe()
    subscribe(humiditySensor1, "humidity", humidityHandler)
	log.debug "Updated with settings: ${settings}"
}

def humidityHandler(evt) {
	log.debug "Humidity: $evt.value, $evt.unit, $evt.name, $evt"

	//def humNum = evt.value.replace("%", "")
    def humNum = Double.parseDouble(evt.value.replace("%", ""))
	def tooHumidNum = humidityUP
    double tooHumid = tooHumidNum
    
    def mySwitch = settings.fanSwitch1

log.debug "Current Humidity: $humNum, Humidity Setting: $tooHumid"
    
    if ($humNum >= $tooHumid) { // she's too Humid, do something
        
        log.debug "Humidity is Greater than Setting"
        if ( fanSwitch1.currentValue("switch") == "off" ) {
        // Turn on if the switch is off
        fanSwitch1.on()   
        }
    } 
       
    if ($humNum < $tooHumid) { // Humidity level is okay...fan should be off
        
        log.debug "Humidity is Less than Setting"
        if ( fanSwitch1.currentValue("switch") == "on" ) {
        // Turn off if the switch is on
        fanSwitch1.off()   
        }
    }   
}

Anyone up for helping a brother out? Any advice would be greatly appreciated.

Ran it and it worked decently for me making these two changes:

  1. Don’t use $ for variable names outside of strings. If it’s in a log.info("${variable} this is fine") - but an assignment or condition: $var1 = “foo” not so much.
  2. I wouldn’t bother putting a check to see the currentValue. If you click a switch off that’s already off…it stays off.

Thank you @ImBrian! I was hopeful that I was pretty close to having a working solution. I fixed up the code and just ran a real-world test and everything worked perfectly! Thanks a ton for jumping in and helping me. :slight_smile:

@rosspeterson Do you care to share your final code? I’d like to do this same exact thing in my house.

Hey Jason…below is what I used. Seems to be working really well for the past 12+ months. The only drawback I’ve found is that sometimes the humidity sensor will turn off the fan prematurely at the beginning of a shower. I’m pretty sure that this is what’s happening:

  1. Get ready to take a show and turn on the fan

  2. Depending on your specific humidity sensor, the cycle duration between readings may be small enough that (even though you are showering and creating humidity) the actual humidity value hasn’t tripped the threshold yet.

  3. App turns off the Fan until the next reading comes in

  4. App turns on the Fan eventually, once the humidity value is reached.

Its more a nuisance than anything…but a nice polish here would be to find a way that if the fan was turned on manually, it overrides any on/off from the app for say 10-15 minutes, making sure that the fan is actually running for that entire time…or you can just try to remember to re-turn on the fan during the shower (assuming you can reach the switch) like I do. :slight_smile:

Anyway…without further ado…here’s the code I’m using:



/**
 *  Humidity Driven Fan
 *
 *  Author: ross.peterson@gmail.com
 *  Date: 2013-09-14
 */


// Automatically generated. Make future change here.
definition(
    name: "Humidity Driven Fan",
    namespace: "",
    author: "ross.peterson@gmail.com",
    description: "When the humidity level goes above a certain value, turn on a switched fan. When that value drops below a separate threshold, turn off the fan.",
    category: "Convenience",
    iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
    iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience%402x.png")

preferences {
	section("Monitor the humidity..."){
		input "humiditySensor1", "capability.relativeHumidityMeasurement", title: "Humidity Sensor?", required: true
    }    
	section("Choose a Switch that controls a Fan..."){
		input "fanSwitch1", "capability.switch", title: "Fan Location?", required: true
    }
    section("Turn fan on when the humidity is above:") {
		input "humidityUP", "number", title: "Humidity Upper Threshold (%)?"
	}
}

def installed() {
    subscribe(humiditySensor1, "humidity", humidityHandler)
	log.debug "Installed with settings: ${settings}"
}

def updated() {
	unsubscribe()
    subscribe(humiditySensor1, "humidity", humidityHandler)
	log.debug "Updated with settings: ${settings}"
}

def humidityHandler(evt) {
	log.debug "Humidity: $evt.value, $evt.unit, $evt.name, $evt"

	//def humNum = evt.value.replace("%", "")
    def humNum = Double.parseDouble(evt.value.replace("%", ""))
	def tooHumidNum = humidityUP
    double tooHumid = tooHumidNum
    
    def mySwitch = settings.fanSwitch1

log.debug "Current Humidity: $humNum, Humidity Setting: $tooHumid"
    
    if (humNum >= tooHumid) { // she's too Humid, do something
        
        log.debug "Humidity is Greater than Setting"
        if ( fanSwitch1.currentValue("switch") == "off" ) {
        // Turn on if the switch is off
        fanSwitch1.on()   
        }
    } else {
       
    if (humNum < tooHumid) { // Humidity level is okay...fan should be off
        
        log.debug "Humidity is Less than Setting"
        if ( fanSwitch1.currentValue("switch") == "on" ) {
        // Turn off if the switch is on
        fanSwitch1.off()   
        }
    }    
  }
}

2 Likes