Using random timers within group for roller shutters fails

This should be an easy thing to do. I have a group of roller shutters that should open at sunrise. But instead of opening all simultaneously they should open over a random period of about 2 minutes, like when operated from a person that walks from shutter to shutter.

Iterating over a group is not the problem, if I omit the “createTimer” part everything works fine and all shutters rise. But how do I add a different random interval to each shutter?

var Integer RollerShutterRandom = 0

rule "Roller shutter UP at sunrise"
when
    Channel 'astro:sun:local:rise#event' triggered START
then
    group_rollershutter.members.forEach[ rollershutter | {
        RollerShutterRandom = (Math::floor((Math::random * 120).doubleValue).intValue)
        RollerShutterTimer = createTimer(now.plusSeconds(RollerShutterRandom), [|
            rollershutter.sendCommand(UP)
        ])
    }]
end

I assume the “rollershutter” item is not longer valid when the timer fires, but how do I solve this?
I’m a programmer for nearly 30 years now, but openHAB rules are always driving me nuts!
Instead of “just working” every little change only creates unexpected exceptions or something like that :frowning:

2018-04-24 11:22:27.233 [ERROR] [org.quartz.core.JobRunShell         ] - Job DEFAULT.2018-04-24T11:22:27.232+02:00: Proxy for org.eclipse.xtext.xbase.lib.Procedures$Procedure0: [ | {
  <XFeatureCallImplCustom>.sendCommand(<XFeatureCallImplCustom>)
} ] threw an unhandled Exception: 
java.lang.NullPointerException: null
	at org.eclipse.smarthome.model.script.engine.ScriptError.<init>(ScriptError.java:66) [145:org.eclipse.smarthome.model.script:0.10.0.b1]
	at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.invokeFeature(ScriptInterpreter.java:130) [145:org.eclipse.smarthome.model.script:0.10.0.b1]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:901) [164:org.eclipse.xtext.xbase:2.12.0.v20170519-0752]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:864) [164:org.eclipse.xtext.xbase:2.12.0.v20170519-0752]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:223) [164:org.eclipse.xtext.xbase:2.12.0.v20170519-0752]
	at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.doEvaluate(ScriptInterpreter.java:215) [145:org.eclipse.smarthome.model.script:0.10.0.b1]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:203) [164:org.eclipse.xtext.xbase:2.12.0.v20170519-0752]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.evaluateArgumentExpressions(XbaseInterpreter.java:1115) [164:org.eclipse.xtext.xbase:2.12.0.v20170519-0752]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._invokeFeature(XbaseInterpreter.java:1045) [164:org.eclipse.xtext.xbase:2.12.0.v20170519-0752]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.invokeFeature(XbaseInterpreter.java:991) [164:org.eclipse.xtext.xbase:2.12.0.v20170519-0752]
	at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.invokeFeature(ScriptInterpreter.java:141) [145:org.eclipse.smarthome.model.script:0.10.0.b1]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:763) [164:org.eclipse.xtext.xbase:2.12.0.v20170519-0752]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:219) [164:org.eclipse.xtext.xbase:2.12.0.v20170519-0752]
	at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.doEvaluate(ScriptInterpreter.java:215) [145:org.eclipse.smarthome.model.script:0.10.0.b1]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:203) [164:org.eclipse.xtext.xbase:2.12.0.v20170519-0752]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:446) [164:org.eclipse.xtext.xbase:2.12.0.v20170519-0752]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:227) [164:org.eclipse.xtext.xbase:2.12.0.v20170519-0752]
	at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.doEvaluate(ScriptInterpreter.java:215) [145:org.eclipse.smarthome.model.script:0.10.0.b1]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:203) [164:org.eclipse.xtext.xbase:2.12.0.v20170519-0752]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.evaluate(XbaseInterpreter.java:189) [164:org.eclipse.xtext.xbase:2.12.0.v20170519-0752]
	at org.eclipse.xtext.xbase.interpreter.impl.ClosureInvocationHandler.doInvoke(ClosureInvocationHandler.java:46) [164:org.eclipse.xtext.xbase:2.12.0.v20170519-0752]
	at org.eclipse.xtext.xbase.interpreter.impl.AbstractClosureInvocationHandler.invoke(AbstractClosureInvocationHandler.java:29) [164:org.eclipse.xtext.xbase:2.12.0.v20170519-0752]
	at com.sun.proxy.$Proxy179.apply(Unknown Source) [?:?]
	at org.eclipse.smarthome.model.script.internal.actions.TimerExecutionJob.execute(TimerExecutionJob.java:49) [145:org.eclipse.smarthome.model.script:0.10.0.b1]
	at org.quartz.core.JobRunShell.run(JobRunShell.java:202) [115:org.eclipse.smarthome.core.scheduler:0.10.0.b1]
	at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573) [115:org.eclipse.smarthome.core.scheduler:0.10.0.b1]
2018-04-24 11:22:27.235 [ERROR] [org.quartz.core.ErrorLogger         ] - Job (DEFAULT.2018-04-24T11:22:27.232+02:00: Proxy for org.eclipse.xtext.xbase.lib.Procedures$Procedure0: [ | {
  <XFeatureCallImplCustom>.sendCommand(<XFeatureCallImplCustom>)
} ] threw an exception.
org.quartz.SchedulerException: Job threw an unhandled exception.
	at org.quartz.core.JobRunShell.run(JobRunShell.java:213) [115:org.eclipse.smarthome.core.scheduler:0.10.0.b1]
	at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573) [115:org.eclipse.smarthome.core.scheduler:0.10.0.b1]
Caused by: java.lang.NullPointerException
	at org.eclipse.smarthome.model.script.engine.ScriptError.<init>(ScriptError.java:66) ~[?:?]
	at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.invokeFeature(ScriptInterpreter.java:130) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:901) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:864) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:223) ~[?:?]
	at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.doEvaluate(ScriptInterpreter.java:215) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:203) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.evaluateArgumentExpressions(XbaseInterpreter.java:1115) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._invokeFeature(XbaseInterpreter.java:1045) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.invokeFeature(XbaseInterpreter.java:991) ~[?:?]
	at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.invokeFeature(ScriptInterpreter.java:141) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:763) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:219) ~[?:?]
	at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.doEvaluate(ScriptInterpreter.java:215) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:203) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:446) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:227) ~[?:?]
	at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.doEvaluate(ScriptInterpreter.java:215) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:203) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.evaluate(XbaseInterpreter.java:189) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.ClosureInvocationHandler.doInvoke(ClosureInvocationHandler.java:46) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.AbstractClosureInvocationHandler.invoke(AbstractClosureInvocationHandler.java:29) ~[?:?]
	at com.sun.proxy.$Proxy179.apply(Unknown Source) ~[?:?]
	at org.eclipse.smarthome.model.script.internal.actions.TimerExecutionJob.execute(TimerExecutionJob.java:49) ~[?:?]
	at org.quartz.core.JobRunShell.run(JobRunShell.java:202) ~[?:?]
	... 1 more
2018-04-24 11:23:00.205 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule 'Roller shutter UP at sunrise': Couldn't invoke 'assignValueTo' for feature JvmVoid:  (eProxyURI: rollershutter.rules#|::0.2.0.2.0.0.7.0.1.0.0.0.1::0::/1)
1 Like

Are you running SNAPSHOT? If yes add a space after the [

Declare the timer at the top of the rule file

var Timer RollerShutterTimer = null

Works, thank you very, very much :smile:
Though I do not use SNAPSHOT it does not seem to hurt to add a space after the [.
The critical point seems to be declaration of the timer variable.

I would not have found the error, why this should an “error message” for an undeclared variable is beyond me :persevere:
This really needs some work under the hood to clearly indicate what’s wrong within a rule…

2018-04-24 13:03:49.186 [ERROR] [org.quartz.core.JobRunShell         ] - Job DEFAULT.2018-04-24T13:03:49.185+02:00: Proxy for org.eclipse.xtext.xbase.lib.Procedures$Procedure0: [ | {
  <XFeatureCallImplCustom>.sendCommand(<XFeatureCallImplCustom>)
} ] threw an unhandled Exception: 
java.lang.NullPointerException: null
	at org.eclipse.smarthome.model.script.engine.ScriptError.<init>(ScriptError.java:66) [145:org.eclipse.smarthome.model.script:0.10.0.b1]
(goes on for a few dozen lines...)

Exceptions like these are generated deep within library code that OH has no ability to influence. I’m expecting that error reporting will be much better with the Experimental Rules Engine.

For the record, the root of the call stack is the Quartz scheduler indicating that the error occurred when a Timer tried to execute the lambda (i.e. the function defined with the [ | ]). The <XFeatureCallImplCustom> part further indicates the error took place in a lambda.

Unfortunately when Xtend is unable to automatically convert a type into a usable type (e.g. convert a Number to a String) it throws a NullPointerException.

Anyway, no one would expect most average users to be able to figure this out. However, there is VSCode with the openHAB extension which would have immediately identified RollerShutterTimer as not being defined.

Thank you for your detailed explanation. I would not have figured this out, although I’m a programmer for 30 years. Xtend is just to strange :wink:

I did use the latest VSCode to edit the rule and I also have the openHAB extension installed, which is, as far as I can tell, also talking to the openHAB API on my server.
I did not see any problems with the RollerShutterTimer variable there. I just tried it again, deleting the declaration on top, and the editor was quite happy. Is there anything to configure there?

If you insert some other error does it highlighting it? I just want to verify that VSCode is connecting to the OH language server.

If it does there needs to be an issue opened in the VSCode e openHAB xtension GitHub.

I investigated this a little more. It seems I need the addon “misc-lsp” on my server, however, the add-on does not appear on my Paper UI add-ons page. Looks like it’s not packaged with the openHAB 2.2.0 release I’m using. I’ll try to install the add-on manually later this evening and will report back.

It should be. This is odd indeed. @kubawolanin, do you have any ideas?

Is this the language server we’re talking about? This is from the list of all installed bundles from the karaf console:

138 │ Active │ 80 │ 0.10.0.b1 │ Eclipse SmartHome Language Server │ mvn:org.eclipse.smarthome.model/org.eclipse.smarthome.model.lsp/0.10.0.b1

Although it is not listed on my Paper UI add-ons page the add-on seems to be running, at least the openHAB process is listening on port 5007:

root@HomeServer:~# netstat -l -p | grep 5007
tcp6       0      0 [::]:5007               [::]:*                  LISTEN      22795/java

This is my VSCode user configuration for the openHAB extension:

    "openhab.host": "192.168.178.17",
    "openhab.port": 8080,
    "openhab.useRestApi": true,
    "openhab.lspEnabled": true,

I see no error messages in VSCode about no being able to connect to openHAB, but I see no syntax checks, either :thinking:

Yes

It wouldn’t be. It’s a core bundle, part of OH and it should always be there.

Very odd. When you start using VSCode or after making some edits do you see any little popups in the lower right corner indicating that the extension couldn’t communicate with the language server?

image

Is that config in your User Settings or Workspace Settings? I’ve had some issues in the past where there was an empty workspace config which overrode my User Config effectively wiping out the openhab.host field and disabling my syntax checking. I never looked into it after that to verify that is what was happening, though it could be something to look into. You can check by looking to see if there is a .vscode folder in your conf folder. If so stop VSCode, delete it, and try again. See if that makes a difference.

I’ve not seen anyone report this sort of problem before so I’ve not a lot of ideas.

Thanks Rich for your help, it was indeed a problem with VSCode! I did have empty workspace settings and a .VSCode folder in my openHAB conf directory. However, deleting the .VSCode folder did not help.
Instead I moved the openHAB settings from my user settings to the workspace settings and immediately got hints about errors in my current rule file :grinning:

My next step: try to figure out how to access the karaf console from my VSCode Windows environment :thinking:

I open a terminal (under tools I think) and ssh to the console from there. Though if course I don’t think that it would work so smoothly running VSCode on Windows. I suppose you would need a command line ssh command. I believe git and putty come with one you can call.

Also, in the likely event that you VSCode is running on a different machine from your OH server, https://docs.openhab.org/administration/console.html#bind-console-to-all-interfaces

It seems on Windows the internal VSCode terminal is currently really broken :face_with_raised_eyebrow:
There are several open issues about this on the VSCode git page.

For me any internal terminal, be it bash, cmd.exe or powershell onyl works when VSCode is started with administrator privileges. But then my openHAB network share is not mapped, and starting an editor with admin rights is wrong, anyway. So I wait until this is fixed.

Remotely accessing the karaf console with putty works fine, though :slight_smile:
Thanks for all your help so far!