Update garage-switch.groovy
[smartapps.git] / official / thermostat-auto-off.groovy
1 /**
2  *  HVAC Auto Off
3  *
4  *  Author: dianoga7@3dgo.net
5  *  Date: 2013-07-21
6  */
7
8 // Automatically generated. Make future change here.
9 definition(
10     name: "Thermostat Auto Off",
11     namespace: "dianoga",
12     author: "dianoga7@3dgo.net",
13     description: "Automatically turn off thermostat when windows/doors open. Turn it back on when everything is closed up.",
14     category: "Green Living",
15     iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
16     iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience%402x.png",
17     oauth: true
18 )
19
20 preferences {
21         section("Control") {
22                 input("thermostat", "capability.thermostat", title: "Thermostat")
23         }
24     
25     section("Open/Close") {
26         input("sensors", "capability.contactSensor", title: "Sensors", multiple: true)
27         input("delay", "number", title: "Delay (seconds)")
28     }
29 }
30
31 def installed() {
32         log.debug "Installed with settings: ${settings}"
33
34         initialize()
35 }
36
37 def updated() {
38         log.debug "Updated with settings: ${settings}"
39
40         unsubscribe()
41     unschedule()
42         initialize()
43 }
44
45 def initialize() {
46         state.changed = false
47         subscribe(sensors, "contact", sensorChange)
48 }
49
50 def sensorChange(evt) {
51         log.debug "Desc: $evt.value , $state"
52     if(evt.value == 'open' && !state.changed) {
53         unschedule()
54         runIn(delay, 'turnOff')
55     } else if(evt.value == 'closed' && state.changed) {
56         // All closed?
57         def isOpen = false
58         for(sensor in sensors) {
59                 if(sensor.id != evt.deviceId && sensor.currentValue('contact') == 'open') {
60                         isOpen = true
61             }
62         }
63         
64         if(!isOpen) {
65                 unschedule()
66                 runIn(delay, 'restore')
67         }
68     }
69 }
70
71 def turnOff() {
72         log.debug "Turning off thermostat due to contact open"
73         state.thermostatMode = thermostat.currentValue("thermostatMode")
74         thermostat.off()
75     state.changed = true
76     log.debug "State: $state"
77 }
78
79 def restore() {
80     log.debug "Setting thermostat to $state.thermostatMode"
81     thermostat.setThermostatMode(state.thermostatMode)
82     state.changed = false
83 }