[Solved] Why doesn't my cron rule work?

I’ve been banging my head against the wall all day trying to get a timed rule to work. I’m guessing I’ve got something wrong in my syntax. Here’s the rule:

import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import java.lang.Math
import java.util.Calendar
import java.util.Date
import java.util.TimeZone
import java.text.SimpleDateFormat

rule "Test"
when
	Time cron "0 17 17 1/1 * ? *"
then
	sendCommand(Test_Number, 255)
end

and here’s the device:

Number Test_Number "Test Number [%s]" <wind> (Living,HVAC)

I used Cronmaker website to generate the cron information. I am assuming that this reads like this:

At 17:17, change Test_Number to 255

This does not work nor do any of my other cron timed events. I included my includes but I’m not sure I have or need something to make cron work.

Can someone please clue me in to what I am doing wrong. I am running OH on Win 7 if that maters. Thanks!

without much of any other idea, I would say Windows does not have cron running like any Linux system would. At least this is what I would figure out first.

E.g have a look here: http://cronw.sourceforge.net/

Thanks for the tip. I wasn’t sure if cron was actually being used.

Does any other local scheduling options exist outside of cron?

Neither am I – new to openHAB as well --, but what else would ‘consume’ the cron parameter string?
Let us know how you went, and if it solved the problem, feel free to tick the ‘tick’-box.

Openhab 1 uses the Quartz Java library for scheduling cron triggers.

http://www.quartz-scheduler.org/documentation/quartz-2.1.x/tutorials/tutorial-lesson-06

Thanks steve1. Can you spot why it’s not working? I think my cron statement is formed correctly. Are my includes OK? Sorry but very me to OH. Thanks

Try adding:
import org.joda.time.*

And if you want to send that command every day, every week, every month, … you could use

Time cron "0 17 17 * * ?"

Are there any errors in the logs, when the rule is executed?

Perhaps, try to log something yourself with logInfo(…). Is this shown in the logs?

@sihui, thanks… I did try that import last night and it didn’t make a difference.

@larshendrikkorte, I tried adding ‘logInfo(“Mydebug”, “Write to log if cron job runs”)’ to my when condition and it did not write out the log. It’s like cron is not working.

Can anyone suggest another way to test this? I think my example is very basic but it will not run.

Are there any other options besides cron? I know that there are statements like ‘is midnight’ or ‘is noon’, but are there others? Can I use ‘is 17:17’?

Here is another working basic rule with all my imports, maybe it helps:

import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import org.java.math.*
import org.joda.time.*

rule "Fountain last OFF"
when   
        Time cron "0 1 22 * * ?"
then
        Licht_UG_Fountain.sendCommand(OFF)
        logInfo("Brunnen aus", "Brunnen gestoppt")
end

@sihui Thank you for posting this. I made a separate rules file (I currently have them all in one big rules file) using your settings and it worked. I don’t know what the problem is with the one I’m using but I’m guessing it’s the java.util.Date or something. Thanks again.

Just to complete the explanation, Quartz does not strictly follow the standard cron format so websites like cronmaker may not generate correctly formatted cron strings.

In Quarts the fields are (from the Rules wiki page):

  1. Seconds
  1. Minutes
  2. Hours
  3. Day-of-Month
  4. Month
  5. Day-of-Week
  6. Year (optional field)

Typically to have it run every day you would use “*” instead of “1/1”. I’m not sure what 1/1 would do in the Day of the Month field and that might be causing some headache.

There are no includes required to use a Time cron trigger in a rules file. So the include org.joda.time.* is a red herring.

2 Likes

Good to know!

Never heard that, some special saying in English language?

Sorry, colloquialisms sometimes pop up in my postings.

It is indeed an English colloquialism which means “a misleading clue”.

You actually made me go down the rabbit hole (oops, another colloquialism) to look up the origin. It comes from Middle English and refers to the practice of fugitives using pieces of smoked herring (which are red in color as opposed to fresh herring which is white) to throw blood hounds off their scent.

In this case, something being wrong with the imports is a red herring because it distracts us from the real cause of the problem.

1 Like

Thanks, always happy to improve my English skills :grinning:
And: thanks again for another (even off topic) detailed explanation :eyeglasses:

Thanks for pointing out Cronmaker. I am not a dumb man but the crons kept baffling me and when I ran some through Cronmaker, they turned out to be different from what I expected. I won’t tire my brain over it anymore but use cronmaker. Thanks

Yet this is explicitly referenced as a resource in the openHAB documentation related to rules.

I have been experiencing similar issues with getting a cron rule to fire. Following the documentation has been less that fruitful.

Well, that 2-year-old posting is incorrect. At the time of that posting I didn’t realize that cronmaker is based on Quartz.