-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVirtualThermostat.groovy
142 lines (135 loc) · 3.88 KB
/
VirtualThermostat.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* Copyright 2015 SmartThings
*
* 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:
*
* 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
* 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.
*
* Virtual Thermostat
*
* Author: SmartThings
*/
definition(
name: "Virtual Thermostat",
namespace: "castlecole",
author: "SmartThings",
description: "Control a space heater or window air conditioner in conjunction with any temperature sensor, like a SmartSense Multi.",
category: "Green Living",
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/temp_thermo-switch.png",
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/[email protected]"
)
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("Select 'heat' for a heater and 'cool' for an air conditioner..."){
input "mode", "enum", title: "Heating or cooling?", options: ["heat","cool"]
}
}
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.currentTemperature
if (lastTemp != null) {
evaluate(lastTemp, setpoint)
}
} else if (evt.value == "inactive") {
def isActive = hasBeenRecentMotion()
log.debug "INACTIVE($isActive)"
if (isActive || emergencySetpoint) {
def lastTemp = sensor.currentTemperature
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
}