[SOLVED] Javascript: createTimer fails with Java cast error

I am experiencing issues on my javascript code. Using createTimer does not work any longer.

createTimer 180 Error:java.lang.ClassCastException: Cannot cast org.joda.time.DateTime to org.joda.time.base.AbstractInstant

The code has not been touched for ages:

this.timer = createTimer(JodaNow().plusMillis(250), obj.timer_callback.bind(obj) );

I am currently using OpenHab 2.4.
Can somebody shed some light on this or help me converting DateTime to base.AbstractInstant?

I guess you are talking about rules, not javascript?

now.plusMillis(250)

If you have any
import org.joda.time.*
remove them

No, it’s Javascript.
JodaNow returns org.joda.time.DateTime.now

Are you using the helper libraries from here… https://github.com/openhab-scripters/openhab-helper-libraries?

If you haven’t done anything to OH or your scripts, something else must have changed. OS update? Java update?

No, I am using an older version of those. But I carefully compared the code and it is equivalent.
All examples - whether jython or javascript - pass org.joda.time.DateTime to ScriptExecution.createTimer. createTimer however expects org.joda.time.base.AbstractInstant
According to the information I found AbstractInstant is a base class of DateTime and as such an explicit cast should not be needed.

The JS timer example works for me on S1674… https://openhab-scripters.github.io/openhab-helper-libraries/Examples/Timer%20Example.html

Thanks a lot for the help. Much appreciated.

I created a bare bone example and checked it against the reference implementation you linked:

var ScriptExecution 		= Java.type("org.eclipse.smarthome.model.script.actions.ScriptExecution");
var DateTime 				= Java.type("org.joda.time.DateTime");

ScriptExecution.createTimer(DateTime.now().plusSeconds(1), function () { logError("Called"); });

results in

2019-09-08 03:42:50.007 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab2/automation/jsr223/dimmers.js': Cannot cast org.joda.time.DateTime to org.joda.time.base.AbstractInstant

The only change I can remember actually was to stop openhab, clear cache and temp and reboot openhabian.

Pretty late now, need some sleep. I’ll stop openhab tomorrow and clear cache and tmp again and restart and try again. The only extra on top of stock 2.4 openhabian is a 2.5 zwave binding which requires to install (feature:install) a new serial library. Want to exclude this as a cause.
Thanks a lot for all the support.

No prob… I really really doubt the zwave binding would cause an issue here. Check your Java version.

This works for me too…

'use strict';
var log = Java.type("org.slf4j.LoggerFactory").getLogger("jsr223.javascript.TEST");
var ScriptExecution 		= Java.type("org.eclipse.smarthome.model.script.actions.ScriptExecution");
var DateTime 				= Java.type("org.joda.time.DateTime");

ScriptExecution.createTimer(DateTime.now().plusSeconds(1), function () { log.debug("Called"); });
1 Like

Seems it will be easier to go for 2.5 snapshots than trying to solve this issue. Here’s the java version information:

$ java -version
openjdk version "1.8.0_152"
OpenJDK Runtime Environment (Zulu Embedded 8.25.0.76-linux-aarch32hf) (build 1.8.0_152-b76)
OpenJDK Client VM (Zulu Embedded 8.25.0.76-linux-aarch32hf) (build 25.152-b76, mixed mode, Evaluation)

2.5M3 just came out today

openjdk version “1.8.0_212”
OpenJDK Runtime Environment (build 1.8.0_212-b04)
OpenJDK 64-Bit Server VM (build 25.212-b04, mixed mode)

The error seems to be caused by openhab stable 2.4.0 on openhabian.
I have now upgraded to the latest snapshot using openhabian-config.
Problem solved.
Needed to fix some issues in my scripts. Hope to migrate to current helper libraries soon.
A big thanks to @5iver and @rossko57 for their support.

2 Likes