[SOLVED] Time related rule using cron

  • Platform information:
    • Hardware: Synology
    • OS: _Linux
    • openHAB version: 2

Hi guys, i tried to make a rule that will turn off my lights outside my house at midnight for every night. But it didn’t worked out, as the lights where still on when I came down the next day. Looking in the logs, there was no sign that the rule was executed. This is the rule:

rule “Luifelverlichting uit” //luifelverlichting uit
when
Time cron “0 0 * * *”
then
sendCommand(LuifelverlichtingSwitchBinary1,OFF)
end
The indents don’t look right but that’s just how my text is converted and I don’t know how to fix that…

I know can adjust the “when” to Time is midnight, but I wanna know what’s wrong with this, so that I can use it for other times as well.

Log files around midnight:

2020-06-16 23:14:56.016 [vent.ItemStateChangedEvent] - Temperature changed from 16.54 to 16.67
2020-06-17 00:00:30.872 [vent.ItemStateChangedEvent] - Sunrise_Time changed from 2020-06-16T05:23:00.000+0200 to 2020-06-17T05:23:00.000+0200
2020-06-17 00:00:30.879 [vent.ItemStateChangedEvent] - Sunset_Time changed from 2020-06-16T22:00:00.000+0200 to 2020-06-17T22:00:00.000+0200
2020-06-17 00:09:55.983 [vent.ItemStateChangedEvent] - Temperature changed from 16.67 to 16.40

1 Like

In order to have a feedback in the logs use a logInfo line in the rule.
Cron uses seconds as well, this one triggers 10 seconds after midnight.
Time cron "10 0 0 * * ?"

Hi Jürgen,
thanks for your responds, however I don’t know exactly what you mean. In the events.log should be like a trigger for the rule right? And since it’s not in there, I assume that the problem is in the cron time right?

My response had two topics

A logInfo line, like:

logInfo("Luifelverlichting uit", "The rule triggered")
used as the first line after the “then” would show in to log if the rule was triggered at all.

Your from was wrong (hence nothing in the logs), the seconds were missing. Use the one I posted above.

This should be Time cron "10 0 0 * * ? *" as @Jim_Verspuij mentioned (be aware of the extra *), in case of 10 seconds after midnight, otherwise it should be Time cron "0 0 0 * * ? *"
On other option is to use Design Pattern: Time Of Day. It more advanced rule.

On other problem that can be is this:

This actions only accepts strings, OFF is not a string, "OFF" is. See MyItem.sendCommand(“new state”) versus sendCommand(MyItem, “new state”)

My advise is to use LuifelverlichtingSwitchBinary1.sendCommand(OFF)

1 Like

To be more precise :wink: :

Time cron is using quartz cron (and this is a little bit different to crontab). The Time cron expression is:

Time cron "ss mm hh DD MM WW YYYY"

where YYYY is OPTIONAL. All other fields are mandatory.
ss -> Seconds
mm -> Minutes
hh -> Hours
DD -> Day of Month
MM -> Month (i.e. 1 to 12 or JAN to DEC)
WW -> Day of Week (i.e. 1 to 7 or SUN to SAT)
You MUST use exactly one ? in your Time cron expresison. Either use it as Day of Week or as Day of Month. So, a trigger every day @ midnight would be either

Time cron "0 0 0 ? * *"

or

Time cron "0 0 0 * * ?"

as the year is optional.

Of course, you could write 0/60 instead of 0 for the Seconds or Minutes, as well as 1/1 for Day of Month or Month, or use an asterisk for the year…

The main point is: a questionmark is mandatory and quartz cron uses seconds.

3 Likes

Okay thanks! I shall implement it to make sure it will get into my logs.
Ah okay strange that the seconds where missing, cuz I used a website where you can generate your time in cron format… But anyhow, thanks for the support!

Wow thanks for the elaboration, but what does the “?” mean? That this is the variable in the time? And can you have multiple question marks in your cron format?

This didn’t gave me any problems so far. I use this command also for other lights, triggered by sunset/rise and that didn’t gave me any problems or errors so far…

The questionmark is “This field is not set at all, so don’t care about”.
This is: You can’t build a Time cron expression which will only trigger, if it’s Friday, the 13th.
Of course you can trigger a rule on every 13th of a month and then check within the rule, if it’s Friday. But that is not a simple Time cron expression anymore.

Or in other words: you can specify either Day of Month or Day of Week, but not both at the same Time cron Expression.
To make that more clear to the user, you have to use the questionmark for one of the two fields.

1 Like

Here is a site where you can create cron expression and also explain the cron expression: https://www.freeformatter.com/cron-expression-generator-quartz.html
I find it very useful

3 Likes