Time problem in rule

  • Platform information:
    Raspberry pi 3 with latest openhabian 3

I have this rule for lowering my temperature between 00 and 5.30, but the if-condition for setting the low temp newer turns true ?? What am I doing wrong here ?

// This rule is manually generated !

// Imports
import org.openhab.core.library.types
import org.openhab.core.persistence
import org.openhab.model.script.actions
import org.openhab.core.model.script
import java.time.ZonedDateTime

// Global Variables and values
var Number Thermostat = 0
var Number OldThermostat = 0
val Lowtemp = 19
val HighTemp = 23
val StartLowTempTime = ZonedDateTime.now().with(LocalTime.MIDNIGHT).plusHours(0).plusMinutes(0)
val StopLowTempTime = ZonedDateTime.now().with(LocalTime.MIDNIGHT).plusHours(5).plusMinutes(30)


rule "Nattemp"
when
    Time cron "0 0/30 * * * ?"
then
        OldThermostat = Thermostat

        if (now.isAfter(StartLowTempTime) && now.isBefore(StopLowTempTime))
        {
                Thermostat = LowTemp
        }
        else
        {
                if(HolidayMode.state == OFF)
                {

                        Thermostat = HighTemp
                        logInfo("heating","Hightemp is set")
                }
        }
        var triggerDevices = gThermostats.members
        logInfo("heating","Oldthemostat: {}",OldThermostat)
        logInfo("heating","Thermostat: {}",Thermostat)
        if (OldThermostat != Thermostat){
                logInfo("heating","Rule nattemp has set temp to: {} degrees",Thermostat)
                triggerDevices.forEach [ i |
                       sendCommand(i, Thermostat)
                ]
        }
end

Wouldn’t it be easier to have two rules? One is getting triggered at 0:00am and sets the low temp, and the other rule gets triggered at 5:30am and sets the other temperature but only if you are not in Holiday mode?

Reminders

Please don’t keep opening new threads for the same rule.

Sorry for the double opening! @Sascha_Billian - I did as you proposed and made two rules, but would still like to know what is wrong with the time comparisson I am doing in the old rule.

StartLowTempTime gets populated when the file is loaded, not when the rule runs. After a day, no matter what happens, the if statement will always be false because any time is going to be after 5:30 am yesterday or the day before.

This rule, at best, could only ever work correctly for one day. Move those variables inside the rule, not as globals, as the post @rossko57 linked to says. Why does it not work even for that one day? :man_shrugging: We’d need more information, like what is now, StartLowTempTime and StartLowTempTime when the rule runs and fails to do what it’s supposed to.

You are not doing this here but be careful when the start time is before midnight and the end time is after midnight. When that happens you have to be careful with the day. For example, if your start time were 23:00 and end time 05:00, between 23:00 and 00:00 you have compare between 23:00 today and 05:00 tomorrow. After 00:00 you need to compare to 23:00 yesterday and 05:00 today.

There’s a fantastic project here that allows you to set schedules for items that you might find useful.