forked from SmartThingsCommunity/SmartThingsPublic
/
motion-contact-activated-thermostat.groovy
106 lines (90 loc) · 3.45 KB
/
motion-contact-activated-thermostat.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
/**
* Motion/Contact Activated Thermostat
*
* Author: Chris LeBlanc
*/
definition(
name: "Motion/Contact Activated Thermostat",
namespace: "LeBlaaanc",
author: "Chris LeBlanc",
description: "Changes your thermostat settings automatically in response to motion or contact sensor activities.",
category: "Green Living",
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/temp_thermo.png",
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/temp_thermo@2x.png"
)
preferences {
section("On this thermostat... ") {
input "thermostat", "capability.thermostat"
}
section("When movement is detected by any of these...") {
input "motion", "capability.motionSensor", title: "Motion Sensor(s)", multiple: true, required: false
}
section("Or when any of these are open...") {
input "contact", "capability.contactSensor", title: "Contact Sensor", multiple: true, required: false
}
section("Delay the no motion event...") {
input "delayNoMotionEvent", "number", title: "Minutes", description: "60 minutes", required: false
}
section("Set motion air conditioning setting..."){
input "coolingSetpointMotion", "number", title: "Degrees?", required: false
}
section("Set no motion air conditioning setting..."){
input "coolingSetpointNoMotion", "number", title: "Degrees?", required: false
}
section("Set motion heat setting...") {
input "heatingSetpointMotion", "number", title: "Degrees?", required: false
}
section("Set no motion heat setting...") {
input "heatingSetpointNoMotion", "number", title: "Degrees?", required: false
}
}
def installed()
{
subscribe(motion, "motion", eventHandler)
subscribe(contact, "contact", eventHandler)
if (coolingSetpointMotion != null) log.debug "coolingSetpointMotion: $coolingSetpointMotion heatingSetpointMotion: $heatingSetpointMotion"
}
def updated()
{
unsubscribe()
subscribe(motion, "motion", eventHandler)
subscribe(contact, "contact", eventHandler)
if (coolingSetpointMotion != null) log.debug "coolingSetpointMotion: $coolingSetpointMotion heatingSetpointMotion: $heatingSetpointMotion"
}
def eventHandler(evt)
{
log.debug "device: $device, value: $evt.value, event: $evt, settings: $settings, handlerName: ${evt.handlerName}"
// set the delay, defaulting to 60 minutes
def delay = (delayNoMotionEvent != null && delayNoMotionEvent != "") ? delayNoMotionEvent * 60 : 3600
// unschedule delay since motion is detected and set temp to active state
if (evt.value == 'active' || evt.value == 'open')
{
log.debug "unscheduling no motion temp timer"
unschedule(setTemp)
log.debug "setting motion temp"
setTemp('motion')
}
else
{
// schedule delay since no motion on any device is found
if (motion.find{it.currentState('motion').value == "active"} == null &&
contact.find{it.currentState('contact').value == "open"} == null)
{
log.debug "will set no motion temp in $delay secs"
runIn(delay, setTemp)
}
}
}
def setTemp(state)
{
if (state == "motion")
{
if (coolingSetpointMotion != null) thermostat.setCoolingSetpoint(coolingSetpointMotion)
if (heatingSetpointMotion != null) thermostat.setHeatingSetpoint(heatingSetpointMotion)
}
else
{
if (coolingSetpointNoMotion != null) thermostat.setCoolingSetpoint(coolingSetpointNoMotion)
if (heatingSetpointNoMotion != null) thermostat.setHeatingSetpoint(heatingSetpointNoMotion)
}
}