Update step-notifier.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 def windowChange(evt) {
75   def heating = thermostats.findAll { it?.latestValue("thermostatMode") == "heat" }
76   def cooling = thermostats.findAll { it?.latestValue("thermostatMode") == "cool" }
77
78   if(heating || cooling) {
79     def open = sensors.findAll { it?.latestValue("contact") == "open" }
80     def tempDirection = heating ? "heating" : "cooling"
81     def plural = open.size() > 1 ? "were" : "was"
82     send("${open.join(', ')} ${plural} opened and the thermostat is still ${tempDirection}.")
83
84     thermoShutOffTrigger()
85   }
86 }
87
88 def thermoShutOffTrigger() {
89   if(turnOffTherm == "Yes") {
90     log.info("Starting timer to turn off thermostat")
91     def delay = (turnOffDelay != null && turnOffDelay != "") ? turnOffDelay * 60 : 60
92     state.turnOffTime = now()
93
94     runIn(delay, "thermoShutOff")
95   }
96 }
97
98 def thermoShutOff() {
99   def open = sensors.findAll { it?.latestValue("contact") == "open" }
100   def tempDirection = heating ? "heating" : "cooling"
101   def plural = open.size() > 1 ? "are" : "is"
102
103   log.info("Checking if we need to turn thermostats off")
104
105   if(open.size()) {
106     send("Thermostats turned off: ${open.join(', ')} ${plural} open and thermostats ${tempDirection}.")
107     log.info("Windows still open, turning thermostats off")
108     thermostats?.off()
109   }
110
111   else {
112     log.info("Looks like everything is shut now - no need to turn off thermostats")
113   }
114 }
115
116 private send(msg) {
117   if(sendPushMessage != "No") {
118     log.debug("Sending push message")
119     sendPush(msg)
120   }
121
122   if(phone) {
123     log.debug("Sending text message")
124     sendSms(phone, msg)
125   }
126
127   log.debug(msg)
128 }