Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Automatically generated. Make future change here.
- definition(
- name: "Manage general thermostat",
- namespace: "minollo",
- author: "minollo@minollo.com",
- description: "Manage general thermostat",
- category: "My Apps",
- iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
- iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience%402x.png",
- iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience%402x.png")
- preferences {
- section("Polling...") {
- input "switchSuspendPolling", "capability.switch", title: "Switch to suspend polling", required: false
- }
- section("Choose thermostat... ") {
- input "thermostat", "capability.thermostat"
- input "runProgramming", "boolean", title: "Run Programming?", required: false, defaultValue: true
- }
- section("Helper heaters... "){
- input "heaters", "capability.switch", title: "Heaters", multiple: true, required: false
- }
- section("Mode #1 temperature control") {
- input "mode1", "text", title: "Mode #1?"
- input "heatingSetpoint1", "number", title: "Heating temp (F)?", required: false
- input "coolingSetpoint1", "number", title: "Cooling temp (F)?", required: false
- }
- section("Mode #2 temperature control") {
- input "mode2", "text", title: "Mode #2?"
- input "heatingSetpoint2", "number", title: "Heating temp (F)?", required: false
- input "coolingSetpoint2", "number", title: "Cooling temp (F)?", required: false
- }
- section("Mode #3 temperature control") {
- input "mode3", "text", title: "Mode #3?", required: false
- input "heatingSetpoint3", "number", title: "Heating temp (F)?", required: false
- input "coolingSetpoint3", "number", title: "Cooling temp (F)?", required: false
- }
- section("Away temperature control") {
- input "heatingSetpointAway", "number", title: "Heating temp (F)?", required: false
- input "coolingSetpointAway", "number", title: "Cooling temp (F)?", required: false
- }
- }
- def installed()
- {
- initialize()
- subscribe(thermostat, "heatingSetpoint", heatingSetpointHandler)
- subscribe(thermostat, "coolingSetpoint", coolingSetpointHandler)
- subscribe(thermostat, "thermostatOperatingState", operatingStateHandler)
- subscribe(thermostat, "temperature", temperatureHandler)
- subscribe(thermostat, "thermostatMode", thermostatModeHandler)
- subscribe(location, "mode", changedLocationMode)
- subscribe(app, appTouch)
- scheduleTimer()
- log.debug "Scheduling initialize"
- runIn(20, initialize)
- }
- def updated()
- {
- unsubscribe()
- try {unschedule()} catch(e) {log.error "Error unscheduling: ${e}"}
- subscribe(thermostat, "heatingSetpoint", heatingSetpointHandler)
- subscribe(thermostat, "coolingSetpoint", coolingSetpointHandler)
- subscribe(thermostat, "thermostatOperatingState", operatingStateHandler)
- subscribe(thermostat, "temperature", temperatureHandler)
- subscribe(thermostat, "thermostatMode", thermostatModeHandler)
- subscribe(location, "mode", changedLocationMode)
- subscribe(app, appTouch)
- scheduleTimer()
- log.debug "Scheduling initialize"
- runIn(20, initialize)
- }
- def initialize() {
- log.debug "Initializing"
- if (heatingSetpoint1 == null) heatingSetpoint1 = 40
- if (heatingSetpoint2 == null) heatingSetpoint2 = 40
- if (heatingSetpoint3 == null) heatingSetpoint3 = 40
- if (heatingSetpointAway == null) heatingSetpointAway = 40
- if (coolingSetpoint1 == null) coolingSetpoint1 = 85
- if (coolingSetpoint2 == null) coolingSetpoint2 = 85
- if (coolingSetpoint3 == null) coolingSetpoint3 = 85
- if (coolingSetpointAway == null) coolingSetpointAway = 85
- doUpdateTempSettings()
- }
- def heatingSetpointHandler(evt)
- {
- log.debug "heatingSetpoint: $evt.value, $settings, temperature is ${thermostat.currentValue("temperature")}"
- handleHelperHeaters()
- if (toDouble(evt.value) >= toDouble(thermostat.currentValue("temperature")) && thermostat.currentValue("thermostatMode") != "heat" && thermostat.currentValue("thermostatMode") != "emergencyHeat") {
- log.debug "Set mode to heat"
- thermostat.heat()
- }
- }
- def coolingSetpointHandler(evt)
- {
- log.debug "coolingSetpoint: $evt.value, $settings, temperature is ${thermostat.currentValue("temperature")}"
- if (toDouble(evt.value) <= toDouble(thermostat.currentValue("temperature")) && thermostat.currentValue("thermostatMode") != "cool") {
- log.debug "Set mode to cool"
- thermostat.cool()
- }
- }
- def operatingStateHandler(evt)
- {
- log.debug "thermostatOperatingState: $evt.value"
- if (heaters) {
- state.latestTurnOnAuxHeaters = null
- try {unschedule(turnOnAuxHeaters)} catch(e) {log.error "Ignoring error: ${e}"}
- if (evt.value != "heating" && heaters.first().currentValue("switch") == "on") {
- log.info "Themostat not heating; shutting down any aux heat"
- heaters.off()
- } else if (evt.value == "heating") {
- def minutes = 30
- log.info "Themostat started heating; turn on heaters in $minutes minutes if not done"
- state.latestTurnOnAuxHeaters = now()
- runIn(minutes * 60, turnOnAuxHeaters)
- }
- }
- }
- def turnOnAuxHeaters() {
- def mode = location.currentMode.name
- if (state.latestTurnOnAuxHeaters != null) {
- log.debug "turnOnAuxHeaters; thermostatOperatingState is ${thermostat.currentValue("thermostatOperatingState")}"
- if (thermostat.currentValue("thermostatOperatingState") != "heating") {
- log.info("Ignoring request to turn on aux heaters, as thermostat is not heating")
- } else if (mode.startsWith(mode1) || mode.startsWith(mode2) || mode3 == null || mode.startsWith(mode3)) {
- log.info("Thermostat has been working for a while heating; turn on AUX heaters")
- heaters.on()
- } else {
- log.info("Thermostat has been working for a while heating, but mode is away; ignore the problem")
- }
- state.latestTurnOnAuxHeaters = null
- }
- }
- def temperatureHandler(evt)
- {
- log.debug "currentTemperature: $evt.value, $settings"
- handleHelperHeaters()
- if (toDouble(evt.value) <= toDouble(thermostat.currentValue("heatingSetpoint")) && thermostat.currentValue("thermostatMode") != "heat" && thermostat.currentValue("thermostatMode") != "emergencyHeat") {
- log.debug "Set mode to heat"
- thermostat.heat()
- } else if (toDouble(evt.value) >= toDouble(thermostat.currentValue("coolingSetpoint")) && thermostat.currentValue("thermostatMode") != "cool") {
- log.debug "Set mode to cool"
- thermostat.cool()
- }
- }
- private handleHelperHeaters() {
- if (heaters) {
- def tempNow = toDouble(thermostat.currentValue("temperature"))
- log.debug "Temperature now is: ${tempNow}"
- def tempDistance = toDouble(thermostat.currentValue("heatingSetpoint")) - toDouble(tempNow)
- log.debug "Temperature distance for heating: ${tempDistance}"
- if ( tempDistance > 2.0 && heaters.first().currentValue("switch") == "off") {
- log.info "Switching helper heaters on"
- heaters.on()
- } else if ( tempDistance <= 0.0 && heaters.first().currentValue("switch") == "on") {
- log.info "Switching helper heaters off"
- state.latestTurnOnAuxHeaters = null
- try {unschedule(turnOnAuxHeaters)} catch(e) {log.error "Ignoring error: ${e}"}
- heaters.off()
- }
- }
- }
- def thermostatModeHandler(evt)
- {
- log.debug "thermostatModeHandler: $evt.value, $settings"
- }
- def changedLocationMode(evt)
- {
- log.debug "Now it's ${new Date(now())}"
- log.debug "changedLocationMode: $evt.value, $settings"
- state.modifiedLatestMode = null
- state.latestDoUpdateTempSettings = now()
- try {runIn(30, doUpdateTempSettings)} catch(e) {log.error "Error scheduling: ${e}"} //give the system a little time to react before checking status
- log.debug "temperature now is: ${thermostat.currentValue("temperature")}"
- log.debug "thermostat state now is: ${thermostat.currentValue("thermostatMode")}"
- }
- def doUpdateTempSettings()
- {
- log.debug "doUpdateTempSettings; runProgramming==${runProgramming}"
- def mode = location.currentMode.name
- state.latestDoUpdateTempSettings = null
- state.latestMode = mode
- state.modifiedLatestMode = null
- if (runProgramming == false || runProgramming=='false') {
- log.debug "Programming run is disabled"
- return
- }
- log.debug "Location mode is ${mode}"
- if (mode.startsWith(mode1)) {
- if (heatingSetpoint1 && heatingSetpoint1 != "") {
- setHeatingSetpoint(['heatingSetpoint': heatingSetpoint1])
- }
- if (coolingSetpoint1 && coolingSetpoint1 != "") {
- setCoolingSetpoint(['coolingSetpoint': coolingSetpoint1])
- }
- } else if (mode.startsWith(mode2)) {
- if (heatingSetpoint2 && heatingSetpoint2 != "") {
- setHeatingSetpoint(['heatingSetpoint': heatingSetpoint2])
- }
- if (coolingSetpoint2 && coolingSetpoint2 != "") {
- setCoolingSetpoint(['coolingSetpoint': coolingSetpoint2])
- }
- } else if (mode3 && mode.startsWith(mode3)) {
- if (heatingSetpoint3 && heatingSetpoint3 != "") {
- setHeatingSetpoint(['heatingSetpoint': heatingSetpoint3])
- }
- if (coolingSetpoint3 && coolingSetpoint3 != "") {
- setCoolingSetpoint(['coolingSetpoint': coolingSetpoint3])
- }
- } else { //away
- if (heatingSetpointAway && heatingSetpointAway != "") {
- setHeatingSetpoint(['heatingSetpoint': heatingSetpointAway])
- }
- if (coolingSetpointAway && coolingSetpointAway != "") {
- setCoolingSetpoint(['coolingSetpoint': coolingSetpointAway])
- }
- }
- // thermostat.poll()
- }
- def scheduleTimer() {
- runEvery5Minutes(keepAlive)
- }
- def setCoolingSetpoint(data) {
- def currentSetpoint = thermostat.currentValue('coolingSetpoint')
- log.debug "setCoolingSetpoint(${data}), current coolingSetpoint = ${currentSetpoint}"
- if (currentSetpoint != data.coolingSetpoint) {
- log.debug "Setting setCoolingSetpoint(${data.coolingSetpoint})"
- thermostat.setCoolingSetpoint(data.coolingSetpoint)
- runIn(30, setCoolingSetpoint, ['data': ['coolingSetpoint': data.coolingSetpoint]])
- }
- }
- def setHeatingSetpoint(data) {
- def currentSetpoint = thermostat.currentValue('heatingSetpoint')
- log.debug "setHeatingSetpoint(${data}), current heatingSetpoint = ${currentSetpoint}"
- if (currentSetpoint != data.heatingSetpoint) {
- log.debug "Setting setHeatingSetpoint(${data.heatingSetpoint})"
- thermostat.setHeatingSetpoint(data.heatingSetpoint)
- runIn(30, setHeatingSetpoint, ['data': ['heatingSetpoint': data.heatingSetpoint]])
- }
- }
- def appTouch(evt)
- {
- log.debug "appTouch: $evt, $settings"
- doUpdateTempSettings()
- }
- def keepAlive()
- {
- log.debug "[Polling] keepAliveLatest == ${state.keepAliveLatest}; now() == ${now()}"
- state.keepAliveLatest = now()
- if (switchSuspendPolling == null || switchSuspendPolling.currentValue("switch") == "off") {
- thermostat.refresh()
- } else {
- log.warn "Polling is suspended"
- }
- if (state.latestMode == null || state.latestMode == mode1 || state.latestMode == mode2 || state.latestMode == mode3) { //Check current mode only if latest set state is not away
- if (!state.latestMode || state.latestMode != location.currentMode.name) {
- log.warn "Double checking: state=${state}"
- if (state.modifiedLatestMode == location.mode) {
- log.warn "Mode has changed to ${location.mode} (checked twice); update settings"
- doUpdateTempSettings()
- } else {
- state.modifiedLatestMode = location.mode
- log.warn "Mode seems to have changed (to ${location.mode}); but let's wait one more polling cycle..."
- }
- } else {
- state.latestMode = location.currentMode.name
- state.modifiedLatestMode = null
- }
- } else {
- state.modifiedLatestMode = null
- }
- }
- // catchall
- def event(evt)
- {
- // log.debug "value: $evt.value, event: $evt, settings: $settings, handlerName: ${evt.handlerName}"
- }
- private toDouble(anObj) {
- if (anObj instanceof String) {
- Double.parseDouble(anObj)
- } else {
- anObj
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement