[SOLVED] Rule to maintain temperature of room within bounds

Hi,

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.

Code is below.
Error is:

ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Room thermostat': Script interpreter couldn't be obtain

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
  • Platform information:
    • Hardware: Raspberry pi 3
    • OS: Raspbian
    • Java Runtime Environment: 1.8.0_171
    • openHAB version: 2.4.0~20180609095339-1

Try:
sendCommand(room_heat_switch, ON)

That’s not the problem.
The problem is that the parser looks dead…

@tmn103
Please comment out all the rule and write a small dummy rule in the SAME file
For example:

rule "dummy"
when
    Time cron "0/10 * * ? * * *" // Every 10 seconds
then
    logInfo("DUMMY", "TEST")
end

When you save the file, what do you see in the logs?

Thanks for responses.

I get the following (repeating every 10s):

[INFO ] [eclipse.smarthome.model.script.DUMMY] - TEST

Ok,
So the parser works. Pfweew

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

That seems to have got it, thanks!

Just on this line:

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?

Yes you can. 1 and 0 are not the booleans for java. true and false are.

Just do:

val OnOffType heat_switch = room_heat_switch.state

Great. Thanks for your help.

Please mark the thread as solved, thanks.