Error using a rule from docs

Alarm Clock rule into doc:

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
                logInfo("alarm", "Alarm expired")
                timerAlarm = null
            ])
        }
    } else {
        if (timerAlarm !== null) {
            timerAlarm.cancel
            timerAlarm = null
        }
        logInfo("alarm", "Alarm canceled")
    }
end

Error into log:

An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.ScriptExecution.createTimer(org.joda.time.base.AbstractInstant,org.eclipse.xtext.xbase.lib.Procedures$Procedure0) on instance: null

What’s wrong?

Do you have any imports? There is nothing obviously wrong with that rule and there is no direct reference to org.eclipse.smarthome.model.script.actions.ScriptExecution.

What version of openHAB are you using?

No imports. I am on OH 2.5.11
I think that problem is in

createTimer(newState.toLocaleZone.zonedDateTime, [ |

OH 2.5 requires a Joda DateTime, not a ZonedDateTime in the call to createTimer.

createTimer(new DateTime(newState.toString), [ |
1 Like

So final rule is:

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)
			timerAlarm.reschedule(new DateTime(newState.toString))
        } else {
            logInfo("alarm", "New alarm")
            //timerAlarm = createTimer(newState.toLocaleZone.zonedDateTime, [ |
			timerAlarm = createTimer(new DateTime(newState.toString), [ |
                // Turn on stuff, e.g. radio or light
                logInfo("alarm", "Alarm expired")
                timerAlarm = null
            ])
        }
    } else {
        if (timerAlarm !== null) {
            timerAlarm.cancel
            timerAlarm = null
        }
        logInfo("alarm", "Alarm canceled")
    }
end

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.