-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathThermostatAutoAway.groovy
102 lines (93 loc) · 2.87 KB
/
ThermostatAutoAway.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
/**
* Thermostat Auto Away
*
* Author: sidjohn1@gmail.com
* Date: 2013-12-04
*/
// Automatically generated. Make future change here.
definition(
name: "Thermostat Auto Away",
namespace: "sidjohn1",
author: "sidjohn1@gmail.com",
description: "Simply marks any thermostat away after everyone leaves. Great for Nest!",
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"
)
preferences {
section("When all of these people leave home") {
input "people", "capability.presenceSensor", multiple: true, required: true
}
section("Set this thermostat to away ") {
input "thermostats", "capability.thermostat", multiple: true, required: true
}
section("False alarm threshold (defaults to 10 min)") {
input "falseAlarmThreshold", "decimal", title: "Number of minutes", required: false
}
}
def installed() {
log.debug "Installed with settings: ${settings}"
log.debug "Current mode = ${location.mode}, people = ${people.collect{it.label + ': ' + it.currentPresence}}"
subscribe(people, "presence", presence)
}
def updated() {
log.debug "Updated with settings: ${settings}"
log.debug "Current mode = ${location.mode}, people = ${people.collect{it.label + ': ' + it.currentPresence}}"
unsubscribe()
subscribe(people, "presence", presence)
}
def presence(evt)
{
log.debug "evt.name: $evt.value"
if (evt.value == "not present") {
if (location.mode != newMode) {
log.debug "checking if everyone is away"
if (everyoneIsAway()) {
log.debug "starting sequence"
runIn(findFalseAlarmThreshold() * 60, "takeAction", [overwrite: false])
}
}
else {
log.debug "mode is the same, not evaluating"
}
}
else {
log.debug "present; doing nothing"
}
}
def takeAction()
{
if (everyoneIsAway()) {
def threshold = 1000 * 60 * findFalseAlarmThreshold() - 1000
def awayLongEnough = people.findAll { person ->
def presenceState = person.currentState("presence")
def elapsed = now() - presenceState.rawDateCreated.time
elapsed >= threshold
}
log.debug "Found ${awayLongEnough.size()} out of ${people.size()} person(s) who were away long enough"
if (awayLongEnough.size() == people.size()) {
def message = "SmartThings changed your thermostat to away because everyone left home"
log.info message
thermostats?.away()
} else {
log.debug "not everyone has been away long enough; doing nothing"
}
} else {
log.debug "not everyone is away; doing nothing"
}
}
private everyoneIsAway()
{
def result = true
for (person in people) {
if (person.currentPresence == "present") {
result = false
break
}
}
log.debug "everyoneIsAway: $result"
return result
}
private findFalseAlarmThreshold() {
(falseAlarmThreshold != null && falseAlarmThreshold != "") ? falseAlarmThreshold : 10
}