AlarmClock doesn´t fire

Please amend your code. I will try to analyze it.

Don’t know if I understand what you mean. I tried your exact code. I have a DateTime item in items file called AlarmClock. Don’t need any imports right?

This is what i get when trying to set an alarm:

2020-12-29 15:47:00.124 [INFO ] [org.openhab.core.model.script.alarm ] - Scheduling alarm for 2020-12-30T09:00:00.000+0100 (2020-12-30T08:00:00.000+0000)

2020-12-29 15:47:00.128 [INFO ] [org.openhab.core.model.script.alarm ] - New alarm

2020-12-29 15:47:00.146 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'static-1' failed: An error occurred during the script execution: Could not invoke method: org.openhab.core.model.script.actions.ScriptExecution.createTimer(java.time.ZonedDateTime,org.eclipse.xtext.xbase.lib.Procedures$Procedure0) on instance: null in static

Okay, you are right. My fault. As I do not use DSL rules anymore I missed a small detail: toLocalZone returns a DateTimeType but we need the ZonedDateTime value. I changed it in my post above.

Oh yes! It’s working now! Thank you so much. Another great experience of using the forum. Thanks and happy new year!

I merged the changes for this rule, so it should be live in the docs soon: Update alarm clock rule to work with openHAB 3 (#2348) · openhab/openhab-android@2488abd · GitHub

1 Like

I’m also trying to get the alarm clock feature running in OH3 but unfortunately it doesn’t work with the provided examples which I found online (docs & forum).

Every time I create or cancel an alarm the ‘else’ part of the rule with the log output “Alarm canceled” is executed and the alarm is never properly scheduled.
The AlarmClock item is receiving the time from the android app.

2021-01-31 18:01:51.365 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AlarmClock' changed from UNDEF to 1612198320000

So I assume that (at least for my oh instance) something in the following part is not working as it shoud but I don’t know what could go wrong at this part.

[...]
if( newState instanceof DateTimeType ) {
[...]

What’s the type of your AlarmClock item?

Thanks for the hint! :slightly_smiling_face:
The item type was still ‘Number’ as it was in OH2. Missed that while migrating the rule.

Hi Mueller

I think I’m doing something wrong.

Here is my OH3 rule:

var Timer timerAlarm = null

rule "Alarm Clock"
when
    Item AlarmClock changed
then
    if (newState instanceof DateTimeType) {
        val epoch = newState.toLocaleZone.zonedDateTime.toInstant.toEpochMilli
        logInfo("alarm", "Scheduling alarm for {} ({})", newState.toLocaleZone, epoch)
        if (timerAlarm !== null) {
            logInfo("alarm", "Reschedule alarm")
            timerAlarm.reschedule(newState.toLocaleZone.zonedDateTime)
        } else {
            logInfo("alarm", "New alarm")
            timerAlarm = createTimer(newState.toLocaleZone.zonedDateTime, [ |
                 // Turn on stuff, e.g. radio or light
                    val actions = getActions("network", "network:pingdevice:PC")
                    actions.sendWakeOnLanPacket()
                    val mqttActions = getActions("mqtt", "mqtt:broker:local")
                    mqttActions.publishMQTT("cmnd/entrada/Power", "ON") 
                    mqttActions.publishMQTT("cmnd/ledcocina1/Power", "ON") 
                    mqttActions.publishMQTT("cmnd/luztv/POWER", "ON") 
                logInfo("alarm", "Alarm expired")
                timerAlarm = null
            ])
        }
    } else {
        if (timerAlarm !== null) {
            timerAlarm.cancel
            timerAlarm = null
        }
        logInfo("alarm", "Alarm canceled")
    }
end

and here is my Item:

DateTime AlarmClock “Alarm Clock [%s]”

But the alarm in my phone never fires the rule.
I’m on android 10.
OH3 DateTime item gets the update on the alarms, but the rule isn’t firing.

EDIT: I discovered that the rule fires, but it never fires my actions… :upside_down_face:

Do you see the log message “New alarm” in the openHAB server log?

Sorry for the delay… yes, I see that the alarm is being fired but somehow is not doing the actions I set

val actions = getActions(“network”, “network:pingdevice:PC”)
actions.sendWakeOnLanPacket()
val mqttActions = getActions(“mqtt”, “mqtt:broker:local”)
mqttActions.publishMQTT(“cmnd/entrada/Power”, “ON”)
mqttActions.publishMQTT(“cmnd/ledcocina1/Power”, “ON”)
mqttActions.publishMQTT(“cmnd/luztv/POWER”, “ON”)

Maybe its a bad syntax?

How can you tell, do you see the “Alarm expired” message at the expected time?

How can you tell, are you monitoring MQTT messages perhaps?

I made a change in the script, just enclosed the actions with curly brackets and now it is working.

the log:

2021-03-10 15:19:15.472 [INFO ] [org.openhab.core.model.script.alarm ] - New alarm
2021-03-10 15:20:00.266 [INFO ] [twork.internal.WakeOnLanPacketSender] - Wake-on-LAN packets sent (MAC address: 24:4B:FE:xx:xx:xx)
2021-03-10 15:20:00.268 [INFO ] [org.openhab.core.model.script.alarm ] - Alarm expired

And the “modification”

var Timer timerAlarm = null

rule "Alarm Clock"
when
    Item AlarmClock changed
then
    if (newState instanceof DateTimeType) {
        val epoch = newState.toLocaleZone.zonedDateTime.toInstant.toEpochMilli
        logInfo("alarm", "Scheduling alarm for {} ({})", newState.toLocaleZone, epoch)
        if (timerAlarm !== null) {
            logInfo("alarm", "Reschedule alarm")
            timerAlarm.reschedule(newState.toLocaleZone.zonedDateTime)
        } else {
            logInfo("alarm", "New alarm")
            timerAlarm = createTimer(newState.toLocaleZone.zonedDateTime, [ |
                
                {val actions = getActions("network", "network:pingdevice:PC")
                    actions.sendWakeOnLanPacket()}
                {val mqttActions = getActions("mqtt", "mqtt:broker:local")
                    mqttActions.publishMQTT("cmnd/entrada/Power", "ON") 
                    mqttActions.publishMQTT("cmnd/ledcocina1/Power", "ON") 
                    mqttActions.publishMQTT("cmnd/luztv/POWER", "ON")}
                
                logInfo("alarm", "Alarm expired")
                timerAlarm = null
            ])
        }
    } else {
        if (timerAlarm !== null) {
            timerAlarm.cancel
            timerAlarm = null
        }
        logInfo("alarm", "Alarm canceled")
    }
end

Guys… sorry its me again…
The “fix” didn’t work after all… strange thing is that everything works if my computer is reachable in the network, but if its off, the rule doesn’t fire the actions I configured.

Also, yesterday I changed the alarm time, and it didn’t work.
This is in the log:

2021-03-23 07:30:01.115 [WARN ] [ore.internal.scheduler.SchedulerImpl] - Scheduled job failed and stopped
java.lang.reflect.UndeclaredThrowableException: null
	at com.sun.proxy.$Proxy242.apply(Unknown Source) ~[?:?]
	at org.openhab.core.model.script.actions.ScriptExecution.lambda$0(ScriptExecution.java:82) ~[?:?]
	at org.openhab.core.internal.scheduler.SchedulerImpl.lambda$12(SchedulerImpl.java:166) ~[bundleFile:?]
	at org.openhab.core.internal.scheduler.SchedulerImpl.lambda$1(SchedulerImpl.java:76) [bundleFile:?]
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) [?:?]
	at java.util.concurrent.FutureTask.run(FutureTask.java:264) [?:?]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) [?:?]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) [?:?]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) [?:?]
	at java.lang.Thread.run(Thread.java:834) [?:?]
Caused by: org.openhab.core.model.script.engine.ScriptExecutionException: The name '<unkown>' cannot be resolved to an item or type; line 0, column 0
	at org.openhab.core.model.script.interpreter.ScriptInterpreter.invokeFeature(ScriptInterpreter.java:141) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:992) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:955) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:236) ~[?:?]
	at org.openhab.core.model.script.interpreter.ScriptInterpreter.doEvaluate(ScriptInterpreter.java:226) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:216) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.evaluateArgumentExpressions(XbaseInterpreter.java:1206) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._invokeFeature(XbaseInterpreter.java:1136) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.invokeFeature(XbaseInterpreter.java:1082) ~[?:?]
	at org.openhab.core.model.script.interpreter.ScriptInterpreter.invokeFeature(ScriptInterpreter.java:151) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:862) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:232) ~[?:?]
	at org.openhab.core.model.script.interpreter.ScriptInterpreter.doEvaluate(ScriptInterpreter.java:226) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:216) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:459) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:240) ~[?:?]
	at org.openhab.core.model.script.interpreter.ScriptInterpreter.doEvaluate(ScriptInterpreter.java:226) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:216) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.evaluate(XbaseInterpreter.java:202) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.ClosureInvocationHandler.doInvoke(ClosureInvocationHandler.java:47) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.AbstractClosureInvocationHandler.invoke(AbstractClosureInvocationHandler.java:30) ~[?:?]
	... 10 more

and my rule:

var Timer timerAlarm = null

rule "Alarm Clock"
when
    Item AlarmClock changed
then
    if (newState instanceof DateTimeType) {
        val epoch = newState.toLocaleZone.zonedDateTime.toInstant.toEpochMilli
        logInfo("alarm", "Scheduling alarm for {} ({})", newState.toLocaleZone, epoch)
        if (timerAlarm !== null) {
            logInfo("alarm", "Reschedule alarm")
            timerAlarm.reschedule(newState.toLocaleZone.zonedDateTime)
        } else {
            logInfo("alarm", "New alarm")
            timerAlarm = createTimer(newState.toLocaleZone.zonedDateTime, [ |
                
            {val actions = getActions("network", "network:pingdevice:PC")
                actions.sendWakeOnLanPacket()}
            {val mqttActions = getActions("mqtt", "mqtt:broker:local")
                mqttActions.publishMQTT("cmnd/entrada/Power", "ON") 
                mqttActions.publishMQTT("cmnd/ledcocina1/Power", "ON") 
                mqttActions.publishMQTT("cmnd/luztv/POWER", "ON")}
                
                logInfo("alarm", "Alarm expired")
                timerAlarm = null
            ])
        }
    } else {
        if (timerAlarm !== null) {
            timerAlarm.cancel
            timerAlarm = null
        }
        logInfo("alarm", "Alarm canceled")
    }
end

I just don’t know what is happening anymore…

Neither do we, there isn’t much to go on.

Is that a random find, or is it the time you expected the alarm to go off?

Why not make the first lined of your scheduled code a logInfo, so that you can see if the timed work begins before it encounters any errors.

Your rule has logging to tell you what it’s up to when you change time. What does that logging have to say about it?

Are there other rules in the same file, have you been editing those or this one? Note that when you edit a rules file that has spawned long-running timers, the timers are not destroyed but they are “orphaned”, detached from the context in which they are created.
That means they will still go off at the appointed time but usually fail.

Did you restart openHAB? Your alarm doesn’t get set up after a restart.

Is that a random find, or is it the time you expected the alarm to go off?
Why not make the first lined of your scheduled code a logInfo, so that you can see if the timed work begins before it encounters any errors

It was at the alarm time

Your rule has logging to tell you what it’s up to when you change time. What does that logging have to say about it?

2021-03-22 08:39:00.510 [INFO ] [org.openhab.core.model.script.alarm ] - Alarm expired
2021-03-22 08:39:17.636 [INFO ] [org.openhab.core.model.script.alarm ] - Alarm canceled
2021-03-22 08:39:38.214 [INFO ] [org.openhab.core.model.script.alarm ] - Scheduling alarm for 2021-03-23T07:30:00.000-0300 (1616495400000)
2021-03-22 08:39:38.214 [INFO ] [org.openhab.core.model.script.alarm ] - New alarm
2021-03-22 08:40:12.918 [INFO ] [org.openhab.core.model.script.alarm ] - Scheduling alarm for 2021-03-23T07:29:00.000-0300 (1616495340000)

Date is older but it is what it does when changing time.

No other rules are in the file, the alarm.rules has this script only
Weird thing is that the alarm fires but none of the actions are carried out. That log error posted before happened last time, but most of the time the rule log fires as if it worked.

Will try restarting the server but last time I did that, it didn’t help.

Again, I´m scratching my head because it only works when testing and the pc (not openhab server BTW) is online… no idea what is going on :upside_down_face: :woozy_face:

Where does your MQTT broker live?

Okay, a previous scheduled run finished a few seconds before the next scheduling takes place. Let’s guess that’s us seeing you testing, setting it up again a short time after a test.

Apart from that, looks ordinary enough.

OK, UPDATE:

It seems the problem is on the phone, I tried setting an alarm on another phone and it fires the rule and all my actions.
I have an android 10 running phone, tried the beta app and the main app.
Also… another thing is that I use the google clock on the android 10 phone and the stock alarm app on the working phone.

The weird part of this is that both phones seem to send the alarm update to the server… so yes… I have no idea on whats going on here.

I’ll see what I can find

Can you open the app, go to the settings where the alarm clock item is set. It should say “Last update was …”.
What does it display?

It displays the last update as it should. Rebooting the phone seemed to help… no idea what happened.

I hope this helps someone else having problems, just reboot the phone if you didn’t do it before.

Sorry for the inconvenience and thanks for the support.