Virtual Thermostat Code

Hello, just wanted to ask if someone could post the code for the virtual thermostat app - looking to modify it slightly to use a Ecobee thermostat as the temp sensor to trigger events - specifically an appliance zwave switch for AC unit.

This should be it:

/**
 *  Virtual Thermostat
 *
 *  Author: SmartThings
 */
preferences {
	section("Choose a temperature sensor... "){
		input "sensor", "capability.temperatureMeasurement", title: "Sensor"
	}
	section("Select the heater or air conditioner outlet(s)... "){
		input "outlets", "capability.switch", title: "Outlets", multiple: true
	}
	section("Set the desired temperature..."){
		input "setpoint", "decimal", title: "Set Temp"
	}
	section("When there's been movement from (optional, leave blank to not require motion)..."){
		input "motion", "capability.motionSensor", title: "Motion", required: false
	}
	section("Within this number of minutes..."){
		input "minutes", "number", title: "Minutes", required: false
	}
	section("But never go below (or above if A/C) this value with or without motion..."){
		input "emergencySetpoint", "decimal", title: "Emer Temp", required: false
	}
	section("If this is an air conditioner set the mode to 'cool' (defaults to 'heat')..."){
		input "mode", "text", title: "Mode", required: false
	}
}

def installed()
{
	subscribe(sensor, "temperature", temperatureHandler)
	if (motion) {
		subscribe(motion, "motion", motionHandler)
	}
}

def updated()
{
	unsubscribe()
	subscribe(sensor, "temperature", temperatureHandler)
	if (motion) {
		subscribe(motion, "motion", motionHandler)
	}
}

def temperatureHandler(evt)
{
	def isActive = hasBeenRecentMotion()
	if (isActive || emergencySetpoint) {
		evaluate(evt.doubleValue, isActive ? setpoint : emergencySetpoint)
	}
	else {
		outlets.off()
	}
}

def motionHandler(evt)
{
	if (evt.value == "active") {
		def lastTemp = sensor.latestValue("temperature")
		if (lastTemp != null) {
			evaluate(lastTemp, setpoint)
		}
	} else if (evt.value == "inactive") {
		def isActive = hasBeenRecentMotion()
		log.debug "INACTIVE($isActive)"
		if (isActive || emergencySetpoint) {
			def lastTemp = sensor.latestValue("temperature")
			if (lastTemp != null) {
				evaluate(lastTemp, isActive ? setpoint : emergencySetpoint)
			}
		}
		else {
			outlets.off()
		}
	}
}

private evaluate(currentTemp, desiredTemp)
{
	log.debug "EVALUATE($currentTemp, $desiredTemp)"
	def threshold = 1.0
	if (mode == "cool") {
		// air conditioner
		if (currentTemp - desiredTemp >= threshold) {
			outlets.on()
		}
		else if (desiredTemp - currentTemp >= threshold) {
			outlets.off()
		}
	}
	else {
		// heater
		if (desiredTemp - currentTemp >= threshold) {
			outlets.on()
		}
		else if (currentTemp - desiredTemp >= threshold) {
			outlets.off()
		}
	}
}

private hasBeenRecentMotion()
{
	def isActive = false
	if (motion && minutes) {
		def deltaMinutes = minutes as Long
		if (deltaMinutes) {
			def motionEvents = motion.eventsSince(new Date(now() - (60000 * deltaMinutes)))
			log.trace "Found ${motionEvents?.size() ?: 0} events in the last $deltaMinutes minutes"
			if (motionEvents.find { it.value == "active" }) {
				isActive = true
			}
		}
	}
	else {
		isActive = true
	}
	isActive
}

Awesome and thank you for the info.

@tyler I have a Monoprice motion sensor with temperature and want to be able to take the temperatures reading and turn on a z wave switch? Can i do that with the Virtual Thermostat App?? Here is what I would like to be able to do say my temperature reaches 78 at that point I would like to be able to turn a switch that control my ac and if it cool down to 70 turn it back off? Is it possible to do with this app without a lot of changing it?

If I’m understanding correctly Virtual Thermostat should do exactly as you want. Open the app up, click the plus at the bottom them scroll all the way to the right for “More”. Select “Green Living” from the bottom and it will be in there. No changes to the code of the SmartApp should be required.

Thanks

Guys… I’m kinda lost with this however very simple code… What does the variable “desiredTemp” represent? I mean, it appears out of nowhere in the evaluate() loop and its value is the same as the “setpoint” value from settings, however… nowhere there’s something like def desiredTemp = setpoint declaration! I’m really confused here…

Thanks in advance,
Elfège.