Update sonos-music-modes.groovy
[smartapps.git] / official / whole-house-fan.groovy
1 /**
2  *  Whole House Fan
3  *
4  *  Copyright 2014 Brian Steere
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
7  *  in compliance with the License. You may obtain a copy of the License at:
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
12  *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
13  *  for the specific language governing permissions and limitations under the License.
14  *
15  */
16 definition(
17     name: "Whole House Fan",
18     namespace: "dianoga",
19     author: "Brian Steere",
20     description: "Toggle a whole house fan (switch) when: Outside is cooler than inside, Inside is above x temp, Thermostat is off",
21     category: "Green Living",
22     iconUrl: "https://s3.amazonaws.com/smartapp-icons/Developers/whole-house-fan.png",
23     iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Developers/whole-house-fan%402x.png"
24 )
25
26
27 preferences {
28         section("Outdoor") {
29                 input "outTemp", "capability.temperatureMeasurement", title: "Outdoor Thermometer", required: true
30         }
31     
32     section("Indoor") {
33         input "inTemp", "capability.temperatureMeasurement", title: "Indoor Thermometer", required: true
34         input "minTemp", "number", title: "Minimum Indoor Temperature"
35         input "fans", "capability.switch", title: "Vent Fan", multiple: true, required: true
36     }
37     
38     section("Thermostat") {
39         input "thermostat", "capability.thermostat", title: "Thermostat"
40     }
41     
42     section("Windows/Doors") {
43         paragraph "[Optional] Only turn on the fan if at least one of these is open"
44         input "checkContacts", "enum", title: "Check windows/doors", options: ['Yes', 'No'], required: true 
45         input "contacts", "capability.contactSensor", title: "Windows/Doors", multiple: true, required: false
46     }
47 }
48
49 def installed() {
50         log.debug "Installed with settings: ${settings}"
51
52         initialize()
53 }
54
55 def updated() {
56         log.debug "Updated with settings: ${settings}"
57
58         unsubscribe()
59         initialize()
60 }
61
62 def initialize() {
63         state.fanRunning = false;
64     
65     subscribe(outTemp, "temperature", "checkThings");
66     subscribe(inTemp, "temperature", "checkThings");
67     subscribe(thermostat, "thermostatMode", "checkThings");
68     subscribe(contacts, "contact", "checkThings");
69 }
70
71 def checkThings(evt) {
72         def outsideTemp = settings.outTemp.currentTemperature
73     def insideTemp = settings.inTemp.currentTemperature
74     def thermostatMode = settings.thermostat.currentThermostatMode
75     def somethingOpen = settings.checkContacts == 'No' || settings.contacts?.find { it.currentContact == 'open' }
76     
77     log.debug "Inside: $insideTemp, Outside: $outsideTemp, Thermostat: $thermostatMode, Something Open: $somethingOpen"
78     
79     def shouldRun = true;
80     
81     if(thermostatMode != 'off') {
82         log.debug "Not running due to thermostat mode"
83         shouldRun = false;
84     }
85     
86     if(insideTemp < outsideTemp) {
87         log.debug "Not running due to insideTemp > outdoorTemp"
88         shouldRun = false;
89     }
90     
91     if(insideTemp < settings.minTemp) {
92         log.debug "Not running due to insideTemp < minTemp"
93         shouldRun = false;
94     }
95     
96     if(!somethingOpen) {
97         log.debug "Not running due to nothing open"
98         shouldRun = false
99     }
100     
101     if(shouldRun && !state.fanRunning) {
102         fans.on();
103         state.fanRunning = true;
104     } else if(!shouldRun && state.fanRunning) {
105         fans.off();
106         state.fanRunning = false;
107     }
108 }