NOTE: This rule template is now deprecated. Please use the Time is <item> rule trigger. In the UI you can find that trigger under “Time Event”.
A rule template that will schedule the execution of another rule based on the state of a DateTime Item. This rule makes a good companion to use with the Android App’s Alarm Item but it can be used with any DateTime Item.
The logger is named org.openhab.model.script.rules_tools.Alarm Clock
Alarm Time Item
Item that contains the date and time to run the script.
Script to Call
The Rule or Script to call at the indicated date and time.
If you’ve more than one DateTime Item you can create more than one instance of this rule from the template, one for each Item. They can call the same Script or a different Script as desired.
For hard coded alarm times, use in conjunction with the To Today rule template to advanced those date times to the current day.
The Alarm Time must be in the future or else no alarm will be set and any existing alarm will be cancelled.
The date as well as the time is used. To set a recurring alarm one must have a separate rule to move the Item’s date forward to the new day. Use the To Today rule template for that.
If using the Android App and SleepAsAndroid, the alarm time will not be posted until a little before the alarm is set to go off.
Fixed bug with replacement strings
adjusted for breaking change, will no longer work for versions of OH prior to December 15th
made compatible with Nashorn and JSScripting
NOTE: Sometime after the release of OH 3.2 I’ll remove support for Nashorn so I can use the helper libraries.
added code to pass the name of the Item that triggered the alarm to the called script
added a trigger to run the rule at system started so if OH restarts the alarm timer is rescheduled
question, how can i set this rule to turn on a device with a scheduled date/time and make it running for XXmins?
i’ve copied your rule on my test environment and i see under “when” i can set the setpoint item that could be my runtime (30 mins, 1 hour…) but i would like to have the possibility to chose a weekday to run this and also to to chose the device to turn on/off from a list.
can you help with this to make your script really powerful?
That is literally what the rule does. You have a DateTime Item. At the date and time of that Item, the above rule will run a Script rule that you write.
If you want it to turn on Switch Foo for 60 minutes on Thursday at 09:00, set the DateTime Item to 09:00 on Thursday and write your Script Rule to send an ON command to Foo, wait an hour and then send an OFF command to Foo.
Then set the DateTime Item to the week day that you want it to run.
NOTE: This rule does not reschedule the “alarm”. It is up to you to write something that changes the DateTime Item to move it forward to the next date and time that the alarm should go off.
The only thing this rule template does is schedule a Script to run (a Script you have to write) at a DateTime specified by the state of a DateTime Item. Nothing more.
The features you are asking for are not implemented by this rule and since this rule was written to primarily work in conjunction with the Android app’s Alarm Item feature (i.e. the app will publish the date and time of the next scheduled alarm scheduled on the phone) there really is no way I can add the features you are asking for without breaking this rule template’s primary purpose.
I am also getting “Installation of add-on 127194 failed” on openHAB 3.2.0.RC1 - Milestone Build RPI4.
2021-12-19 07:05:34.717 [ERROR] [tion.MarketplaceRuleTemplateProvider] - Unable to parse YAML: Expected a field name (Scalar value in YAML), got this instead: <org.yaml.snakeyaml.events.MappingStartEvent(anchor=null, tag=null, implicit=true)>
Note that there is a breaking change that occurred after 3.2 M5 so even if you were to successfully install the rule template right now, if you are not on the SNAPSHOTS it won’t work. So that might be coming into play here too.
But there may be a problem in my fixes for that breaking change. I’m debugging that now.
It should work not, but remember that it will only work on SNAPSHOTS post 3.2 M5 and 3.2 release and later.
But note that this is merely one way the rule template can be used. If your DateTime Item is populated from the Android App or iCalendar binding or the like no UI is required. Personally, I just use this template to trigger a rule when I have an alarm set on my phone that will transition my Time of Day state to MORNING if it’s not already day.
My rule templates are created to be build blocks towards a full solution, not a turn-key “install it and use it”. Even so, I do not plan on writing such comprehensive tutorials for all of my rule templates.
@rlkoshak - of course you have already - truly a beautiful article, apologies I tried searching but not hard enough - might be worthy pinning this solution article to the time of day and the above at the top to help others find this awesome article.
I’ve not added it to the docs for this rule template because I don’t want people to think it’s limited for use in just this one example. All this template does is schedule a rule to run based on the state of a DateTime Item. Nothing more, nothing less. But even with that simple capability it is useful in its own right and useful in many different ways.
Bump up the logging on the rule. You can either edit the “debugs” to make them “info” level or adjust the logging level for “org.openhab.model.script.rules_tools.Alarm Clock” in the karaf console or log4j2.xml file.
One minor problem I uncovered is related to the Android app. I discovered that my phone and server were about five seconds apart. Usually that wouldn’t be a problem except Sleep as Android sets the alarm only a minute before it’s supposed to go off and then when it does go off it cancels the alarm. Since my phone was five seconds ahead, the cancel came before the alarm timer in OH could go off and it got cancelled.
I can’t tell if something like that is happening here without more logs.
And yes, I do not restart OH after 12 am.
From the logs a restart before 12 am correctly reconizes the new wakeup time.
So everything seems to work, but the trigger at 6:30 itself.
Anyway, after deleting the rule and creating it again, this morning it worked.
I will track that closely, because this was the case with the first try as well.
(it stopped working).
Thank you, Rich.
I will get back here, if it happens again.
Thanks. It’s really odd behavior. I’ve only ever had it not work for me the one time and it was what I described above and due to a quirk with the Sleep as Android app and my phone and computer being about five seconds apart.
The only thing I didn’t ask was to make sure you grabbed the latest version of the template by removing and then readding it and then recreating the rule. That will ensure we are both looking at the same code.