[SOLVED] Please help validate my rule - all advice accepted

The rule below is aimed at changing the current thermostat set point depending on the time of day. I just want to make sure that I’m using the right method to obtain the results I’m looking for.

Have I used best practice or is there an alternative that I should be using. I will be building on this simple rule over time, but this is where I’d like to make a start.

rule "Change Heating SetPoint"
when
	Item Time_Of_Day changed
then
	var newSetpoint = -1 

	if(Time_Of_Day.state == "MORNING") newSetpoint = Heating_Setpoint_Morning.state as Number
	if(Time_Of_Day.state == "DAY") newSetpoint = Heating_Setpoint_Day.state as Number
	if(Time_Of_Day.state == "EVENING") newSetpoint = Heating_Setpoint_Evening.state as Number
	if(Time_Of_Day.state == "NIGHT") newSetpoint = Heating_Setpoint_Night.state as Number
	if(Time_Of_Day.state == "BED") newSetpoint = Heating_Setpoint_Bed.state as Number
	logInfo("Heating","New Setpoint: " + newSetpoint)
	if(newSetpoint != -1) {
		Group_SetPoint_Command.sendCommand(newSetpoint)
	}
end

This looks good to me.

Your other option would be to use a switch statement in place of the multiple if statements. That also would allow you to have a default case where you can log an informational message due to an unknown or unexpected Time_Of_Day state.

And, you need to ensure that your Heating_Setpoint_* item states are initialized to their numeric value before the rule executes.

Lastly, you could explicitly declare your var as a Number (e.g. var Number newSetpoint = -1) but this is a matter of personal preference, as it is not required by the language. I tend to explicitly type my variables because it helps my poor memory when I come back some time later to edit the rule. :wink:

Thanks for the quick response. Using the switch statement is this the right syntax?

rule “Change Heating SetPoint”
when
Item Time_Of_Day changed
then
var Number newSetpoint = -1

switch Time_Of_Day{
	case Time_Of_Day.state == "MORNING": newSetpoint = Heating_Setpoint_Morning.state as Number
	case Time_Of_Day.state == "DAY": newSetpoint = Heating_Setpoint_Day.state as Number
	case Time_Of_Day.state == "EVENING": newSetpoint = Heating_Setpoint_Evening.state as Number
	case Time_Of_Day.state == "NIGHT": newSetpoint = Heating_Setpoint_Night.state as Number
	case Time_Of_Day.state == "BED": newSetpoint = Heating_Setpoint_Bed.state as Number
    }
logInfo("Heating","New Setpoint: " + newSetpoint)
if(newSetpoint != -1) {
	Group_SetPoint_Command.sendCommand(newSetpoint)
}

end

I haven’t actually validated this, so there may be a typo somewhere.

BTW, VS Code with the openHAB extension will help greatly with finding syntax errors in rules. Although it occurs to me that your rule above is syntactically correct.

rule “Change Heating SetPoint”
when
    Item Time_Of_Day changed
then
    var Number newSetpoint = -1

    switch Time_Of_Day.state.toString {
        case "MORNING": 
            newSetpoint = Heating_Setpoint_Morning.state as Number
        case "DAY": 
            newSetpoint = Heating_Setpoint_Day.state as Number
        case "EVENING": 
            newSetpoint = Heating_Setpoint_Evening.state as Number
        case "NIGHT": 
            newSetpoint = Heating_Setpoint_Night.state as Number
        case "BED": 
            newSetpoint = Heating_Setpoint_Bed.state as Number
        default:
            logInfo("Heating", "Unknown or unexpected Time_Of_Day: {}", Time_Of_Day.state)
    }
    logInfo("Heating","New Setpoint: " + newSetpoint)
    if(newSetpoint != -1) {
        Group_SetPoint_Command.sendCommand(newSetpoint)
    }
end

Thanks once again, I’ll take a look at VS and trail this new rule. I’m quite positive that I’m now making the right progress.

Regards,

Garry

1 Like