How to run a cron job the day before the 1st Wednesday of every month?

Thanks guys. By crossing both of your ideas, I could just run a daily cron job asking if “tomorrow is the 1st Wednesday of the month”. Does anyone know how I could code that in openhab?

A daily cron job is not needed, because you definitely want the message only on a Tuesday.
Just check on every Tuesday wether tomorrows day in month is less or equal to 7. No persistence needed for that.
But what if the calculated Wednesday is a holiday?


@opus, I think the holidays is not a problem because if the first Wednesday is a public holiday, the special garbage would be collected the next day anyway.

You are right, a cronjob on Tuesday only is enough. But I’d need to check if the “tomorrow’s day of month” (not today) is less than 7. Another way is to check if today’s day of month is less than 6 or is the last day of the month.

Do you know how to code that in openhab? I can probably code it in Bash script (which I know better) and run the script via “executeCommandLine” but coding it directly in openhab (if possible) would be more elegant.

As Spiral suggests -
0 0 12 ? * WED *
In the rule check that the date day is less than or equal to 7. Else exit.

From quartz tutorial it looks like there is a way to run a job on the last day of month if it’s Tuesday.
Check out this paragraph:

  • L (“last”) - has different meaning in each of the two fields in which it is allowed. For example, the value “L” in the day-of-month field means “the last day of the month” - day 31 for January, day 28 for February on non-leap years. If used in the day-of-week field by itself, it simply means “7” or “SAT”. But if used in the day-of-week field after another value, it means “the last xxx day of the month” - for example “6L” means “the last friday of the month”. You can also specify an offset from the last day of the month, such as “L-3” which would mean the third-to-last day of the calendar month. When using the ‘L’ option, it is important not to specify lists, or ranges of values, as you’ll get confusing/unexpected results

Never tried it though so I’d be very interested to know if it works…

@Mihai_Badea, good suggestion. But if we take this month as an example, I want the message to be played on Tuesday July 31st but NOT on Tuesday August 7th. So it would imply 2 rules in openhab:

    Time cron "0 30 18 ? * 3#1 *"
	if (today's day_of_month < 6){


    Time cron "0 30 18 L * ? *"
	if (today's weekday = 3){

A more elegant way would be:

    Time cron "0 30 18 ? * TUE *"
	if (tomorrow's day_of_month < 7){

How can I code “tomorrow’s day_of_month < 7” in openhab?

val today = now.getDayOfWeek is what I’m using in one of my rules

@Mihai_Badea is there the same for “tomorrow”?

No, but tomorrow = today + 1, right? :smile:

@Mihai_Badea, indeed but what is the correct syntax for:

val tomorrow = today + 1
val tomorrow_DayOfMonth = tomorrow.getDayOfMonth

I feel sorry for the stupid question, I just don’t know Xbase…

if(now.plusDays(1).getDayOfMonth < 7) 

If you use VSCode with the openHAB extension when you type in “now.” a list of all the methods on now will be listed.


1 Like

Take a look at the Ephemeris binding it provides exactly the awaited functionality. This is an unmerged PR of OH2. I’m currently submitting another PR based on the same principles in ESH

@rlkoshak thank you very much! I imagined it would that simple.

By the way, I see what you mean about typing “now.” and wait for the methods. I am currently using the 1.8.0-designer-linux64bit and I remember that feature but I don’t see it anymore. Can you tell me how I can get it back?

Designer is dead. Use VSCode with the openHAB extension. I provided a link to the instructions to download and configure it above.

I don’t get that in VS code but everything appears to be set up correctly according to the documentation.
Everything else seems to work and auto-complete suggests my own items etc.
I guess it’s a language server problem.

Any ideas?

Many thanks!

I posted this in the VS Code thread but no one responded.

Look in openhab.log and under the Problems panel at the bottom. Are there any errors?

Yes. I didn’t look there (but I know I should have). I’m none the wiser though and didn’t get this error yesterday - only the user-generated choices such as items.

The stuff below is after pressing CTRL + space after if(now.

VS CODE Problems:

[Error - 4:27:31 PM] Request textDocument/completion failed.
  Message: Internal error, please look at the server's logs.
  Code: -32603 

openHAB log:

2018-08-10 16:27:28.990 [ERROR] [ypes.access.impl.AbstractClassMirror] - resource is empty: java:/Objects/org.joda.time.LocalDate
java.lang.IllegalStateException: null
	at org.eclipse.xtext.common.types.access.impl.AbstractClassMirror.getEObject( ~[?:?]
	at org.eclipse.xtext.common.types.access.TypeResource.getEObject( ~[?:?]
	at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.getEObject( ~[?:?]
	at org.eclipse.xtext.common.types.access.impl.IndexedJvmTypeAccess.getIndexedJvmType( ~[?:?]
	at org.eclipse.xtext.common.types.access.impl.IndexedJvmTypeAccess.getIndexedJvmType( ~[?:?]
	at org.eclipse.xtext.common.types.access.TypeResource.resolveJavaObjectURIProxy( ~[?:?]
	at org.eclipse.xtext.common.types.impl.JvmTypeReferenceImplCustom.eResolveProxy( ~[?:?]
	at org.eclipse.xtext.common.types.impl.JvmParameterizedTypeReferenceImpl.getType( ~[?:?]
	at org.eclipse.xtext.xbase.typesystem.internal.ExpressionScope$Scope.getParameterType( ~[?:?]
	at org.eclipse.xtext.xbase.typesystem.internal.ExpressionScope$Scope.appendParameters( ~[?:?]
	at org.eclipse.xtext.xbase.typesystem.internal.ExpressionScope$Scope.getSignature( ~[?:?]
	at org.eclipse.xtext.xbase.typesystem.internal.ExpressionScope$Scope.recordDescription( ~[?:?]
	at org.eclipse.xtext.xbase.typesystem.internal.ExpressionScope$Scope.populateFromParent( ~[?:?]
	at org.eclipse.xtext.xbase.typesystem.internal.ExpressionScope$Scope.ensureInitialized( ~[?:?]
	at org.eclipse.xtext.xbase.typesystem.internal.ExpressionScope$Scope.getAllElements( ~[?:?]
	at org.eclipse.xtext.ide.editor.contentassist.IdeCrossrefProposalProvider.queryScope( ~[?:?]
	at org.eclipse.xtext.ide.editor.contentassist.IdeCrossrefProposalProvider.lookupCrossReference( ~[?:?]
	at org.eclipse.xtext.xbase.ide.contentassist.XbaseIdeContentProposalProvider.createReceiverProposals( ~[?:?]
	at org.eclipse.xtext.xbase.ide.contentassist.XbaseIdeContentProposalProvider.completeXMemberFeatureCall( ~[?:?]
	at org.eclipse.xtext.xbase.ide.contentassist.XbaseIdeContentProposalProvider._createProposals( ~[?:?]
	at org.eclipse.xtext.xbase.ide.contentassist.XbaseIdeContentProposalProvider.createProposals( ~[?:?]
	at org.eclipse.xtext.ide.editor.contentassist.IdeContentProposalProvider.createProposals( ~[?:?]
	at org.eclipse.xtext.ide.server.contentassist.ContentAssistService.createProposals( ~[?:?]
	at org.eclipse.xtext.ide.server.contentassist.ContentAssistService.createCompletionList( ~[?:?]
	at org.eclipse.xtext.ide.server.LanguageServerImpl.lambda$completion$14( ~[?:?]
	at org.eclipse.xtext.ide.server.WorkspaceManager.doRead( ~[?:?]
	at org.eclipse.xtext.ide.server.LanguageServerImpl.completion( ~[?:?]
	at org.eclipse.xtext.ide.server.LanguageServerImpl.lambda$completion$13( ~[?:?]
	at org.eclipse.xtext.ide.server.concurrent.RequestManager.lambda$runRead$2( ~[?:?]
	at java.util.concurrent.CompletableFuture.uniApply( [?:?]
	at java.util.concurrent.CompletableFuture$UniApply.tryFire( [?:?]
	at java.util.concurrent.CompletableFuture$ [?:?]
	at java.util.concurrent.ThreadPoolExecutor.runWorker( [?:?]
	at java.util.concurrent.ThreadPoolExecutor$ [?:?]
	at [?:?]

Thank you for replying.

I’ve seen that occasionally. It usually happens if I’ve had VSCode open for a long time and a restart of VSCode usually fixes it.

If that doesn’t work try restarting OH and see if the error goes away.

1 Like

You’re a star! That fixed it. Not sure why it wasn’t fixed before as I occasionally have had to restart openHAB for back-ups etc.


A post was split to a new topic: Ephemeris binding?