How to get "Weekday" in a rule which works?

I am using in OH 4M3 a rule which simply should check the day of a week. For some reason I do get an error message. Here my rule:

rule "Einschlafmusik"


when
    Item PowerSqueezebox_chn3 received command
then
	
    if(receivedCommand == ON) {
		val day = now.getDayOfWeek.getValue()
		
	    if (day == 1)
		{
			SqueezeBox_Player_Play_a_Favorite.sendCommand("0")
			SqueezeBox_Player_Volume.sendCommand(11)
		}
		else (day == 7)
		{
			SqueezeBox_Player_Play_a_Favorite.sendCommand("15")
			SqueezeBox_Player_Volume.sendCommand(15)
			logInfo("Montag - Z2_Regen3")
		}
        
        
    } else {
        SqueezeBox_Player_Stop.sendCommand("ON")
    }
end

and the error:


[INFO ] [el.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model 'musik.rules', using it anyway:
This expression is not allowed in this context, since it doesn't cause any side effects.
[ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'musik-1' failed: An error occurred during the script execution: index=1, size=1 in musik

Can anybody help pls?

Try
now.getDayOfWeek.toString()

I use a virtual.items
String vDayOfWeek "Current Day of Week [%s]"

Rule:

rule "00_00"
when
        Time cron "0 0 0 ? * * *" // 00:00 
then 
        vDayOfWeek.sendCommand(now.getDayOfWeek.toString())
        logInfo("System", "DAY=" + now.getDayOfWeek)
end

Other rule:

if ((vDayOfWeek.state.toString != "SATURDAY") && (vDayOfWeek.state.toString != "SUNDAY")) {
// do stuff
}

I use the UI and ECMAScript 262 (OH4)

var datetoday = new Date();
var numberofweek = datetoday.getDay();

if ((numberofweek != 0) || (numberofweek != 6)){ 
console.log("It is a weekday")
}

I just run the rule every day.
I am sure there are many better ways as well that work.

In my openhab3 installation I have a rule with

if (now.getDayOfWeek.getValue == 7) SqueezerKueche_PlayaFavorite.sendCommand (0)
		else SqueezerKueche_PlayaFavorite.sendCommand (7)

and this works for me.
Greets

There are a lot of good answers here. I just want to point out that for any questions about how ZonedDateTimes work in Rules DSL see ZonedDateTime (Java SE 17 & JDK 17).

In JS Scripting see Reference | js-joda.

What OP has above should work. The getValue() returns an int representation of the day of the week.

Don’t forget about Ephemeris and the isWeekend Action. Actions | openHAB

if(isWeekend()){

In JS Scripting the Ephemeris call would be

if(actions.Ephemeris.isWeekend()){

To get the day of the week using the Helper Library:

var numberofweek = time.toZDT().dayOfWeek().value();

Also, don’t forget that you have Time based conditions in MainUI Rules where you can limit when rules run based on days of the week or Ephemeris.

Now, back to the original post…

Are you sure the error is coming from this rule? The error indicates trying to access something from a List or an array out of bounds and there is nothing like that in this rule.

don’t you need an ‘if’ as in “else if (day == 7)”?

1 Like

Very sharp eye. Yes indeed an if is required here.

Good point! Thanks

If @jswim788 was the root cause of the problem, the solution should go to that post, not mine.