[SOLVED] Timer error message: Could not invoke method: [....] on instance: null

Hello,

I am starting out on openhab and I must say that I like it a lot. There is only one thing I do not get working properly.
Yesterday it worked fine, but today I am fiddling around with the code and I broke it somehow.
The example I got my code from is https://github.com/openhab/openhab/wiki/AlarmClock
What I got in my rule file is:

import org.openhab.model.script.actions.Timer
import org.joda.time.*

var Timer Wakeuptimer

rule "Set alarm" //--------------------
	when
		Item Wakeup_Time received update or
		Item Wakeup_alarm received update
	then
		logInfo("rules", "set alarm rule activated. Wakeup_Time = [{}]. Wakeup_alarm=[{}]", Wakeup_Time.state, Wakeup_alarm.state)
		if (Wakeuptimer!=null) 
			Wakeuptimer.cancel()
		
		logInfo("rules", "Alamtimer is set to start next alarm at: [{}].", Wakeup_Time.state)
		var Wakeuptimer=createTimer(Wakeup_Time) [| 
            if (Wakeup_alarm.state == ON) 
				Wakeup_event.sendCommand(ON)
		]
end

When the rule gets triggered I get the following message in my logfile:

2016-02-28 23:55:37.957 [INFO ] [org.openhab.model.script.rules] - set alarm rule activated. Wakeup_Time = [2016-02-29T08:00:00]. Wakeup_alarm=[ON]
2016-02-28 23:55:37.960 [INFO ] [org.openhab.model.script.rules] - Alamtimer is set to start next alarm at: [2016-02-29T08:00:00].
2016-02-28 23:55:37.962 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule 'Set alarm': Could not invoke method: org.openhab.model.script.actions.ScriptExecution.createTimer(org.joda.time.base.AbstractInstant,org.eclipse.xtext.xbase.lib.Procedures$Procedure0) on instance: null

Why does it have a problem with the fact that the timers instance is null?
Am I missing something or doing something stupid?

I don’t know how to do this the right way, but you can’t do createTimer(Item.state) as createTimer() wants joda.time.base.AbstractInstant and not a State. In fact, this is exactly the information from the error message.

Instead you could do createTimer(now.plusMinutes((Item.state as DecimalType).intValue))

1 Like

Thanks for the replies!

Tried your solution. Got rid of the Wakeup_alarm and placed it in the actual alarm loop. But no success.
Result:

rule "Set alarm" //--------------------
	when
		Item Wakeup_Time received update or
		Item Wakeup_alarm received command ON
	then
		logInfo("rules", "set alarm rule activated. Wakeup_Time = [{}]. Wakeup_alarm=[{}]", Wakeup_Time.state, Wakeup_alarm.state)
		if (Wakeuptimer!=null) 
			Wakeuptimer.cancel()
		Wakeuptimer=createTimer(Wakeup_Time.state) [|  
				Wakeup_event.sendCommand(ON)
		]
		logInfo("rules", "TIMER: [{}].", Wakeuptimer)
end

Gives me the same error:

2016-03-01 00:01:18.361 [INFO ] [org.openhab.model.script.rules] - set alarm rule activated. Wakeup_Time = [2016-03-01T08:00:00]. Wakeup_alarm=[ON]
2016-03-01 00:01:18.365 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule 'Set alarm': Could not invoke method: org.openhab.model.script.actions.ScriptExecution.createTimer(org.joda.time.base.AbstractInstant,org.eclipse.xtext.xbase.lib.Procedures$Procedure0) on instance: null

Trying to implement your selution. Changed the suggested line of code to:

		Wakeuptimer=createTimer(now.plusMinutes((Wakeup_Time.state as DecimalType).intValue)) [|  
				Wakeup_event.sendCommand(ON)
		]

Then I get the error message:

2016-03-01 00:19:59.475 [INFO ] [org.openhab.model.script.rules] - set alarm rule activated. Wakeup_Time = [2016-03-01T08:00:00]. Wakeup_alarm=[ON]
2016-03-01 00:19:59.486 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule 'Set alarm': Cannot cast org.openhab.core.library.types.DateTimeType to void

When I start playing with the timer things get a bit over my head. When I do:

            Testtimer= createTimer(now.plusSeconds(5)) [|
                if (Wakeup_alarm.state == ON)
                    Wakeup_event.sendCommand(ON)
                    logInfo("rules", "Wakeup event is initiated.")
                ]

Things run fine. But when I try to throw in a DateTime variable I get error messages. Am I doing something stupid?

        var DateTime starttime=Wakeup_Time.state
        starttime=starttime.plusMinutes(1)

Gives me:

2016-03-01 00:13:26.617 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule 'Set alarm': Could not invoke method: org.joda.time.DateTime.plusMinutes(int) on instance: 2016-03-01T08:00:00

Why is this?
A few days back I could throw in the datetime form this Item, but I do not seem to be able to reproduce it somehow.

[edit]
Is there some documentation on the DateTime or Timer methods someone can recommend?
[/edit]

joda time is not java DateTime, and you don’t need to use a DateTime Item but a Number Item with Integer, certainly there is another way to create a Timer from DateTime, but I don’t know how.

Found a solution!
Apparently the format was not correct/complete.
To make it work I added another step:

		var String blup = Wakeup_Time.state.toString
		var DateTime starttime = parse(blup)

Above code wont win any beauty contests, but it runs. Apparently the format was not correct so the Timer method did not accept it.

To show the difference from the log:

Wakeup_Time.state: 2016-03-03T08:00:00 
starttime:         2016-03-03T08:00:00.000+01:00

So the milliseconds and the time zone where missing, that’s all. :dizzy_face:

@m0wlheld and @Udo_Hartmann Thank you both for your time!