Turn on Fans when door opened if....HELP


(Av8rdude) #1

I would like to have an app that will turn on my front porch ceiling fans if the door is opened and the temperature exceeds a certain value (temp to be measured by the door multi). Unfortunately, my development skills are only cut & paste.
My dream version of this app would also have the ability to keep the fans on if the door is immediately closed for a certain amount of time. But leave the fans on continuously if the door is left open.
Another dream addition would be the ability to only turn the fans on if the local windspeed was less than a certain velocity.
Any help is greatly appreciated,
Scott


(Av8rdude) #2

OK…After much study of the great example apps I have a working app. I decided to use a motion sensor instead of the door sensor to turn the fans on/off. This will turn on a switch(es) if there is motion and the temperature is above a certain value:

/**
 *  Porch Fans - On...It is Hot!
 *  Porch Fans on with motion and above a certain temperature
 *
 *  Author: av8rdude
 */

preferences {
	section("Turn on when there's motion..."){
		input "motion1", "capability.motionSensor", title: "Where?"
    }    
	section("Choose a temperature sensor... "){
		input "sensor1", "capability.temperatureMeasurement", title: "Temp Sensor"
    }
    section("When the temperature is above...") {
		input "temperature1", "number", title: "Temp?"
	}
	section("And off when there's been no motion for..."){
		input "minutes1", "number", title: "Minutes?"
	}
	section("Turn on/off switches..."){
		input "switches", "capability.switch", multiple: true
	}
}

def installed()
{
	subscribe(motion1, "motion", motionHandler)
	schedule("0 * * * * ?", "scheduleCheck")
}

def updated()
{
	unsubscribe()
	subscribe(motion1, "motion", motionHandler)
	unschedule()
	schedule("0 * * * * ?", "scheduleCheck")
}

def motionHandler(evt) {
 log.debug "$evt.name: $evt.value"
	if (evt.value == "active") {
		def lastTemp = sensor1.currentTemperature
		if (lastTemp >= temperature1){
			log.debug "turning on lights"
			switches.on()
			state.inactiveAt = null
            }
	} else if (evt.value == "inactive") {
		if (!state.inactiveAt) {
			state.inactiveAt = now()
		}
  	}
}

def scheduleCheck() {
	log.debug "schedule check, ts = ${state.inactiveAt}"
	if (state.inactiveAt) {
		def elapsed = now() - state.inactiveAt
		def threshold = 1000 * 60 * minutes1
		if (elapsed >= threshold) {
			log.debug "turning off lights"
			switches.off()
			state.inactiveAt = null
		}
		else {
			log.debug "${elapsed / 1000} sec since motion stopped"
		}
	}
}