-
Notifications
You must be signed in to change notification settings - Fork 11
/
hvac-auto-off.smartapp.groovy
92 lines (79 loc) · 2.41 KB
/
hvac-auto-off.smartapp.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
/**
* HVAC Auto Off
*
* Author: dianoga7@3dgo.net
* Date: 2013-07-21
*/
// Automatically generated. Make future change here.
definition(
name: "Thermostat Auto Off",
namespace: "dianoga",
author: "dianoga7@3dgo.net",
description: "Automatically turn off thermostat when windows/doors open. Turn it back on when everything is closed up.",
category: "Green Living",
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience%402x.png",
oauth: true
)
preferences {
section("Control") {
input("thermostat", "capability.thermostat", title: "Thermostat")
}
section("Open/Close") {
input("sensors", "capability.contactSensor", title: "Sensors", multiple: true)
input("delay", "number", title: "Delay (seconds) before turning thermostat off")
}
}
def installed() {
log.debug "Installed with settings: ${settings}"
initialize()
}
def updated() {
log.debug "Updated with settings: ${settings}"
unsubscribe()
unschedule()
initialize()
}
def initialize() {
state.changed = false
subscribe(sensors, 'contact', "sensorChange")
}
def sensorChange(evt) {
log.debug "Desc: $evt.value , $state"
if(evt.value == 'open' && !state.changed) {
log.debug "Scheduling turn off in $delay seconds"
state.scheduled = true;
runIn(delay, 'turnOff')
} else if(evt.value == 'closed' && (state.changed || state.scheduled)) {
if(!isOpen()) {
log.debug "Everything is closed, restoring thermostat"
state.scheduled = false;
unschedule('turnOff')
restore()
} else {
log.debug "Something is still open."
}
}
}
def isOpen() {
def result = sensors.find() { it.currentValue('contact') == 'open'; }
log.debug "isOpen results: $result"
return result
}
def turnOff() {
log.debug "Preparing to turn off thermostat due to contact open"
if(isOpen()) {
log.debug "It's safe. Turning it off."
state.thermostatMode = thermostat.currentValue("thermostatMode")
state.changed = true
thermostat.off()
log.debug "State: $state"
} else {
log.debug "Just kidding. The platform did something bad."
}
}
def restore() {
log.debug "Setting thermostat to $state.thermostatMode"
thermostat.setThermostatMode(state.thermostatMode)
state.changed = false
}