# How to optimize complex heating rules

Hi There,

since I’m using openHAB I’m fighting with way to complex rules for heating control in my house. These rules have two essential problems:

1. They are so complex, that every change leads to a lot of pain. There are a lot of nested if-else constructs - the deepest one has 4 levels.
2. Most of the rules are time-based, so that I need multiple items for the heating mode in one room to allow an manual overdrive of the calculated heating mode (f.e. party mode).

My heating rules are based on the following information:

• Room
• Time
• Outdoor Temperature
• Presence
• Holiday (I’ don’t work but I’m not traveling around)
• Vacation (I’m traveling around so I’m not at home at all)

I’ve tried to add all these parameters to an excel file but even this is to complex.

With this question I don’t ask for specific implementation details. I’m asking for others that have the same problems and have successfully solved them.

Are there “best practices” for such problems? Is there e a way to avoid multiple Items for a single room?

Bests
Pascal

Is usually tend to nest IF ELSE statements, but for readability this is a lot more complicated.
Have you thought about drawing a state diagram and from that to create a state machine?
That helped me with my garden irrigation.

On the other hand instead of

``````if (condition_a and condition_b and condition_c...)
{
;do something
}
``````

i’m no using sometimes:

``````if (!condition_a)
{
return;
}
``````

to stop the rule at that position.
It improves readability quite a lot especially when the alternative would be having nested IF statements

Is a good tutorial for a pretty comprehensive approach.

Design Pattern: How to Structure a Rule provides some help perhaps for dealing with the nested if/elses. For example, you can check for Holiday or Vacation mode up front and exit the Rule which eliminates one set of nesting.

But until you can build up the state machine and fully understand what you want to have happen for all of your conditions then you will never be able to build the code. So maybe draw a state diagram (see A State Machine Primer with HABlladin, the openHAB Genie and Washing Machine State Machine for examples) to map out all the conditions and appropriate states. Once it is all mapped out your Rule will be a bunch of same level if/else statements or switch statements.

This is the standard approach to a problem like this in programming.

THX! I’ve checked out both articles. In fact, my current implementation is a mixture from both patterns. So obviously there seems to be no way to create a better solution using openHAB.

I’ll think about storing the configuration in an external file and building a generic implementation, so that I only have to change the config file and not the code. Maybe with this approach I can stop writing duplicated code…

Please post it as a tutorial or a DP if you get it to work. Good luck!