Compare commits

...

1 Commits

Author SHA1 Message Date
박춘영
fd2a45da05 MSA-948: 테스트용으로작성한것 취소요청 2016-03-09 22:50:20 -06:00

View File

@@ -1,143 +1,142 @@
/** /**
* Copyright 2015 SmartThings * Copyright 2015 SmartThings
* *
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at: * in compliance with the License. You may obtain a copy of the License at:
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
* for the specific language governing permissions and limitations under the License. * for the specific language governing permissions and limitations under the License.
* *
* Virtual Thermostat * Virtual Thermostat
* *
* Author: SmartThings * Author: SmartThings
*/ */
definition( definition(
name: "Virtual Thermostat", name: "Virtual Thermostat",
namespace: "smartthings", namespace: "smartthings",
author: "SmartThings", author: "SmartThings",
description: "Control a space heater or window air conditioner in conjunction with any temperature sensor, like a SmartSense Multi.", description: "Control a space heater or window air conditioner in conjunction with any temperature sensor, like a SmartSense Multi.",
category: "Green Living", category: "Green Living",
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/temp_thermo-switch.png", iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/temp_thermo-switch.png",
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/temp_thermo-switch@2x.png" iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/temp_thermo-switch@2x.png"
) )
preferences { preferences {
section("Choose a temperature sensor... "){ section("Choose a temperature sensor... "){
input "sensor", "capability.temperatureMeasurement", title: "Sensor" input "sensor", "capability.temperatureMeasurement", title: "Sensor"
} }
section("Select the heater or air conditioner outlet(s)... "){ section("Select the heater or air conditioner outlet(s)... "){
input "outlets", "capability.switch", title: "Outlets", multiple: true input "outlets", "capability.switch", title: "Outlets", multiple: true
} }
section("Set the desired temperature..."){ section("Set the desired temperature..."){
input "setpoint", "decimal", title: "Set Temp" input "setpoint", "decimal", title: "Set Temp"
} }
section("When there's been movement from (optional, leave blank to not require motion)..."){ section("When there's been movement from (optional, leave blank to not require motion)..."){
input "motion", "capability.motionSensor", title: "Motion", required: false input "motion", "capability.motionSensor", title: "Motion", required: false
} }
section("Within this number of minutes..."){ section("Within this number of minutes..."){
input "minutes", "number", title: "Minutes", required: false input "minutes", "number", title: "Minutes", required: false
} }
section("But never go below (or above if A/C) this value with or without motion..."){ section("But never go below (or above if A/C) this value with or without motion..."){
input "emergencySetpoint", "decimal", title: "Emer Temp", required: false input "emergencySetpoint", "decimal", title: "Emer Temp", required: false
} }
section("Select 'heat' for a heater and 'cool' for an air conditioner..."){ section("Select 'heat' for a heater and 'cool' for an air conditioner..."){
input "mode", "enum", title: "Heating or cooling?", options: ["heat","cool"] input "mode", "enum", title: "Heating or cooling?", options: ["heat","cool"]
} }
} }
def installed() def installed()
{ {
subscribe(sensor, "temperature", temperatureHandler) subscribe(sensor, "temperature", temperatureHandler)
if (motion) { if (motion) {
subscribe(motion, "motion", motionHandler) subscribe(motion, "motion", motionHandler)
} }
} }
def updated() def updated()
{ {
unsubscribe() unsubscribe()
subscribe(sensor, "temperature", temperatureHandler) subscribe(sensor, "temperature", temperatureHandler)
if (motion) { if (motion) {
subscribe(motion, "motion", motionHandler) subscribe(motion, "motion", motionHandler)
} }
} }
def temperatureHandler(evt) def temperatureHandler(evt)
{ {
def isActive = hasBeenRecentMotion() def isActive = hasBeenRecentMotion()
if (isActive || emergencySetpoint) { if (isActive || emergencySetpoint) {
evaluate(evt.doubleValue, isActive ? setpoint : emergencySetpoint) evaluate(evt.doubleValue, isActive ? setpoint : emergencySetpoint)
} }
else { else {
outlets.off() outlets.off()
} }
} }
def motionHandler(evt) def motionHandler(evt)
{ {
if (evt.value == "active") { if (evt.value == "active") {
def lastTemp = sensor.currentTemperature def lastTemp = sensor.currentTemperature
if (lastTemp != null) { if (lastTemp != null) {
evaluate(lastTemp, setpoint) evaluate(lastTemp, setpoint)
} }
} else if (evt.value == "inactive") { } else if (evt.value == "inactive") {
def isActive = hasBeenRecentMotion() def isActive = hasBeenRecentMotion()
log.debug "INACTIVE($isActive)" log.debug "INACTIVE($isActive)"
if (isActive || emergencySetpoint) { if (isActive || emergencySetpoint) {
def lastTemp = sensor.currentTemperature def lastTemp = sensor.currentTemperature
if (lastTemp != null) { if (lastTemp != null) {
evaluate(lastTemp, isActive ? setpoint : emergencySetpoint) evaluate(lastTemp, isActive ? setpoint : emergencySetpoint)
} }
} }
else { else {
outlets.off() outlets.off()
} }
} }
} }
private evaluate(currentTemp, desiredTemp) private evaluate(currentTemp, desiredTemp)
{ {
log.debug "EVALUATE($currentTemp, $desiredTemp)" log.debug "EVALUATE($currentTemp, $desiredTemp)"
def threshold = 1.0 def threshold = 1.0
if (mode == "cool") { if (mode == "cool") {
// air conditioner // air conditioner
if (currentTemp - desiredTemp >= threshold) { if (currentTemp - desiredTemp >= threshold) {
outlets.on() outlets.on()
} }
else if (desiredTemp - currentTemp >= threshold) { else if (desiredTemp - currentTemp >= threshold) {
outlets.off() outlets.off()
} }
} }
else { else {
// heater // heater
if (desiredTemp - currentTemp >= threshold) { if (desiredTemp - currentTemp >= threshold) {
outlets.on() outlets.on()
} }
else if (currentTemp - desiredTemp >= threshold) { else if (currentTemp - desiredTemp >= threshold) {
outlets.off() outlets.off()
} }
} }
} }
private hasBeenRecentMotion() private hasBeenRecentMotion()
{ {
def isActive = false def isActive = false
if (motion && minutes) { if (motion && minutes) {
def deltaMinutes = minutes as Long def deltaMinutes = minutes as Long
if (deltaMinutes) { if (deltaMinutes) {
def motionEvents = motion.eventsSince(new Date(now() - (60000 * deltaMinutes))) def motionEvents = motion.eventsSince(new Date(now() - (60000 * deltaMinutes)))
log.trace "Found ${motionEvents?.size() ?: 0} events in the last $deltaMinutes minutes" log.trace "Found ${motionEvents?.size() ?: 0} events in the last $deltaMinutes minutes"
if (motionEvents.find { it.value == "active" }) { if (motionEvents.find { it.value == "active" }) {
isActive = true isActive = true
} }
} }
} }
else { else {
isActive = true isActive = true
} }
isActive isActive
} }