[SOLVED] Thermostat - Looking for cleaner code

Hardware: Intel Q9650 on Dell 0G261D motherboard
OS: Ubuntu 18.04.1 LTS
JRE: OpenJDK 1.8.0_192
OpenHAB 2.5.0

I’m running a z-wave thermostat that overall is working well. However the code isn’t as clean as it could be. I have a number of variables built into the rules:

  1. The house has an “HVAC Mode” (heating, cooling, hold temp, etc…) that is selected input
  2. The “HVAC_Time” variable is adjusted by a cron statement, and changes setpoints based on the time of the day (for example, cooler during the night, when heating)

The scheme works well enough, but in order to see if the heating setpoints have been changed I run a cron statement to trigger the rule the rule that writes the setpoint to the thermostat. A portion of the rule is below, which obviously continues with additional cases until I hit the end statement.

The cron is set for 10 seconds currently for testing and I’d like a regular check of the setpoint inputs. But right now the system sends the update command at that interval and it’s clogging up the logs. Not that it’s a huge problem, but this is just not efficient programming.

Any suggestions on how to clean this up? It works really well and I don’t mind keeping it, but there’s always a better way!

rule "HVAC Assign House Heating Setpoints"
    Time cron "0/10 * * ? * * *" 
    switch HVAC_Time.state {
        case "WEEKEND-DAY": {
            if (House_HVAC_Mode.state == "1") {
                House_Thermostat_Heating_Setpoint.sendCommand(House_Heating_AtHome_Temp.state as Number) 
            else if (House_HVAC_Mode.state == "2") {
                House_Thermostat_Cooling_Setpoint.sendCommand(House_Cooling_Away_Temp.state as Number)
            else if (House_HVAC_Mode.state == "3") {
                House_Thermostat_Heating_Setpoint.sendCommand(House_Hold_Temp.state as Number)
            else if (House_HVAC_Mode.state == "4") {
                House_Thermostat_Cooling_Setpoint.sendCommand(House_Hold_Temp.state as Number)

Check to see what the existing state is before sending a new command; if it is already in the correct state, just don’t.

You might implement that by setting your target into a variable.
You could have a default in case of no case match.
After the switch case, test if target != current and command if needed.

Thanks, @rossko57. I’ve used that technique in a few other instances but since I’m already checking several other states, the “if” levels start getting really deep. It would be great if you could check several states in a single if…

Use && to “and” your boolean expressions. (If you need “or”, use ||)

Thanks @job, that did it (I’d swear I’d tried that in the past and it didn’t work, must have messed it up previously…).
I also discovered that I had to change the Items that hold setpoints from Number to Number:Temperature items, so that they compared similarly to the thermostat input items. But it’s already cleaning up the logs. Thanks all!