Alarm Clock Rule

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.

By the way.
My issue is gone :smiley:
Thanks again!

1 Like

Where in this code can I add an if condition?

I want a script to run at Civil Dawn time, but only if my SceneSelection item == NIGHT

In the “but only if…” section of the rule. Or you can edit the code of the Script Action.

It looked to me like the rule triggering happened at the start of the timer, not just before the script ran.
I want it to check the condition just before it triggers the script.

I think I just answered my own question by saying it out loud. I’ll put the condition in the script I’m running. Duh!