Alarm clock rule error - no viable alternative at input 'then'

Dear All,

Apologies, if my issue is very trivial, I tried to find a solution, but I have not found any :frowning: .
I would like to set up an Alarm Clock solution to control my led strips and I found a code that I like very much, so I would like to use it as my base.

Unfortunately, OpenHAB gets back the following error for my rule file:
Configuration model ‘alarm.rules’ has errors, therefore ignoring it: [24,1]: no viable alternative at input ‘then’

I’m using OpenHAB 2.5, the rule code is the following:
`import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import org.openhab.action.squeezebox.*

import java.util.concurrent.locks.ReentrantLock

var Timer timer0 = null
var Timer timer1 = null
var Timer timer2 = null
var Timer timer3 = null

var java.util.concurrent.locks.ReentrantLock lock1 = new java.util.concurrent.locks.ReentrantLock()

rule "Alarm"
when
    Item alarmTimeHour changed or
    Item alarmTimeMinutes changed or
    Item alarmOffset1Minutes changed or
    Item alarmOffset2Minutes changed or
    Item alarmOffset3Minutes changed or
then
    lock1.lock()
    try {
    var String msg = ""
    logInfo("rules","Alarm has been changed.")

    var hour = alarmTimeHour.state as DecimalType
    var minute = alarmTimeMinutes.state as DecimalType

    if (hour < 10) { msg = "0" } 
    msg = msg + alarmTimeHour.state.format("%d") + ":"
    if (minute < 10) { msg = msg + "0" }
    msg = msg + alarmTimeMinutes.state.format("%d")
    postUpdate(alarmTimeMessage,msg)
    logInfo("rules","New alarm time: "+msg)

    var int alarm1
    alarm1 = (alarmTimeHour.state as DecimalType).intValue * 60 + (alarmTimeMinutes.state as DecimalType).intValue
    alarm1 = alarm1.intValue

    var int hour1
    hour1 = now.getMinuteOfDay
    hour1 = hour1.intValue

    var int delta1
    delta1 = (alarm1 - hour1)
    delta1 = delta1.intValue

    if (hour1 > alarm1) { delta1 = delta1 + 1440 }

    var int delta2
    delta2 = delta1 + (alarmOffset1Minutes.state as DecimalType).intValue

    var int delta3
    delta3 = delta1 + (alarmOffset2Minutes.state as DecimalType).intValue

    var int delta4
    delta4 = delta1 + (alarmOffset3Minutes.state as DecimalType).intValue

    if (timer0 != null) {
	timer0.cancel
	timer0 = null
	}

    timer0 = createTimer(now.plusMinutes(delta1)) [|
    timer0.reschedule(now.plusHours(24))

    if(alarmEnabled.state == ON) {
	var Number day = now.getDayOfWeek
	if (((day == 1) && (alarmMonday.state == ON))	||
	    ((day == 2) && (alarmTuesday.state == ON))	||
	    ((day == 3) && (alarmWensday.state == ON))	||
	    ((day == 4) && (alarmThursday.state == ON))	||
	    ((day == 5) && (alarmFriday.state == ON))	||
	    ((day == 6) && (alarmSaturday.state == ON))	||
	    ((day == 7) && (alarmSunday.state == ON))) {
		// The things to do if the alarm clock is enabled for this day of week:

		}
    }
    ]
    logInfo("rules","Alarm, timer0 set.")
    if (timer1 != null) {
	timer1.cancel
	timer1 = null
    }

    timer1 = createTimer(now.plusMinutes(delta2)) [|
    timer1.reschedule(now.plusHours(24))

    if(alarmEnabled.state == ON) {
	var Number day = now.getDayOfWeek
	if (((day == 1) && (alarmMonday.state == ON))	||
	    ((day == 2) && (alarmTuesday.state == ON))	||
	    ((day == 3) && (alarmWensday.state == ON))	||
	    ((day == 4) && (alarmThursday.state == ON))	||
	    ((day == 5) && (alarmFriday.state == ON))	||
	    ((day == 6) && (alarmSaturday.state == ON))	||
	    ((day == 7) && (alarmSunday.state == ON))) {
	    // The things to do if the alarm clock is enabled for this day of week:

	}
    }
    ]
    logInfo("rules","Alarm, timer1 set.")
    if (timer2 != null) {
	timer2.cancel
	timer2 = null
    }

    timer2 = createTimer(now.plusMinutes(delta3)) [|
    timer2.reschedule(now.plusHours(24))

    if(alarmEnabled.state == ON) {
	var Number day = now.getDayOfWeek
	if (((day == 1) && (alarmMonday.state == ON))	||
	    ((day == 2) && (alarmTuesday.state == ON))	||
	    ((day == 3) && (alarmWensday.state == ON))	||
	    ((day == 4) && (alarmThursday.state == ON))	||
	    ((day == 5) && (alarmFriday.state == ON))	||
	    ((day == 6) && (alarmSaturday.state == ON))	||
	    ((day == 7) && (alarmSunday.state == ON))) {
	    // The things to do if the alarm clock is enabled for this day of week: 

	}
    }
    ]
    logInfo("rules","Alarm, timer2 set.")
    if (timer3 != null) {
	timer3.cancel
	timer3 = null
    }

    timer3 = createTimer(now.plusMinutes(delta4)) [|
    timer3.reschedule(now.plusHours(24))

    if(alarmEnabled.state == ON) {
	var Number day = now.getDayOfWeek
	if (((day == 1) && (alarmMonday.state == ON))	||
	    ((day == 2) && (alarmTuesday.state == ON))	||
	    ((day == 3) && (alarmWensday.state == ON))	||
	    ((day == 4) && (alarmThursday.state == ON))	||
	    ((day == 5) && (alarmFriday.state == ON))	||
	    ((day == 6) && (alarmSaturday.state == ON))	||
	    ((day == 7) && (alarmSunday.state == ON))) {
	    // The things to do if the alarm clock is enabled for this day of week: 

	}
    }
    ]
    logInfo("rules","Alarm, timer3 set.")
    } finally {
	lock1.unlock()
	}
end

rule "Initialization"
when
    System started
then
     postUpdate(alarmEnabled, ON)
     // postUpdate(radioEnabled, ON)
     postUpdate(alarmTimeHour,  6)
     postUpdate(alarmTimeMinutes, 30)
     postUpdate(alarmOffset1Minutes, 20)
     postUpdate(alarmOffset2Minutes, 15)
     postUpdate(alarmOffset3Minutes, 50)
     postUpdate(alarmMonday, ON)
     postUpdate(alarmTuesday, ON)
     postUpdate(alarmWensday, ON)
     postUpdate(alarmThursday, ON)
     postUpdate(alarmFriday, ON)
     postUpdate(alarmSaturday, OFF)
     postUpdate(alarmSunday, OFF)
end`

I do not understand this error message what should I review and correct in the code.
May I ask you for your help to give me some direction where should I look?

Much appreciated for your help!
Gergely

remove the last “or”

rule "Alarm"
when
    Item alarmTimeHour changed or
    Item alarmTimeMinutes changed or
    Item alarmOffset1Minutes changed or
    Item alarmOffset2Minutes changed or
    Item alarmOffset3Minutes changed
then
2 Likes

Oh my God, I’m so silly… Thank you very much! :slight_smile:

Note, this must be really old code. You do not need any of those imports from org.openhab in the Rule.

Also, avoid the use of primitives in your Rules where ever possible. It can add dozens of minutes to the amount of time it takes the file to parse, especially when running on an RPi but it’s even noticeable on beefy machines.

And there are lines here that are meaningless.

alarm1 = alarm1.intValue

You declared alarm1 as an int. It can be nothing but an int. calling intValue doesn’t do anything. But becayse alarm1 is declared as an int and you are calling intValue needlessly here it is almost certainly adding a good deal of load on how long it takes this file to load.

ReentrantLocks can be very dangerous and I’m not sure they are needed here. The likelihood that two instances of this Rule would ever need to run at the same time seem highly unlikely. Only use it if you are certain it’s needed.

Thank you very much for all your help!
You are right, I just started to learn and I use the codes to figure it our how it is working, so much appreciated for all feedback and help.

I will try to rewrite this code in this case and rethink the base of it.