I’m sure I already posted something like this, but I couldn’t find it. I’ll add this one to the helper library docs… Helper Libraries for openHAB Scripted Automation — openHAB Helper Libraries documentation. This will create a rule that watches for changes in the DatetTmeItem and reschedules a cron rule when it changes. BTW, forget the official scripted automation documentation… it is very out of date. I plan to update them when/if my add-ons are merged.
from core.rules import rule
from core.triggers import when
@rule("Monitor DateTime Item for changes")
@when("Item Virtual_DateTime_1 changed")
@when("System started")
def monitor_datetime_changes(event):
zoneddatetime = items["Virtual_DateTime_1"].zonedDateTime
cron_expression = "{} {} {} {} {} ? {}".format(zoneddatetime.getSecond(), zoneddatetime.getMinute(), zoneddatetime.getHour(), zoneddatetime.getDayOfMonth(), zoneddatetime.getMonthValue(), zoneddatetime.getYear())
monitor_datetime_changes.log.info("Creating cron rule from DateTimeType: Virtual_DateTime_1: {}, cron_expression: {}".format(items["Virtual_DateTime_1"], cron_expression))
if event:# the existing rule needs to be removed or its trigger needs to be changed (removing is easier)
# event will only be None when the trigger is 'System started'
scriptExtension.importPreset("RuleSupport")
ruleRegistry.remove(monitor_datetime_changes.cron_uid)
@rule("Cron rule based on DateTime Item")
@when("Time cron " + cron_expression)
def cron_example(event):
cron_example.log.warn("Cron triggered")
monitor_datetime_changes.cron_uid = cron_example.UID
For a simple installation of Jython and the helper libraries, I have created an add-on…
Let me know if you have any other questions! Oh… and for beer donations, click here!