I’m trying to make use of a Hue temp sensor, bulb and a tp-link wifi socket (attached to a heater) to keep a room within a temperature range when it’s occupied. My rule isn’t working and I’ve not been able to debug using Visual Studio.
I suspect the issue is with local variables and types, but I’m not sure what I’m doing wrong. Grateful for any help.
rule "Room thermostat"
when
Item hue_room_temp changed
then
logInfo("Temperature of room being checked:","Room")
// the current room temperature from a hue sensor
var Number temp = hue_room_temp.state
// the current light status from the hue bulb in the room (if > 0 then it's on)
var Number light_state = room_dimmer.state
// the smartplug (connected to heater) status
val heat_switch = if(room_heat_switch.state == ON) 1 else 0
if ((temp <= 20.0) && (light_state > 0.0)) {
//if temperature is too low and the room is occupied, turn the switch on
sendCommand(room_heat_switch, 'ON')
logInfo("Room temp too low and occupied", ":"+temp.toString)
logInfo("Room light is:","light:"+light_state.toString)
sendTelegram("bot4", "Room heating turned %s, current temp is %s", heat_switch.toString, temp.toString)
}
else {
logInfo("Room temp too high or unoccupied", ":"+temp.toString)
}
if ((temp >= 21.0) && (light_state >0.0)) {
//if temperature is too high and the room is occupied, turn the switch off
sendCommand(room_heat_switch, 'OFF')
sendTelegram("bot4", "Room heating turned %s, current temp is %s", heat_switch.toString, temp.toString)
logInfo("Room temp too high and occupied", ":"+temp.toString)
}
else {
//do nothing
logInfo("Room temp not too high or unoccupied", ":"+temp.toString)
}
end
I tidied up your logic a bit and added some logInfo for testing:
rule "Room thermostat"
when
Item hue_room_temp changed
then
logInfo("Temperature of room being checked:","Room")
// the current room temperature from a hue sensor
var temp = hue_room_temp.state as Number
logInfo("temp", temp.toString)
// the current light status from the hue bulb in the room (if > 0 then it's on)
var light_state = room_dimmer.state as Number
logInfo("light_state", light_state.toString)
// the smartplug (connected to heater) status
val heat_switch = if(room_heat_switch.state == ON) 1 else 0
logInfo("heat_switch", heat_switch.toString)
if ((temp <= 20.0) && (light_state > 0.0)) {
//if temperature is too low and the room is occupied, turn the switch on
room_heat_switch.sendCommand(ON)
logInfo("Room temp too low and occupied", ":" + temp.toString)
logInfo("Room light is:","light:"+light_state.toString)
sendTelegram("bot4", "Room heating turned %s, current temp is %s", heat_switch.toString, temp.toString)
}
else if ((temp >= 21.0) && (light_state > 0.0)) {
//if temperature is too high and the room is occupied, turn the switch off
room_heat_switch.sendCommand(OFF)
sendTelegram("bot4", "Room heating turned %s, current temp is %s", heat_switch.toString, temp.toString)
logInfo("Room temp too high and occupied", ":" + temp.toString)
}
else {
//do nothing
logInfo("Room temp not too high or unoccupied", ":" + temp.toString)
}
end
val heat_switch = if(room_heat_switch.state == ON) 1 else 0
I added this as I may use heat_switch to decide whether to turn the plug off and couldn’t get if ((room_heat_switch.state == ON) {} to work. I take it that I should be able to query the state directly rather than translate to a binary?