Dynamic set days and time for rules, instead of hard-coded cron?

Hi all,

I currently use cron-based rules for controlling several items.
However, my question now is…is there a structured way of replacing cron with self set schedules, which can be adjusted trough a UI (preferably Habpanel).

When I have a rule like this:

rule "Event start at 15:00 on SAT-SUN"
    Time cron "0 0 15 ? * SAT,SUN *"
  // *do stuff*

Is is possible to create the same, but based on a setting from UI, e.g. where I can select the days and set the time for example?
Reason is that I don’t want to go into the rules files all the time when I need to change the schedule(s).
If someone can point me in the right direction…

Thanks in advance!

If I had a penny esch time someone asked for this I would be a rich man.
Have you searched the forum.

Scheduling is poorly addressed in OH but there are alternatives…


Hi Vincent,

LOL…I was afraid of it…
Yes I did search and found some ‘workarounds’ but not satisfying so far.
will do some more investigations later tonight in that case…

If it helps

I’ve modified this to suit my needs and I have to say that they are working really well.

On a similar theme, I’ve adapted the widget to work with the built in timers in the hardware I have… (Ahem… ahem…)

I did try a few of the other options I found on the forum, but nothing really worked for me as I had hoped.

Good luck.

Thank you @MDAR,

I’ll go through it now and let you know.


1 Like

Good luck.

Not strictly an openHAB2 solution, but if you only need to toggle two states.

I’ve got it swapping a radio station preset.

But how does the times set get triggered?
It looks good as a start, but what I miss is the ‘knowledge’ of how these settings are checked by openhab and get triggered…

Are you asking about the DSL solution or the NodeRed one?

DSL rules check every minute and act as the rule demands.

It’s a rather good solution.

Read the chaps write up carefully and it should make sense.

Or just try it and see how you get on.

I’m reading about Joda time… make me wonder if something like this is possible?

  currentTime.compareTo(setTime) == 0
  //execute something

Since the result of compare to shall be 0 when they match…otherwise it’s -1 or 1.
I see that you can use string types with Parse method to get them to compare to a datetime object…

If this can be achieved, then it should be possible to set hours and minutes and combine them in a string type item (“hh:mm”) and compare this to currentTime…then there is the scheduler…
Or am I too optimistic?



because you can’t do the ‘currentTime.compareTo(othertime)’ part?

Shoot…I thought I had something…hahahaha

That’s right.
The rules triggers are events only.
See: https://www.openhab.org/docs/configuration/rules-dsl.html#rule-triggers

and the jsr223 jython method maybe?

trigger = newTrigger()
    .withIdentity("triggerX", "groupY")
    .withSchedule(dailyAtHourAndMinute(10, 42))

instead of hardcode 10 and 42, this could be an itemvalue maybe?
Will this trigger work?

How about a rule that triggers on an Item change, where the action creates/modifies another rule that triggers on the time from the Item in the first rule? This is not possible in the old rule engine, but with scripted automation, you could do something like this…

from core.rules import rule
from core.triggers import when

@rule("Adjust rule trigger")
@when("Item Virtual_DateTime_1 changed")
@when("System started")
def adjust_rule_trigger(event):
    adjust_rule_trigger.log.warn("Cron Item changed")

    # if rule exists, remove it
        rule_uid = [rule_object for rule_object in rules.getAll() if rule_object.name == "Alarm clock"][0].UID
        adjust_rule_trigger.log.warn("Alarm clock rule exists, so removing it")
    # create rule
    time = items["Virtual_DateTime_1"].zonedDateTime
    @rule("Alarm clock")
    @when("Time cron {} {} {} * * ?".format(time.second, time.minute, time.hour))
    def alarm_clock(event):
        alarm_clock.log.warn("Wake up!")

This adjusts the cron rule based on the time portion of the DateTimeItem’s state. If you’re not using this for a recurring daily timer, it can be easily modified to trigger on a specific date and time. You can use something similar for scheduling through Alexa or the alarm Item updated through Habdroid.


This is a way to achieve scheduling controlled from UI (BasicUI).

1 Like