Hi Guys,
I am trying to learn Groovy to make an app to control my air-to-air heat pump. I am struggling a bit so I asked ChatGPTs new code version for some assistance, and together we did an app that Hubitat accepted and saved. There is however a tiny flaw. Nothing happens.
If the inside temperature, provided buy a device, is below a certain temperature set by a user variable, the heat pump should start in heat mode and provide heat. If the inside temperature is over a certain temperature, again set using a variable, the heatpump should start in cooling mode and provide cooling air until a threshold temperature is reached, also set via a user variable.
Looking in the logfile it seems the app is picking up the temperature from the temperature device correctly. It also seems to only send one of three commands. I am guessing the syntax is incorrect for sending commands to the RM4 Pro? I can do this using Rule Machine so it is possible to do, but not this way it seems.
All suggestions on why nothing is happening are very welcome and much apreciated!
Log file:
App code:
definition(
name: "Heat Pump Manager 0.2",
namespace: "magnus_s",
author: "magnus_s and ChatGPT",
description: "Manage an air-to-air heat pump based on inside temperature.",
category: "Convenience",
iconUrl: "",
iconX2Url: "",
iconX3Url: ""
)
preferences {
section("Select Devices") {
input "tempDevice", "capability.temperatureMeasurement", title: "Select 'Termometer virtuell - Genomsnittstemperaturer - inomhus 3'", required: true, multiple: false
input "broadlinkDevice", "capability.temperatureMeasurement", title: "Select 'Broadlink Remote 2023 01'", required: true, multiple: false
}
section("Temperature Settings") {
input "heatPumpStartHeatTemp", "decimal", title: "Heat Pump Start Heat Temperature (°C)", required: true, defaultValue: 20.0
input "heatPumpStartCoolTemp", "decimal", title: "Heat Pump Start Cool Temperature (°C)", required: true, defaultValue: 24.0
input "heatPumpStopCoolTemp", "decimal", title: "Heat Pump Stop Cool Temperature (°C)", required: true, defaultValue: 22.0
}
}
def installed() {
log.debug "Installed with settings: ${settings}"
initialize()
}
def updated() {
log.debug "Updated with settings: ${settings}"
unsubscribe()
initialize()
}
def initialize() {
log.debug "Initializing"
subscribe(tempDevice, "temperature", temperatureHandler)
}
def temperatureHandler(evt) {
log.debug "Temperature event received: ${evt.value} from ${evt.device}"
BigDecimal currentInsideTempV = evt.value.toBigDecimal()
log.debug "Current inside temperature: ${currentInsideTempV}"
manageHeatPump(currentInsideTempV)
}
def manageHeatPump(BigDecimal currentInsideTempV) {
BigDecimal heatPumpStartHeatTempV = settings.heatPumpStartHeatTemp?.toBigDecimal() ?: 20.0
BigDecimal heatPumpStartCoolTempV = settings.heatPumpStartCoolTemp?.toBigDecimal() ?: 24.0
BigDecimal heatPumpStopCoolTempV = settings.heatPumpStopCoolTemp?.toBigDecimal() ?: 22.0
if (currentInsideTempV < heatPumpStartHeatTempV) {
log.debug "Current temperature (${currentInsideTempV}) is below heat threshold (${heatPumpStartHeatTempV}). Starting heat pump."
sendBroadlinkCommands(["ON", "MODE_HEAT", "TEMP_24"])
} else if (currentInsideTempV > heatPumpStartCoolTempV) {
log.debug "Current temperature (${currentInsideTempV}) is above cool threshold (${heatPumpStartCoolTempV}). Starting cooling."
sendBroadlinkCommands(["ON", "MODE_COLD", "TEMP_17"])
} else if (currentInsideTempV < heatPumpStopCoolTempV && currentInsideTempV > heatPumpStartHeatTempV) {
log.debug "Current temperature (${currentInsideTempV}) is within range. Stopping heat pump."
sendBroadlinkCommand("OFF")
}
}
def sendBroadlinkCommands(commands) {
commands.eachWithIndex { cmd, idx ->
runIn(idx * 2, sendBroadlinkCommand, [data: cmd])
}
}
def sendBroadlinkCommand(cmd) {
log.debug "Sending command to Broadlink: ${cmd}"
broadlinkDevice.SendStoredCode(cmd)
}
EDIT: I was actually wrong, one command is sent by the RM4 Pro, I just didn't notice it. But only one is sent.
This is what the bit of the rule that caters for sending the commands in Rule Machine looks like (The rule is longer but essentially caters for different condintions when to send different values in the below format).