Help with rule from OH2.5 to OH3.1

Hi folks,

I’ve a rule that was working with OH2.5, but now I have some issues with OH3.1 It depends most probably on the new class ZonedDateTime.

I’m trying to trigger some automation depending on day/time.

I’ve tried with logs, in order to check what’s happening, but I can’t see anything (so there is a problem before the log :))

Any clue?

rule "A2A Energy Timeslot"
when
    Time cron "0 0/1 * * * ?" or
    //Time cron "0 0 0/1 1/1 * ? *" or
    System started
then
   if (((now.getDayOfWeek.getValue == 1) || (now.getDayOfWeek.getValue == 2) || (now.getDayOfWeek.getValue == 3) || (now.getDayOfWeek.getValue == 4) || (now.getDayOfWeek.getValue == 5)) && (LocalTime_Holiday.state == OFF)) {
        val int thisHour = now.getHour()
        val int thisMinute = now.getHour() * 60 + now.getMinute()
        //val int thisHour = now.getHourOfDay
        //val int thisMinute = now.getMinuteOfDay
        logInfo("test", "thisHour=" + thisHour + " thisMinute=" + thisMinute)
        switch thisHour {
            case thisHour < 7: {
               Auto_Energy_Timeslot.postUpdate(3)
               Auto_AirConditioner.postUpdate(ON)
               Auto_Boiler.postUpdate(ON)
            }
            case thisHour == 7: {
                Auto_Energy_Timeslot.postUpdate(2)
                Auto_AirConditioner.postUpdate(ON)
                Auto_Boiler.postUpdate(ON)
            }
            case (thisHour > 7 && thisHour < 19): {
                Auto_Energy_Timeslot.postUpdate(1)
                Auto_AirConditioner.postUpdate(OFF)
                Auto_Boiler.postUpdate(OFF)
            }
            case (thisHour >= 19 && thisHour < 21): {
                Auto_Energy_Timeslot.postUpdate(2)
                Auto_AirConditioner.postUpdate(OFF)
                Auto_Boiler.postUpdate(OFF)
            }
            case (thisHour >= 21 && thisMinute < 21*60+30): {
                Auto_Energy_Timeslot.postUpdate(2)
                Auto_AirConditioner.postUpdate(ON)
                Auto_Boiler.postUpdate(OFF)
            }
            case (thisMinute >= 21*60+30 && thisHour < 23): {
                Auto_Energy_Timeslot.postUpdate(2)
                Auto_AirConditioner.postUpdate(ON)
                Auto_Boiler.postUpdate(ON)
            }
            case thisHour >= 23: {
                Auto_Energy_Timeslot.postUpdate(3)
                Auto_AirConditioner.postUpdate(ON)
                Auto_Boiler.postUpdate(ON)
            }
        }
    }
    else if ((now.getDayOfWeek.getValue == 6) && (LocalTime_Holiday.state == OFF)) {
        //val int thisHour = now.getHourOfDay
        //val int thisMinute = now.getMinuteOfDay
        val int thisHour = now.getHour()
        val int thisMinute = now.getHour() * 60 + now.getMinute()
        switch thisHour {
            case thisHour < 7: {
                Auto_Energy_Timeslot.postUpdate(3)
                Auto_AirConditioner.postUpdate(ON)
                Auto_Boiler.postUpdate(ON)
            }
            case (thisHour >= 7 && thisHour < 10): {
                Auto_Energy_Timeslot.postUpdate(2)
                Auto_AirConditioner.postUpdate(ON)
                Auto_Boiler.postUpdate(ON)
            }
            case (thisHour >= 10 && thisHour < 21): {
                Auto_Energy_Timeslot.postUpdate(2)
                Auto_AirConditioner.postUpdate(OFF)
                Auto_Boiler.postUpdate(OFF)
            }
            case (thisHour >= 21 && thisMinute < 21*60+30): {
                Auto_Energy_Timeslot.postUpdate(2)
                Auto_AirConditioner.postUpdate(ON)
                Auto_Boiler.postUpdate(OFF)
            }
            case (thisMinute >= 21*60+30 && thisHour < 23): {
                Auto_Energy_Timeslot.postUpdate(2)
                Auto_AirConditioner.postUpdate(ON)
                Auto_Boiler.postUpdate(ON)
            }
            case thisHour >= 23: {
                Auto_Energy_Timeslot.postUpdate(3)
                Auto_AirConditioner.postUpdate(ON)
                Auto_Boiler.postUpdate(ON)
            }
        }
    }
    else if ((now.getDayOfWeek.getValue == 7) || (LocalTime_Holiday.state == ON)) {
        //val int thisHour = now.getHourOfDay
        //val int thisMinute = now.getMinuteOfDay
        val int thisHour = now.getHour()
        val int thisMinute = now.getHour() * 60 + now.getMinute()
        switch thisHour {
            case thisHour < 10: {
                Auto_Energy_Timeslot.postUpdate(3)
                Auto_AirConditioner.postUpdate(ON)
                Auto_Boiler.postUpdate(ON)
            }
            case (thisHour >= 10 && thisHour < 21): {
                Auto_Energy_Timeslot.postUpdate(3)
                Auto_AirConditioner.postUpdate(OFF)
                Auto_Boiler.postUpdate(OFF)
            }
            case (thisHour >= 21 && thisMinute < 21*60+30): {
                Auto_Energy_Timeslot.postUpdate(3)
                Auto_AirConditioner.postUpdate(ON)
                Auto_Boiler.postUpdate(OFF)
            }
            case (thisMinute >= 21*60+30): {
                Auto_Energy_Timeslot.postUpdate(3)
                Auto_AirConditioner.postUpdate(ON)
                Auto_Boiler.postUpdate(ON)
            }
        }
    }
end

thanks for any suggestion
Andrea

You can add more logs temporarily to investigate problems like this.

...
then
   logInfo("diag","My rule was triggered")
   // now what is that if() going to be looking at?
   logInfo("diag", "Some Item state " + LocalTime_Holiday.state.toString)
   logInfo("diag", "Some date derived info " + now.getDayOfWeek.getValue.toString)
   if (((now.getDayOfWeek.getValue == 1) || (now.getDayOfWeek.getValue == 2) || (now.getDayOfWeek.getValue == 3) || (now.getDayOfWeek.getValue == 4) || (now.getDayOfWeek.getValue == 5)) && (LocalTime_Holiday.state == OFF)) {
   ...

This post looks relevant though

Logs are generated for a reason. They tell us things about why a rule failed to run. Without them we are left guessing.

See DateTime Conversion (openHAB 3.x) and make sure to click on the links.

Without seeing the logs I can offer nothing more.

But there is a lot not to like about this rule beyond the fact that it doesn’t work. Why update those three Items every minute? Presumably at least two of these Items actually control something. Posting an update to Auto_Boiler isn’t going to actually turn the boiler on. It’s just going to make OH think that it’s on.

This rule appears to be implementing a problem the Design Pattern: Time Of Day is intended to solve, and it does so without needing to run a rule 1440 times a day when it will only do something different seven of those runs. That means the rule is only doing something useful < .5% of the times it runs.

1 Like

thanks God we have logs :slight_smile:

Now it works like a charm, thank you :slight_smile:

now.getDayOfWeek.getValue.intValue did the trick :slight_smile:

Andrea

it was just for testing :slight_smile: but yeah, I need to work on the rule. Apologies if my level in doing rules is very low :frowning: I’ll work on it :slight_smile: