Heating rule doing me crazy

Hey guys,
after two days of brainstorming and setting up rules like this and that and so, I need your advice.
What I’m trying is to define a rule, which controls my heating depending on the following basic information:

  • set temp to 22°C between 6 and 8 am or 5 and 7pm but set this in dependence of the outside temperature

May last try is like that:

var Number hour_Bath = now.getHourOfDay

rule “Test Rule2”
when
//Item Temperature received update or
//Item Date received update or
Time cron “0 0 * * * ?” or
System started
then
logInfo(“Test Rule2”,“Start Test Rule2”)

if((Temperature.state as Number) <= 14){
    if((hour_Bath >= 17 && hour_Bath <= 19) || (hour_Bath >= 6 && hour_Bath <= 8)){
        var heat=22.0
        logInfo("Test Rule2", "Die Heizung im Bad wird auf "+heat + "°C gestellt.")
        sendNotification("notificationaddress", "Die Heizung im Bad wird auf "+heat + "°C gestellt.")
        Bath_Control_Mode.sendCommand(heat)
    }

    else{
        var heat=4.5
        logInfo("Test Rule2", "Außerhalb der Heizzeiten, wird die Heizung deaktiviert.")
        sendNotification("notificationaddress", "Außerhalb der Heizzeiten, wird die Heizung deaktiviert.")
        Bath_Control_Mode.sendCommand(heat)
    }
}
else if((Temperature.state as Number) >= 14.01){
        if((hour_Bath >= 17 && hour_Bath <= 19) || (hour_Bath >= 6 && hour_Bath <= 8)){
            logInfo("Test Rule2", "Es ist wärmer als 14°C. Die Heizung wird trotz Heizzeit nicht aktiviert.")
            sendNotification("notificationaddress", "Es ist wärmer als 14°C. Die Heizung wird trotz Heizzeit nicht aktiviert.")
        }
    
}

end

What I figured out is that the rule will fire an info to log after “system started”. But not between 6 and 8pm or 5 and 7pm.
If I tried to set the rule in the “when” part like this:
Item Temperature received update
The rule will fire an logentry every update of item Temperature.
Is there a better way to get this rule running with my basic conditions? And do you think that the if conditions with the timeframes are correct?

BR and thanks, Alex

The hour_Bath variable is outside of the rule. It will be set when the rule file is loaded and never change. If you move it into your rule, it will update every time the rule runs.

You are also checking the times twice. Instead, just check the time once and move the temp check inside. You also don’t need the ‘else if’… is the temp is not <= 14, then it is > 14, so you can just use an else.

Yes.
Once you’ve sorted out your time periods, you can make the actions (and logging) more sophisticated.

...
if (Bath_Control_Mode.state != heat) {
   Bath_Control_Mode.sendCommand(heat)
   logInfo("Test Rule2", "Changing mode to " + heat.toString)
}

You are correct that there isn’t a lot of point in triggering the rule from periodic cron - if the temperature hasn’t changed, there’s nothing to do.

1 Like

Thanks both for your help. I have done some improvements to the rule and it works pretty well.
So but I have an additional quastion related to that rule, where I stuck.

I have changed my rule to the following:

rule "Test Rule1"

when 

    Item Temperature received update or

    //Item Date received update or

    //Time cron "0 0 * * * ?" or

    System started

then

    var Number hour_Bath = now.getHourOfDay

    var heat=22.0

    var heat_setback=4.50

    

    if(((hour_Bath >= 17) && (hour_Bath < 19)) || ((hour_Bath >= 6) && (hour_Bath < 8))){

        if((Temperature.state as Number) <= 14){

            //var heat=22.0

            if (Bath_Control_Mode.state != heat){

                //var heat=22.0

                logInfo("Test Rule1", "Bad: Die Heizung im Bad wird auf "+heat + "°C gestellt.")

                Bath_Control_Mode.sendCommand(heat)

                //logInfo("Test Rule1", "Changing mode to " + heat.toString)

            }

            else{

                loginfo("Test Rule1", "Bad: Die Heizung ist bereits auf "+heat + "°C gestellt.")

            }}

        else{

            logInfo("Test Rule1", "Bad: Es ist wärmer als 14°C. Die Heizung wird trotz Heizzeit nicht aktiviert.")

            logInfo("Test Rule1", "Bad: Die Heizung ist auf "+Bath_Control_Mode +"°C gestellt.")

        }

        }    

    else{

        logInfo("Test Rule1", "Bad: Außerhalb der Heizzeiten, wird die Heizung deaktiviert.")

        //sendNotification("notificationaddress", "Bad: Außerhalb der Heizzeiten, wird die Heizung deaktiviert.")

        Bath_Control_Mode.sendCommand(heat_setback)

    }

end

So, what I’m looking now is an additional “if” or “else if” to check during second and third “else” if “Bath_Control_Mode” = heat_setback, and if not, set it to heat_setback.
What do you think or where do you think, does I have do change/adopt my rule?

Not sure what is stopping you adding more.

Not sure what you are trying to do exactly, you might find “else if” construction useful?

if (condition X) {
   // do this
} else if (condition Y) {
   // do that
} else {
   // neither X nor Y were true
   // do something else
}
// only one of three sections will be executed