Extending Blockly with new openHAB commands

Hi Rich,

function arguments

By any chance, does it work with arguments?

As you can see in the following post blockly has a rather uncommon approach of parameters of a function which is basically a “no” to your question as a function doesn’t support local variables.

https://oucc.uk/index.php?action=content&id=47

Initialization

If it’s different from null we absolutely fo not want to set it to null .

I agree which is why I did it that way:

this.MyTimer=(this.MyTimer === undefined || this.MyTimer === null) ? null : this.MyTimer

which is the same like

this.MyTimer=(this.MyTimer !=== undefined && this.MyTimer !=== null) ? this.MyTimer : null

→ If the timer is null or undefined then set it to null, otherwise keep the value. With an if-statement I could have omitted the the assignment of the original value but the ternary operator doesn’t allow that. However, I find the latter more concise in the code or would you prefer an if like so?

if (this.MyTimer === undefined) {
  this.myTimer = null
}

Maybe this more clearer even when it was three lines line or we just format it to be a one-liner

 if (this.MyTimer === undefined) { this.myTimer = null}

I think we should go for the if-one liner, shouldn’t we?

Notifications

This is the code I am generating:

var notificationAction = Java.type("org.openhab.io.openhabcloud.NotificationAction")

notificationAction.sendNotification('you@email.com','Message')

which results into

2021-10-19 08:59:39.734 [WARN ] [e.automation.internal.RuleEngineImpl] - Fail to execute action: 2
java.lang.RuntimeException: java.lang.ClassNotFoundException: org.openhab.io.openhabcloud.NotificationAction cannot be found by org.openhab.core.automation.module.script_3.2.0.M3
	at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:531) ~[jdk.scripting.nashorn:?]
	at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:456) ~[jdk.scripting.nashorn:?]
	at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:413) ~[jdk.scripting.nashorn:?]
	at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:409) ~[jdk.scripting.nashorn:?]
	at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:162) ~[jdk.scripting.nashorn:?]

which btw also tells me we are on Nashorn.

The code of the action could be found here. As you can see it is code within a bundle. Now that I know the issue I am able to ask the right question :wink: and therefore found the thread that already deals with it:

@5iver recommends using the openhab-scripters library.

var OPENHAB_CONF = Java.type('java.lang.System').getenv('OPENHAB_CONF')
load(OPENHAB_CONF + '/automation/lib/javascript/core/actions.js')
NotificationAction.sendNotification('mail@me.com','Test notification')

Apart from the fact that even though I have installed these libs and I then get an exception

org.eclipse.smarthome.model.script.actions.Exec cannot be found by org.openhab.core.automation.module.script_3.2.0.M3

I think this solution is not a solution because it references the openhab-scripters library which is not delivered by default with openhab and is far from easy to be installed.

@Kai @cweitkamp As you are the author of the original NotificationAction, do you have an idea or recommendation how we could solve that?