Cron in rules files for Openhab

Simple cron in rules files for Openhab
In this post i explain how to schedule task from rules file using cron.
Rules are triggered based on the what you specify in the “when” part.
It can either trigger by update of item or in specific time. For time scheduling is used Unix-like job scheduler called cron.

Create rules files in the Openhab configuration folder inside folder “rules”
Extension for this files is *.rules .File will look like this “example.rules”
Bellow are some examples what is the standard format of this file

Examples from my rule files.
Open Rollershutter every day 7:30 except winter months

rule "Morning Shutter"
Time cron "0 30 6 ? 3-10 *"
logInfo("rule Morning Shutter", "triggered")

Here is another example where i scheduled the task every day at 1AM or by clicking switch.

rule "Daily energy charts"
Item manualruletrigger received update or
Time cron "0 1 0 * * ?"

And another to trigger rule every five minutes

rule "Every 5 minutes"
Time cron "0 */5 * ? * *"

Very good cron generator for Openhab expected format

Some versions of cron does not starts with seconds as Openhab format and also accepts “*” for day of the week which Openhab version does not accept. Have to use “?”

  1. I’m not sure that these are correct.
    The first example is 6:30am not 7:30am
    The second example is 0:01am not 1:00am and not sure ? is in the right place. Shouldn’t it be 0 0 1 ? * * *
    The third example would be better as 0 0/5 * ? * * * as it would start on the hour

Leave a Reply

Your email address will not be published. Required fields are marked *