uid: rules_tools:alarm_clock label: Alarm Clock description: This will trigger on an update to a DateTime Item and schedule a timer to call another script at the DateTime's state. configDescriptions: - name: alarmTime type: TEXT context: item filterCriteria: - name: type value: DateTime label: Alarm Time Item required: true description: Item that holds the date and time to run the script. - name: script type: TEXT context: rule label: Script to Call required: true description: The Script or Rule to call at the alarm time. triggers: - id: "1" configuration: itemName: "{{alarmTime}}" type: core.ItemStateChangeTrigger - id: "3" configuration: startlevel: 100 type: core.SystemStartlevelTrigger conditions: [] actions: - inputs: {} id: "2" configuration: type: application/javascript script: >- if(typeof(require) === "function") Object.assign(this, require('@runtime')); var FrameworkUtil = (FrameworkUtil === undefined) ? Java.type("org.osgi.framework.FrameworkUtil") : FrameworkUtil; var ScriptExecution = (ScriptExecution === undefined) ? Java.type("org.openhab.core.model.script.actions.ScriptExecution") : ScriptExecution; var ZDT = (ZDT === undefined) ? Java.type("java.time.ZonedDateTime") : ZDT; var logger = (logger === undefined) ? Java.type("org.slf4j.LoggerFactory").getLogger("org.openhab.model.script.rules_tools.Alarm Clock") : logger; logger.warn('The Alarm Clock rule template has been deprecated. Use the Time is trigger instead.') this.timer = (this.timer === undefined) ? null : this.timer; var time = items['{{alarmTime}}']; var item = '{{alarmTime}}'; var script = '{{script}}'; var callScriptGenerator = function(data, script) { return function() { logger.debug("About to call script action"); // Get the RuleManager this.ScriptHandler = Java.type("org.openhab.core.automation.module.script.rulesupport.shared.ScriptedHandler"); var _bundle = FrameworkUtil.getBundle(ScriptHandler.class); var bundle_context = _bundle.getBundleContext() var classname = "org.openhab.core.automation.RuleManager" var RuleManager_Ref = bundle_context.getServiceReference(classname); var RuleManager = bundle_context.getService(RuleManager_Ref); RuleManager.runNow(script, true, data); } } // No alarm scheduled var type = (typeof(require) === "function") ? DateTimeType : DateTimeType.class; if(time.class != type || time.getZonedDateTime().isBefore(ZDT.now())) { logger.info("No alarm scheduled for " + item); if(this.timer !== null) { this.timer.cancel(); } } // create or schedule a timer to run at the configured time else { logger.info("Scheduling alarm at " + time + " for " + item); if(this.timer !== null) { logger.debug("Rescheduling alarm for " + time); this.timer.reschedule(time.getZonedDateTime()); } else { logger.debug("Setting a new alarm for " + time); var map = new java.util.HashMap(); map.put("triggeringItem", item) this.timer = ScriptExecution.createTimer(time.getZonedDateTime(), callScriptGenerator(map, script)); } } type: script.ScriptAction