Heating weekly schedule - more efficient way to implement this

It works with any CalDAV compliant server. For the “no cloud services” types, OwnCloud provides a self hosted alternative. I think NextCloud supports CalDAV too.

I’ll second Markus’s statement. It needs to make sense to you. But since you posted, lets look at the code as written and see if there are some techniques we can use to improve it. As with lots of things, “improvement” is in the eye of the beholder.

The first thing that comes to mind is to move the time calculations (i.e. what day is it) to another Rule. See [Deprecated] Design Pattern: Time Of Day for an example. Your version of the Rule will be a bit different as you are only calculating the day of week, not a time of day. So you just need to run a smidgen after midnight.

Item:

String DayType

Rule:

rule "Calculate the type of day"
when
    Time Midnight
then
    Thread::sleep(500) // normally I don't recommend sleeps, but it is probably OK here.
    var dayType = "WEEKDAY"
    switch(now.getDayOfWeek) {
        case 5:  dayType = "FRIDAY"
        case 6: dayType = "SATURDAY"
        case 7: dayType = "SUNDAY"
    } 

    if(DayType.state.toString != dayType) DayType.postUpdate(dayType)
end

This lets your heating Rule a bit more self documenting and it lets you use DayType in other Rules rather than needing to repeat the same code.

Next, this Rule looks like it has the same code repeated over and over for the most part so apply Design Pattern: How to Structure a Rule to avoid some of this repeated code.

We can also take advantage of Design Pattern: Associated Items to avoid some of the duplicated code. What this will have you do is put your Items into a Group and change the names of your Items so they can be calculated in the Rule. So instead of having a separate if/else statement for each room you can have one loop that handles all three.

Finally, rather than having the Rule trigger once every five minutes it would be better to trigger the rule based on changes or other events that might change the heating state (time of day, presence, changing temperatures).

I don’t have time to do this all my self right now but if you run into trouble feel free to ask.

1 Like