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" when Time cron "0 30 6 ? 3-10 *" then logInfo("rule Morning Shutter", "triggered") dinning_roller.sendCommand(95) get_state_dinningroller.postUpdate(dinning_roller.state.toString) end
Here is another example where i scheduled the task every day at 1AM or by clicking switch.
rule "Daily energy charts" when Item manualruletrigger received update or Time cron "0 1 0 * * ?" then .... end
And another to trigger rule every five minutes
rule "Every 5 minutes" when Time cron "0 */5 * ? * *" then .... end
Very good cron generator for Openhab expected format
https://www.freeformatter.com/cron-expression-generator-quartz.html
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 “?”
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