Store the day of the week in variable form or use it in scripts

I have a scene that when triggered needs to turn on a certain light only on certain days of the week. I have checked java documentation and have created a number item called DayOfTheWeek for testing and a rule to update it at midnight,

rule SetDayOfWeek
when
Time cron “0 0 24 * * ?”
then
DayOfWeek.postUpdate(now.dayOfWeek)
end

However, this does not seem to work, the cron fires but I get an error.

2015-12-01 16:46:00.006 [ERROR] [.o.m.r.i.engine.ExecuteRuleJob] - Error during the execution of rule SetDayOfWeek
java.lang.IllegalStateException: Could not invoke method: org.openhab.model.script.actions.BusEvent.postUpdate(org.openhab.core.items.Item,java.lang.Number) on instance: null
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.invokeOperation(XbaseInterpreter.java:738) ~[na:na]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._featureCallOperation(XbaseInterpreter.java:713) ~[na:na]
at sun.reflect.GeneratedMethodAccessor58.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_31]
at java.lang.reflect.Method.invoke(Method.java:483) ~[na:1.8.0_31]
at org.eclipse.xtext.util.PolymorphicDispatcher.invoke(PolymorphicDispatcher.java:291) ~[na:na]
at org.openhab.model.script.interpreter.ScriptInterpreter.internalFeatureCallDispatch(ScriptInterpreter.java:69) ~[na:na]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateMemberFeatureCall(XbaseInterpreter.java:549) ~[na:na]
at sun.reflect.GeneratedMethodAccessor60.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_31]
at java.lang.reflect.Method.invoke(Method.java:483) ~[na:1.8.0_31]
at org.eclipse.xtext.util.PolymorphicDispatcher.invoke(PolymorphicDispatcher.java:291) ~[na:na]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:218) ~[na:na]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateBlockExpression(XbaseInterpreter.java:321) ~[na:na]
at sun.reflect.GeneratedMethodAccessor63.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_31]
at java.lang.reflect.Method.invoke(Method.java:483) ~[na:1.8.0_31]
at org.eclipse.xtext.util.PolymorphicDispatcher.invoke(PolymorphicDispatcher.java:291) ~[na:na]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:218) ~[na:na]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.evaluate(XbaseInterpreter.java:204) ~[na:na]
at org.openhab.model.script.internal.engine.ScriptImpl.execute(ScriptImpl.java:59) ~[na:na]
at org.openhab.model.rule.internal.engine.ExecuteRuleJob.execute(ExecuteRuleJob.java:55) ~[na:na]
at org.quartz.core.JobRunShell.run(JobRunShell.java:213) [quartz-all-2.1.7.jar:na]
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:557) [quartz-all-2.1.7.jar:na]
Caused by: java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_31]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_31]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_31]
at java.lang.reflect.Method.invoke(Method.java:483) ~[na:1.8.0_31]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.invokeOperation(XbaseInterpreter.java:729) ~[na:na]
… 23 common frames omitted

Has anyone made something similar work?

Hi

How about using the item DayOfTheWeek instead of DayOfWeek in your rule, if that’s the name of your item :wink:

Regards
Dieter

lol, I realize that was a typo, the item name is actually DayOfWeek, the (now.dayOfWeek) part is what’s not working, If I replace that with an actual variable or integer it works fine.

Maybe you should give it a try nevertheless. There might be some problems with case-sensitve variable/item/function names.
Otherwise you should take into account that now.dayOfWeek doesn’t return a Number but a DateTime.Property. So the error does not mean a NullPointerException but a ClassCastException. If there are still problems I have to check my rules when I’m back at home for the solution where I’ve used similar functions.

Regards
Dieter

It is now.getDayOfWeek :wink:

THANK YOU!, this works and is exactly what I needed. Is there a reference where that was found? I did not see that in the java docs anywhere.

It’s part of JodaTime: http://www.joda.org/joda-time/apidocs/index.html

Hello everyone.

I have been reading some Jodatime docs trying to get the day name (Monday, Tuesday, etc) into a string variable but haven’t succeeded.

var DateTime dateTime = new DateTime()	
var String strT = dateTime.dayOfWeek().getAsText();

logInfo("TEST",now.dayOfWeek().getAsText()) // Did not work
logInfo("TEST",strT) // Neither :(

I believe it should work, and its simple than the ther methods since its straightforward.
Do you see something obvious that I’m missing?

Thanks, regards

PS: Some docs: Joda Docs

I’m using:

var Heute = ""
switch now.getDayOfWeek{
        case 1: Heute = "MO"
        case 2: Heute = "DI"
        case 3: Heute= "MI"
        case 4: Heute= "DO"
        case 5: Heute= "FR"
        case 6: Heute= "SA"
        case 7: Heute= "SO"
    }

without problems.
HTH,
-OLI

Thanks Oli. That works, but I’m trying to understand why the above doesn’t since its a plain example from joda docs.

Ahhh…curiosity: brought us to the moon, but also sent many brilliant minds right into the padded walls of a mental asylum :wink:
I think there are some threads explaining that even though OpenHABs and yodatimes datetime formats share the same names, they are handled totally different internally and can not be interchanged without extra effort.

This works fine for me:

var String strMonth= LocalTime_Date.state.format("%1$ta") //returns Di
var String strMonth= LocalTime_Date.state.format("%1$tA") //returns Dienstag

… assuming your system language is set to DE_…

cf. http://www.java2s.com/Tutorials/Java/Java_Format/0120__Java_Format_Dates_Times.htm

1 Like
logInfo(logName,now.getDayOfWeek)

What should that return