Update double-tap.groovy
[smartapps.git] / third-party / hvac-auto-off.smartapp.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) before turning thermostat off")
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         subscribe(sensors, "contact", sensorChange)
49 }
50
51 def sensorChange(evt) {
52         log.debug "Desc: $evt.value , $state"
53     if(evt.value == 'open' && !state.changed) {
54         log.debug "Scheduling turn off in $delay seconds"
55         state.scheduled = true;
56         runIn(delay, 'turnOff')
57     } else if(evt.value == 'closed' && (state.changed || state.scheduled)) {        
58         if(!isOpen()) {
59                 log.debug "Everything is closed, restoring thermostat"
60             state.scheduled = false;
61             unschedule('turnOff')
62                         restore()
63         } else {
64                 log.debug "Something is still open."
65         }
66     }
67 }
68
69 def isOpen() {
70         def result = sensors.find() { it.currentValue('contact') == 'open'; }
71     log.debug "isOpen results: $result"
72     
73     return result
74 }
75
76 def turnOff() {
77         log.debug "Preparing to turn off thermostat due to contact open"
78     if(isOpen()) {
79         log.debug "It's safe. Turning it off."
80                 state.thermostatMode = thermostat.currentValue("thermostatMode")
81         state.changed = true
82         thermostat.off()
83         log.debug "State: $state"
84     } else {
85         log.debug "Just kidding. The platform did something bad."
86     }
87 }
88
89 def restore() {
90     log.debug "Setting thermostat to $state.thermostatMode"
91     thermostat.setThermostatMode(state.thermostatMode)
92     state.changed = false
93 }