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