Update gentle-wake-up.groovy
[smartapps.git] / official / keep-me-cozy-ii.groovy
1 /**
2  *  Copyright 2015 SmartThings
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  *  in compliance with the License. You may obtain a copy of the License at:
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
10  *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
11  *  for the specific language governing permissions and limitations under the License.
12  *
13  *  Keep Me Cozy II
14  *
15  *  Author: SmartThings
16  */
17
18 definition(
19     name: "Keep Me Cozy II",
20     namespace: "smartthings",
21     author: "SmartThings",
22     description: "Works the same as Keep Me Cozy, but enables you to pick an alternative temperature sensor in a separate space from the thermostat. Focuses on making you comfortable where you are spending your time rather than where the thermostat is located.",
23     category: "Green Living",
24     iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/temp_thermo.png",
25     iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/temp_thermo@2x.png"
26 )
27
28 preferences() {
29         section("Choose thermostat... ") {
30                 input "thermostat", "capability.thermostat"
31         }
32         section("Heat setting..." ) {
33                 input "heatingSetpoint", "decimal", title: "Degrees"
34         }
35         section("Air conditioning setting...") {
36                 input "coolingSetpoint", "decimal", title: "Degrees"
37         }
38         section("Optionally choose temperature sensor to use instead of the thermostat's... ") {
39                 input "sensor", "capability.temperatureMeasurement", title: "Temp Sensors", required: false
40         }
41 }
42
43 def installed()
44 {
45         log.debug "enter installed, state: $state"
46         subscribeToEvents()
47 }
48
49 def updated()
50 {
51         log.debug "enter updated, state: $state"
52         unsubscribe()
53         subscribeToEvents()
54 }
55
56 def subscribeToEvents()
57 {
58         subscribe(location, changedLocationMode)
59         if (sensor) {
60                 subscribe(sensor, "temperature", temperatureHandler)
61                 subscribe(thermostat, "temperature", temperatureHandler)
62                 subscribe(thermostat, "thermostatMode", temperatureHandler)
63         }
64         evaluate()
65 }
66
67 def changedLocationMode(evt)
68 {
69         log.debug "changedLocationMode mode: $evt.value, heat: $heatingSetpoint, cool: $coolingSetpoint"
70         evaluate()
71 }
72
73 def temperatureHandler(evt)
74 {
75         evaluate()
76 }
77
78 private evaluate()
79 {
80         if (sensor) {
81                 def threshold = 1.0
82                 def tm = thermostat.currentThermostatMode
83                 def ct = thermostat.currentTemperature
84                 def currentTemp = sensor.currentTemperature
85                 log.trace("evaluate:, mode: $tm -- temp: $ct, heat: $thermostat.currentHeatingSetpoint, cool: $thermostat.currentCoolingSetpoint -- "  +
86                         "sensor: $currentTemp, heat: $heatingSetpoint, cool: $coolingSetpoint")
87                 if (tm in ["cool","auto"]) {
88                         // air conditioner
89                         if (currentTemp - coolingSetpoint >= threshold) {
90                                 thermostat.setCoolingSetpoint(ct - 2)
91                                 log.debug "thermostat.setCoolingSetpoint(${ct - 2}), ON"
92                         }
93                         else if (coolingSetpoint - currentTemp >= threshold && ct - thermostat.currentCoolingSetpoint >= threshold) {
94                                 thermostat.setCoolingSetpoint(ct + 2)
95                                 log.debug "thermostat.setCoolingSetpoint(${ct + 2}), OFF"
96                         }
97                 }
98                 if (tm in ["heat","emergency heat","auto"]) {
99                         // heater
100                         if (heatingSetpoint - currentTemp >= threshold) {
101                                 thermostat.setHeatingSetpoint(ct + 2)
102                                 log.debug "thermostat.setHeatingSetpoint(${ct + 2}), ON"
103                         }
104                         else if (currentTemp - heatingSetpoint >= threshold && thermostat.currentHeatingSetpoint - ct >= threshold) {
105                                 thermostat.setHeatingSetpoint(ct - 2)
106                                 log.debug "thermostat.setHeatingSetpoint(${ct - 2}), OFF"
107                         }
108                 }
109         }
110         else {
111                 thermostat.setHeatingSetpoint(heatingSetpoint)
112                 thermostat.setCoolingSetpoint(coolingSetpoint)
113                 //thermostat.poll() (Not supported command by thermostat device: according to Graph website!)
114         }
115 }
116
117 // for backward compatibility with existing subscriptions
118 def coolingSetpointHandler(evt) {
119         log.debug "coolingSetpointHandler()"
120 }
121 def heatingSetpointHandler (evt) {
122         log.debug "heatingSetpointHandler ()"
123 }