[Deprecated] Alarm Clock Rule

logo

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

Configuration:

Parameter Type Required Description
Alarm Time Item DateTime Item X Item that contains the date and time to run the script.
Script to Call Rule ID X 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.

Language: Nashorn JavaScript (default built into OH 3.x) or JSScripting

Limitations:

  • 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.

Changelog

Version 0.7

  • deprecated

Version 0.6

  • Fixed bug with replacement strings

Version 0.5

  • adjusted for breaking change, will no longer work for versions of OH prior to December 15th

Version 0.4

  • 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.

Version 0.3

  • added code to pass the name of the Item that triggered the alarm to the called script

Version 0.2

  • bug fixes
  • added a trigger to run the rule at system started so if OH restarts the alarm timer is rescheduled

Version 0.1

  • initial release

Resources

https://raw.githubusercontent.com/rkoshak/openhab-rules-tools/main/rule-templates/alarmClock/alarmclock.yaml

3 Likes

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.

Get “Installation of add-on 127194 failed” when trying to add Alarm Clock Rule. Running OH version 3.2.0.M5 on Win 10. Am I missing some other plug-ins or ??

I am also getting “Installation of add-on 127194 failed” on openHAB 3.2.0.RC1 - Milestone Build RPI4.

Logs:
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)>

at [Source: (StringReader); line: 23, column: 19] (through reference chain: org.openhab.core.automation.dto.RuleTemplateDTO[“triggers”]->java.util.ArrayList[0]->org.openhab.core.automation.dto.TriggerDTO[“configuration”])

2021-12-19 07:05:34.719 [ERROR] [ty.CommunityRuleTemplateAddonHandler] - Rule template from marketplace is invalid: Unable to parse YAML

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.

Thanks Rich, I have updated to latest snapshot and all is working well. Thank you for all your efforts and good work you put into the openhab community.

@rlkoshak do you have an end to end example using this rule template with items, widgets and rules for changing in the UI the alarm day, and time?

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.

Thanks again for all that you do

It is already at the top of the deprecated time of day tutorial. [Deprecated] Design Pattern: Time Of Day

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.

I am using your template since a few weeks together with the “MoveToNextDay” (Marketplace).

Usually it works fine, but sometimes it does not.

The logs of yesterday / today show e.g:

2022-01-30 00:00:01.512 [INFO ] [model.script.rules_tools.Alarm Clock] - Scheduling alarm at 2022-01-30T06:30:00.000+0100 for AlarmClockTime

but at 6:30 am this morning, nothing happened.

I restarted the system yesterday afternoon, but the init went well, too:

2022-01-29 17:25:43.659 [INFO ] [model.script.rules_tools.Alarm Clock] - No alarm scheduled for AlarmClockTime

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.

Hi Rich and thank you for your help.
I will play around with the log level settings.

By the way: I don’t use the Android App. The rule is running purely on the OH host.

Also pay attention to updates and changes and the current state of the DateTime Item that drives the rule.

Same thing this morning:

2022-02-02 00:00:01.080 [INFO ] [model.script.rules_tools.Alarm Clock] - Scheduling alarm at 2022-02-02T06:30:00.000+0100 for AlarmClockTime
2022-02-02 00:00:01.121 [INFO ] [model.script.rules_tools.Alarm Clock] - Setting a new alarm for 2022-02-02T06:30:00.000+0100
...

but at 6:30 am:

2022-02-02 06:26:25.273 [INFO ] [.script.+++ PositionAddressRetrieval] - Geofencing - Address update: iCloudNCOsiPhone_Location is here: zuhause
2022-02-02 06:30:17.629 [INFO ] [del.script.+++ TileKeyFobPositioning] - TILE 1 location retrieval done
2022-02-02 06:30:17.634 [INFO ] [del.script.+++ TileKeyFobPositioning] - TILE 2 location retrieval done
2022-02-02 06:30:17.631 [INFO ] [org.openhab.rule.TileLocationUpdate ] - TileKeyFob1_LastLocationUpdate updated to 2022-02-02T06:14:22.600+0100
2022-02-02 06:30:17.637 [INFO ] [org.openhab.rule.TileLocationUpdate ] - TileKeyFob2_LastLocationUpdate updated to 2022-02-02T06:14:22.600+0100
2022-02-02 06:30:17.640 [INFO ] [del.script.+++ TileKeyFobPositioning] - TILE DNA location retrieval done
2022-02-02 06:30:17.643 [INFO ] [org.openhab.rule.TileLocationUpdate ] - TileKeyFobDona_LastLocationUpdate updated to 2022-02-02T06:14:22.600+0100
2022-02-02 06:30:21.893 [INFO ] [b.rule.CorridorMotionTimeStampUpdate] - Corridor Motion Timestamp updated / Occupancy activated

The AlarmClockTime did not change in between (just with the push to next day at 12am)

EDIT:
I have removed your rule and set it up again.
Although it was working before and I did not change anything in your rule, I will check if it will start working again.

Add logging to the script that gets called by the alarm clock too, assuming there isn’t some there already.

I assume you are not restarting OH between the time the alarm is set and the alarm is to go off. The rule should handle that but it is an important piece of information even so.

Yes, logging is in place.

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.

Thanks - I will do this right away.