OH4 Rules/Blockly contextual triggering item name

Hello,

just migrated to 4.1.2 (Docker), after some hints most things work well, but i dont know how to change the following rule, it worked well on 3.4.5.
Triggering item name for example: SchlafziRolladenBalkonProxy
Item that shall recieve command: SchlafziRolladenBalkonLevel
Thanks for help!

Logs after execution:

2024-03-30 13:20:40.263 [ERROR] [b.automation.script.javascript.stack] - Failed to execute script:
org.graalvm.polyglot.PolyglotException: TypeError: (intermediate value).Transformation.transform(…).sendCommand is not a function
at .:program(:8) ~[?:?]
at org.graalvm.polyglot.Context.eval(Context.java:399) ~[?:?]
at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:458) ~[?:?]
at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:426) ~[?:?]
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:262) ~[java.scripting:?]
at org.openhab.automation.jsscripting.internal.scriptengine.DelegatingScriptEngineWithInvocableAndAutocloseable.eval(DelegatingScriptEngineWithInvocableAndAutocloseable.java:53) ~[?:?]
at org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.eval(InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.java:78) ~[?:?]
at org.openhab.automation.jsscripting.internal.scriptengine.DelegatingScriptEngineWithInvocableAndAutocloseable.eval(DelegatingScriptEngineWithInvocableAndAutocloseable.java:53) ~[?:?]
at org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.eval(InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.java:78) ~[?:?]
at org.openhab.core.automation.module.script.internal.handler.ScriptActionHandler.lambda$0(ScriptActionHandler.java:71) ~[?:?]
at java.util.Optional.ifPresent(Optional.java:178) [?:?]
at org.openhab.core.automation.module.script.internal.handler.ScriptActionHandler.execute(ScriptActionHandler.java:68) [bundleFile:?]
at org.openhab.core.automation.internal.RuleEngineImpl.executeActions(RuleEngineImpl.java:1188) [bundleFile:?]
at org.openhab.core.automation.internal.RuleEngineImpl.runRule(RuleEngineImpl.java:997) [bundleFile:?]
at org.openhab.core.automation.internal.TriggerHandlerCallbackImpl$TriggerData.run(TriggerHandlerCallbackImpl.java:87) [bundleFile:?]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539) [?:?]
at java.util.concurrent.FutureTask.run(FutureTask.java:264) [?:?]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) [?:?]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) [?:?]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) [?:?]
at java.lang.Thread.run(Thread.java:840) [?:?]
2024-03-30 13:20:40.265 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘92f191c443’ failed: org.graalvm.polyglot.PolyglotException: TypeError: (intermediate value).Transformation.transform(…).sendCommand is not a function

Ok, got the solution. First i changed the rule with blockly to:
Screenshot 2024-03-30 190345

Then i edited the code directly and changed

Level_Item.sendCommand('1');

to

items.getItem(Level_Item).sendCommand('1');

But can this be done within Blockly?
Thanks!

OK, i got the Blockly-solution, i simply had to insert a “get item”…
Screenshot 2024-03-30 191833

Maybe this Thread helps anybody with the same question :slight_smile:

I’d consider this a bug in blockly

Nope, it is a bit more complex :wink:

IHMO, This is typical unknown “var”-type blockly problem. Blockly cannot detect the type of the variable. Therefore the block where the variable block is added to needs to expect a certain type. In the case of the send command (like in the backing js command) eventually requires the item name to be provided. If a non-var-block is attached, the blockly code I implemented is smart enough to work with the item object (where it then retrieves the name from it) or the the item name.
If a var is attached blockly does expect the name in the variable:

  • var: must contain name
  • non-var block: may be the item object (getItem) or the item-block (which only holds the name).

Alternatively, you could set level-item to get item transform....
This way you don’t have to repeat getitem in your sendcommands.

1 Like

After thinking about it a bit further, items.getItem() needs to be made to check the type of argument, if it’s string, do the usual, and if it’s an item object, simply return the passed argument. But this is a change on jsscripting library and not blockly.

I think the objection I read in past was that this would add an extra step at runtime and that is something @florian-h05 thinks should be avoided.

Because someone who writes his own code would know whether he has an Item or only the name, I am against adding something like that to openhab-js.

But Blockly could be adjusted to generate something like:

(typeof var === „string“ ? items.getItem(var) : var