Ever wanted to have an automation run on:
- a specific day like someone’s birthday or a fixed holiday?
- a floating holiday like Thanksgiving (USA)?
- the last day of every month?
- the first Monday but only in June and July?
You can easily do this with cron.
This is a Feature Request to provide scheduling in cron format (the Quartz version of cron).
Cron uses a compact expression to define simple or complex schedules. In the Quartz version of cron, the expression is composed of seven parameters, from second
to year
.
┌───────────── second (0-59)
│ ┌───────────── minute (0-59)
│ │ ┌───────────── hour (0-23)
│ │ │ ┌───────────── day of the month (1-31)
│ │ │ │ ┌───────────── month (1-12 or JAN-DEC)
│ │ │ │ │ ┌───────────── day of the week (1-7 or SUN-SAT)
│ │ │ │ │ │ ┌───────────── year (optional)
│ │ │ │ │ │ │
│ │ │ │ │ │ │
* * * * * * *
For example, the following expression means to trigger on December 25th at 7:15:00 for any year:
0 15 7 25 DEC ? *
Thanksgiving Day (USA) is the fourth Thursday in November. Here’s the expression to trigger on Thanksgiving at 18:45:00 for any year:
0 45 18 ? NOV 5#4 *
Trigger every Monday, Wednesday, and Friday at 6:00:00, 12:00:00, and 18:00:00 from January to March only in 2019.
0 0 6,12,18 ? JAN,FEB,MAR MON,WED,FRI 2019
At noon on the last weekday of every month.
0 0 12 LW * ?
Creating these expressions is simplified through the use of an expression generator like this one:
https://www.freeformatter.com/cron-expression-generator-quartz.html
This Feature Request could either be a completely new cron
component or an enhancement to the existing time
component.
As a new cron
component:
# compact format
trigger:
platform: cron
at: "0 45 18 ? NOV 5#4 *"
# verbose format
trigger:
platform: cron
- second: 0
- minute: 45
- hour: 18
- dom: '?'
- month: 'NOV'
- dow: '5#4'
- year: '*'
Alternately, as an enhancement to the existing time
component:
# compact format
trigger:
platform: time
style: cron
at: "0 45 18 ? NOV 5#4 *"
# verbose format
trigger:
platform: time
style: cron
- second: 0
- minute: 45
- hour: 18
- dom: '?'
- month: 'NOV'
- dow: '5#4'
- year: '*'
I’m not proficient with python but a little research suggests there are no python libraries for the Quartz scheduler (which is written in Java). However, on PyPi I found python-crontab. It appears to emulate the classic Unix cron so it lacks some features found in Quartz cron.
For example, it doesn’t support second
and year
and refers to Sun-Sat as 0-6 (as opposed to 1-7). If the use of python-crontab
makes it easier to implement this Feature Request, the loss of second
and year
is an acceptable compromise.
python-crontab’s expression format using five parameters from minutes to day of the week:
┌───────────── minute (0-59)
│ ┌───────────── hour (0-23)
│ │ ┌───────────── day of the month (1-31)
│ │ │ ┌───────────── month (1-12 or JAN-DEC)
│ │ │ │ ┌───────────── day of the week (0-6 or SUN-SAT)
│ │ │ │ │
│ │ │ │ │
│ │ │ │ │
* * * * *