Advertisement
Guest User

Untitled

a guest
Oct 17th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.22 KB | None | 0 0
  1.  
  2. // Automatically generated. Make future change here.
  3. definition(
  4. name: "Manage general thermostat",
  5. namespace: "minollo",
  6. author: "minollo@minollo.com",
  7. description: "Manage general thermostat",
  8. category: "My Apps",
  9. iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
  10. iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience%402x.png",
  11. iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience%402x.png")
  12.  
  13. preferences {
  14. section("Polling...") {
  15. input "switchSuspendPolling", "capability.switch", title: "Switch to suspend polling", required: false
  16. }
  17. section("Choose thermostat... ") {
  18. input "thermostat", "capability.thermostat"
  19. input "runProgramming", "boolean", title: "Run Programming?", required: false, defaultValue: true
  20. }
  21. section("Helper heaters... "){
  22. input "heaters", "capability.switch", title: "Heaters", multiple: true, required: false
  23. }
  24. section("Mode #1 temperature control") {
  25. input "mode1", "text", title: "Mode #1?"
  26. input "heatingSetpoint1", "number", title: "Heating temp (F)?", required: false
  27. input "coolingSetpoint1", "number", title: "Cooling temp (F)?", required: false
  28. }
  29. section("Mode #2 temperature control") {
  30. input "mode2", "text", title: "Mode #2?"
  31. input "heatingSetpoint2", "number", title: "Heating temp (F)?", required: false
  32. input "coolingSetpoint2", "number", title: "Cooling temp (F)?", required: false
  33. }
  34. section("Mode #3 temperature control") {
  35. input "mode3", "text", title: "Mode #3?", required: false
  36. input "heatingSetpoint3", "number", title: "Heating temp (F)?", required: false
  37. input "coolingSetpoint3", "number", title: "Cooling temp (F)?", required: false
  38. }
  39. section("Away temperature control") {
  40. input "heatingSetpointAway", "number", title: "Heating temp (F)?", required: false
  41. input "coolingSetpointAway", "number", title: "Cooling temp (F)?", required: false
  42. }
  43. }
  44.  
  45. def installed()
  46. {
  47. initialize()
  48. subscribe(thermostat, "heatingSetpoint", heatingSetpointHandler)
  49. subscribe(thermostat, "coolingSetpoint", coolingSetpointHandler)
  50. subscribe(thermostat, "thermostatOperatingState", operatingStateHandler)
  51. subscribe(thermostat, "temperature", temperatureHandler)
  52. subscribe(thermostat, "thermostatMode", thermostatModeHandler)
  53. subscribe(location, "mode", changedLocationMode)
  54. subscribe(app, appTouch)
  55. scheduleTimer()
  56. log.debug "Scheduling initialize"
  57. runIn(20, initialize)
  58. }
  59.  
  60. def updated()
  61. {
  62. unsubscribe()
  63. try {unschedule()} catch(e) {log.error "Error unscheduling: ${e}"}
  64. subscribe(thermostat, "heatingSetpoint", heatingSetpointHandler)
  65. subscribe(thermostat, "coolingSetpoint", coolingSetpointHandler)
  66. subscribe(thermostat, "thermostatOperatingState", operatingStateHandler)
  67. subscribe(thermostat, "temperature", temperatureHandler)
  68. subscribe(thermostat, "thermostatMode", thermostatModeHandler)
  69. subscribe(location, "mode", changedLocationMode)
  70. subscribe(app, appTouch)
  71. scheduleTimer()
  72. log.debug "Scheduling initialize"
  73. runIn(20, initialize)
  74. }
  75.  
  76.  
  77. def initialize() {
  78. log.debug "Initializing"
  79. if (heatingSetpoint1 == null) heatingSetpoint1 = 40
  80. if (heatingSetpoint2 == null) heatingSetpoint2 = 40
  81. if (heatingSetpoint3 == null) heatingSetpoint3 = 40
  82. if (heatingSetpointAway == null) heatingSetpointAway = 40
  83. if (coolingSetpoint1 == null) coolingSetpoint1 = 85
  84. if (coolingSetpoint2 == null) coolingSetpoint2 = 85
  85. if (coolingSetpoint3 == null) coolingSetpoint3 = 85
  86. if (coolingSetpointAway == null) coolingSetpointAway = 85
  87. doUpdateTempSettings()
  88. }
  89.  
  90. def heatingSetpointHandler(evt)
  91. {
  92. log.debug "heatingSetpoint: $evt.value, $settings, temperature is ${thermostat.currentValue("temperature")}"
  93. handleHelperHeaters()
  94. if (toDouble(evt.value) >= toDouble(thermostat.currentValue("temperature")) && thermostat.currentValue("thermostatMode") != "heat" && thermostat.currentValue("thermostatMode") != "emergencyHeat") {
  95. log.debug "Set mode to heat"
  96. thermostat.heat()
  97. }
  98. }
  99.  
  100. def coolingSetpointHandler(evt)
  101. {
  102. log.debug "coolingSetpoint: $evt.value, $settings, temperature is ${thermostat.currentValue("temperature")}"
  103. if (toDouble(evt.value) <= toDouble(thermostat.currentValue("temperature")) && thermostat.currentValue("thermostatMode") != "cool") {
  104. log.debug "Set mode to cool"
  105. thermostat.cool()
  106. }
  107. }
  108.  
  109. def operatingStateHandler(evt)
  110. {
  111. log.debug "thermostatOperatingState: $evt.value"
  112. if (heaters) {
  113. state.latestTurnOnAuxHeaters = null
  114. try {unschedule(turnOnAuxHeaters)} catch(e) {log.error "Ignoring error: ${e}"}
  115. if (evt.value != "heating" && heaters.first().currentValue("switch") == "on") {
  116. log.info "Themostat not heating; shutting down any aux heat"
  117. heaters.off()
  118. } else if (evt.value == "heating") {
  119. def minutes = 30
  120. log.info "Themostat started heating; turn on heaters in $minutes minutes if not done"
  121. state.latestTurnOnAuxHeaters = now()
  122. runIn(minutes * 60, turnOnAuxHeaters)
  123. }
  124. }
  125. }
  126.  
  127. def turnOnAuxHeaters() {
  128. def mode = location.currentMode.name
  129. if (state.latestTurnOnAuxHeaters != null) {
  130. log.debug "turnOnAuxHeaters; thermostatOperatingState is ${thermostat.currentValue("thermostatOperatingState")}"
  131. if (thermostat.currentValue("thermostatOperatingState") != "heating") {
  132. log.info("Ignoring request to turn on aux heaters, as thermostat is not heating")
  133. } else if (mode.startsWith(mode1) || mode.startsWith(mode2) || mode3 == null || mode.startsWith(mode3)) {
  134. log.info("Thermostat has been working for a while heating; turn on AUX heaters")
  135. heaters.on()
  136. } else {
  137. log.info("Thermostat has been working for a while heating, but mode is away; ignore the problem")
  138. }
  139. state.latestTurnOnAuxHeaters = null
  140. }
  141. }
  142.  
  143. def temperatureHandler(evt)
  144. {
  145. log.debug "currentTemperature: $evt.value, $settings"
  146. handleHelperHeaters()
  147. if (toDouble(evt.value) <= toDouble(thermostat.currentValue("heatingSetpoint")) && thermostat.currentValue("thermostatMode") != "heat" && thermostat.currentValue("thermostatMode") != "emergencyHeat") {
  148. log.debug "Set mode to heat"
  149. thermostat.heat()
  150. } else if (toDouble(evt.value) >= toDouble(thermostat.currentValue("coolingSetpoint")) && thermostat.currentValue("thermostatMode") != "cool") {
  151. log.debug "Set mode to cool"
  152. thermostat.cool()
  153. }
  154. }
  155.  
  156. private handleHelperHeaters() {
  157. if (heaters) {
  158. def tempNow = toDouble(thermostat.currentValue("temperature"))
  159. log.debug "Temperature now is: ${tempNow}"
  160. def tempDistance = toDouble(thermostat.currentValue("heatingSetpoint")) - toDouble(tempNow)
  161. log.debug "Temperature distance for heating: ${tempDistance}"
  162. if ( tempDistance > 2.0 && heaters.first().currentValue("switch") == "off") {
  163. log.info "Switching helper heaters on"
  164. heaters.on()
  165. } else if ( tempDistance <= 0.0 && heaters.first().currentValue("switch") == "on") {
  166. log.info "Switching helper heaters off"
  167. state.latestTurnOnAuxHeaters = null
  168. try {unschedule(turnOnAuxHeaters)} catch(e) {log.error "Ignoring error: ${e}"}
  169. heaters.off()
  170. }
  171. }
  172. }
  173.  
  174. def thermostatModeHandler(evt)
  175. {
  176. log.debug "thermostatModeHandler: $evt.value, $settings"
  177. }
  178.  
  179. def changedLocationMode(evt)
  180. {
  181. log.debug "Now it's ${new Date(now())}"
  182. log.debug "changedLocationMode: $evt.value, $settings"
  183. state.modifiedLatestMode = null
  184. state.latestDoUpdateTempSettings = now()
  185. try {runIn(30, doUpdateTempSettings)} catch(e) {log.error "Error scheduling: ${e}"} //give the system a little time to react before checking status
  186.  
  187. log.debug "temperature now is: ${thermostat.currentValue("temperature")}"
  188. log.debug "thermostat state now is: ${thermostat.currentValue("thermostatMode")}"
  189. }
  190.  
  191. def doUpdateTempSettings()
  192. {
  193. log.debug "doUpdateTempSettings; runProgramming==${runProgramming}"
  194. def mode = location.currentMode.name
  195. state.latestDoUpdateTempSettings = null
  196. state.latestMode = mode
  197. state.modifiedLatestMode = null
  198. if (runProgramming == false || runProgramming=='false') {
  199. log.debug "Programming run is disabled"
  200. return
  201. }
  202. log.debug "Location mode is ${mode}"
  203. if (mode.startsWith(mode1)) {
  204. if (heatingSetpoint1 && heatingSetpoint1 != "") {
  205. setHeatingSetpoint(['heatingSetpoint': heatingSetpoint1])
  206. }
  207. if (coolingSetpoint1 && coolingSetpoint1 != "") {
  208. setCoolingSetpoint(['coolingSetpoint': coolingSetpoint1])
  209. }
  210. } else if (mode.startsWith(mode2)) {
  211. if (heatingSetpoint2 && heatingSetpoint2 != "") {
  212. setHeatingSetpoint(['heatingSetpoint': heatingSetpoint2])
  213. }
  214. if (coolingSetpoint2 && coolingSetpoint2 != "") {
  215. setCoolingSetpoint(['coolingSetpoint': coolingSetpoint2])
  216. }
  217. } else if (mode3 && mode.startsWith(mode3)) {
  218. if (heatingSetpoint3 && heatingSetpoint3 != "") {
  219. setHeatingSetpoint(['heatingSetpoint': heatingSetpoint3])
  220. }
  221. if (coolingSetpoint3 && coolingSetpoint3 != "") {
  222. setCoolingSetpoint(['coolingSetpoint': coolingSetpoint3])
  223. }
  224. } else { //away
  225. if (heatingSetpointAway && heatingSetpointAway != "") {
  226. setHeatingSetpoint(['heatingSetpoint': heatingSetpointAway])
  227. }
  228. if (coolingSetpointAway && coolingSetpointAway != "") {
  229. setCoolingSetpoint(['coolingSetpoint': coolingSetpointAway])
  230. }
  231. }
  232. // thermostat.poll()
  233. }
  234.  
  235. def scheduleTimer() {
  236. runEvery5Minutes(keepAlive)
  237. }
  238.  
  239. def setCoolingSetpoint(data) {
  240. def currentSetpoint = thermostat.currentValue('coolingSetpoint')
  241. log.debug "setCoolingSetpoint(${data}), current coolingSetpoint = ${currentSetpoint}"
  242. if (currentSetpoint != data.coolingSetpoint) {
  243. log.debug "Setting setCoolingSetpoint(${data.coolingSetpoint})"
  244. thermostat.setCoolingSetpoint(data.coolingSetpoint)
  245. runIn(30, setCoolingSetpoint, ['data': ['coolingSetpoint': data.coolingSetpoint]])
  246. }
  247. }
  248.  
  249. def setHeatingSetpoint(data) {
  250. def currentSetpoint = thermostat.currentValue('heatingSetpoint')
  251. log.debug "setHeatingSetpoint(${data}), current heatingSetpoint = ${currentSetpoint}"
  252. if (currentSetpoint != data.heatingSetpoint) {
  253. log.debug "Setting setHeatingSetpoint(${data.heatingSetpoint})"
  254. thermostat.setHeatingSetpoint(data.heatingSetpoint)
  255. runIn(30, setHeatingSetpoint, ['data': ['heatingSetpoint': data.heatingSetpoint]])
  256. }
  257. }
  258.  
  259. def appTouch(evt)
  260. {
  261. log.debug "appTouch: $evt, $settings"
  262. doUpdateTempSettings()
  263. }
  264.  
  265. def keepAlive()
  266. {
  267. log.debug "[Polling] keepAliveLatest == ${state.keepAliveLatest}; now() == ${now()}"
  268. state.keepAliveLatest = now()
  269. if (switchSuspendPolling == null || switchSuspendPolling.currentValue("switch") == "off") {
  270. thermostat.refresh()
  271. } else {
  272. log.warn "Polling is suspended"
  273. }
  274. if (state.latestMode == null || state.latestMode == mode1 || state.latestMode == mode2 || state.latestMode == mode3) { //Check current mode only if latest set state is not away
  275. if (!state.latestMode || state.latestMode != location.currentMode.name) {
  276. log.warn "Double checking: state=${state}"
  277. if (state.modifiedLatestMode == location.mode) {
  278. log.warn "Mode has changed to ${location.mode} (checked twice); update settings"
  279. doUpdateTempSettings()
  280. } else {
  281. state.modifiedLatestMode = location.mode
  282. log.warn "Mode seems to have changed (to ${location.mode}); but let's wait one more polling cycle..."
  283. }
  284. } else {
  285. state.latestMode = location.currentMode.name
  286. state.modifiedLatestMode = null
  287. }
  288. } else {
  289. state.modifiedLatestMode = null
  290. }
  291. }
  292.  
  293. // catchall
  294. def event(evt)
  295. {
  296. // log.debug "value: $evt.value, event: $evt, settings: $settings, handlerName: ${evt.handlerName}"
  297. }
  298.  
  299. private toDouble(anObj) {
  300. if (anObj instanceof String) {
  301. Double.parseDouble(anObj)
  302. } else {
  303. anObj
  304. }
  305. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement