getDayOfWeek problem


I was designing some rules to trigger lights based on the hour of the day and the day of the week.
While the getHourOfDay part works fine, it seems the getDayOfWeek part is not correctly returning the values.

Meaning: After some time I get wrong values returned for the days (i.e. return value is 5 (5th day) but actually it’s sunday and it should return 7).

If I force a refresh of the rule (opening and saving again), then the returned values are correct again. (i.e. if it’s a friday, it again returns “5” as day of the week).

Is there any way to get rid of this issue?

Here’s the full code of the rule I was using:

var Timer waitSchlafzimmerTimer
var Number day = now.getDayOfWeek

rule "SZ motion Abend"

        Item FF_Bedroom_Sensor_MotionEvent changed from OFF to ON
        logInfo("day", "Der Wochentag wird überprüft: Es ist der " +day + ". Tag der Woche")
        if ((day == 1) || (day == 2) || (day == 3) || (day == 4) || (day == 5)) {
        if ((Holiday1.state == OFF) || (Holiday.state == OFF)){

        logInfo("getHourOfDay","Check ob es zwischen 6:00 und 21:00 ist: " + now.getHourOfDay + " schlägt die Stunde")
        if (now.getHourOfDay() >= 6 && now.getHourOfDay() < 22) {

                        if (waitSchlafzimmerTimer !== null) {
                        logInfo("waitSchlafzimmerTimer","Ein Timer ist schon da ->  nochmal 3 Minuten")
         } else {

                        var DecimalType hue = new DecimalType(29) 
                        var PercentType sat = new PercentType(19) 
                        var PercentType bright = new PercentType(94) 
                        var HSBType light = new HSBType(hue,sat,bright)

                        logInfo("FF_Bedroom_Light3_Color","Jetzt geht das Licht an: " + FF_Bedroom_Light3_Color.state + " ist die Farbe")
                        sendCommand(FF_Bedroom_Light3_Color, light)
                        logInfo("FF_Bedroom_Light2_Color","Jetzt geht das Licht an: " + FF_Bedroom_Light2_Color.state + " ist die Farbe")
                        sendCommand(FF_Bedroom_Light2_Color, light)
                        logInfo("FF_Bedroom_Light1_Color","Jetzt geht das Licht an: " + FF_Bedroom_Light2_Color.state + " ist die Farbe")
                        sendCommand(FF_Bedroom_Light1_Color, light)

                        logInfo("waitSchlafzimmerTimer", "Jetzt mach ich einen 3 Minuten Timer")
                        waitSchlafzimmerTimer = createTimer(now.plusMinutes(3))[|

                        logInfo("FF_Bedroom_Sensor_MotionEvent", "Ich checke ob Bewegung erkannt: " + FF_Bedroom_Sensor_MotionEvent.state + " ist der Status, so schauts aus")
                        if (FF_Bedroom_Sensor_MotionEvent.state ==  ON) {
                        logInfo("waitSchlafzimmerTimer","Weil Bewegung erkannt, Timer neu setzen")
        } else {

                        logInfo("FF_Bedroom_Light3_Color", "Jetzt geht das Licht aus: " + FF_Bedroom_Light3_Color.state + "  ist der HUE Code")
                        sendCommand(FF_Bedroom_Light3_Color, HSBType::BLACK)
                        sendCommand(FF_Bedroom_Light2_Color, HSBType::BLACK)
                        sendCommand(FF_Bedroom_Light1_Color, HSBType::BLACK)

                        logInfo("waitSchlafzimmerTimer", "Ende der Regel -> Timer 0")
                        waitSchlafzimmerTimer = null

Any help here would be appreciated.
My goal is to check if it’s a weekday then trigger lights differently, depending if it’s a weekday or weekend.

As a result of the above, I may get the following in return when 2 rules interact and ask for the day of the week:

2017-11-11 09:54:37.739 [INFO ] [] - Der Wochentag wird überprüft: Es ist der 6. Tag der Woche 
2017-11-11 09:54:37.758 [INFO ] [] - Es wird überprüft ob Wochenende ist: Es ist der  5. Tag der Woche!

thanks a lot for your help upfront,

As you’ve put the ‘getDayOfWeek’ on top as a global variable assignment outside your rule, it’ll only ever get executed when the rule file gets (re)loaded. That’s why.
Move that into your rule and you should be fine.

thanks! :slight_smile: