Update double-tap.groovy
[smartapps.git] / official / thermostat-window-check.groovy
1 /**
2  *  Thermostat Window Check
3  *
4  *  Author: brian@bevey.org
5  *  Date: 9/13/13
6  *
7  *  If your heating or cooling system come on, it gives you notice if there are
8  *  any windows or doors left open, preventing the system from working
9  *  optimally.
10  */
11
12 definition(
13   name: "Thermostat Window Check",
14   namespace: "imbrianj",
15   author: "brian@bevey.org",
16   description: "If your heating or cooling system come on, it gives you notice if there are any windows or doors left open, preventing the system from working optimally.",
17   category: "Green Living",
18   iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
19   iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience%402x.png"
20 )
21
22 preferences {
23   section("Things to check?") {
24     input "sensors", "capability.contactSensor", multiple: true
25   }
26
27   section("Thermostats to monitor") {
28     input "thermostats", "capability.thermostat", multiple: true
29   }
30
31   section("Notifications") {
32     input "sendPushMessage", "enum", title: "Send a push notification?", metadata: [values: ["Yes", "No"]], required: false
33     input "phone", "phone", title: "Send a Text Message?", required: false
34   }
35
36   section("Turn thermostat off automatically?") {
37     input "turnOffTherm", "enum", metadata: [values: ["Yes", "No"]], required: false
38   }
39
40   section("Delay to wait before turning thermostat off (defaults to 1 minute)") {
41     input "turnOffDelay", "decimal", title: "Number of minutes", required: false
42   }
43 }
44
45 def installed() {
46   subscribe(thermostats, "thermostatMode", thermoChange);
47   subscribe(sensors, "contact.open", windowChange);
48 }
49
50 def updated() {
51   unsubscribe()
52   subscribe(thermostats, "thermostatMode", thermoChange);
53   subscribe(sensors, "contact.open", windowChange);
54 }
55
56 def thermoChange(evt) {
57   if(evt.value == "heat" ||
58      evt.value == "cool") {
59     def open = sensors.findAll { it?.latestValue("contact") == "open" }
60
61     if(open) {
62       def plural = open.size() > 1 ? "are" : "is"
63       send("${open.join(', ')} ${plural} still open and the thermostat just came on.")
64
65       thermoShutOffTrigger()
66     }
67
68     else {
69       log.info("Thermostat came on and nothing is open.");
70     }
71   }
72 }
73
74 // These should have been global variables
75 def heating
76 def cooling
77
78 def windowChange(evt) {
79   heating = thermostats.findAll { it?.latestValue("thermostatMode") == "heat" }
80   cooling = thermostats.findAll { it?.latestValue("thermostatMode") == "cool" }
81
82   if(heating || cooling) {
83     def open = sensors.findAll { it?.latestValue("contact") == "open" }
84     def tempDirection = heating ? "heating" : "cooling"
85     def plural = open.size() > 1 ? "were" : "was"
86     send("${open.join(', ')} ${plural} opened and the thermostat is still ${tempDirection}.")
87
88     thermoShutOffTrigger()
89   }
90 }
91
92 def thermoShutOffTrigger() {
93   if(turnOffTherm == "Yes") {
94     log.info("Starting timer to turn off thermostat")
95     def delay = (turnOffDelay != null && turnOffDelay != "") ? turnOffDelay * 60 : 60
96     state.turnOffTime = now()
97
98     runIn(delay, "thermoShutOff")
99   }
100 }
101
102 def thermoShutOff() {
103   def open = sensors.findAll { it?.latestValue("contact") == "open" }
104   def tempDirection = heating ? "heating" : "cooling"
105   def plural = open.size() > 1 ? "are" : "is"
106
107   log.info("Checking if we need to turn thermostats off")
108
109   if(open.size()) {
110     send("Thermostats turned off: ${open.join(', ')} ${plural} open and thermostats ${tempDirection}.")
111     log.info("Windows still open, turning thermostats off")
112     thermostats?.off()
113   }
114
115   else {
116     log.info("Looks like everything is shut now - no need to turn off thermostats")
117   }
118 }
119
120 private send(msg) {
121   if(sendPushMessage != "No") {
122     log.debug("Sending push message")
123     sendPush(msg)
124   }
125
126   if(phone) {
127     log.debug("Sending text message")
128     sendSms(phone, msg)
129   }
130
131   log.debug(msg)
132 }