Blockly sendCommand via script and variable

OH4.0.1

I found an issue that worked before update to OH4.0.1. Now i get an error but tried different things and find a confusing solution and would ask if this is a bug or must i do this everytime:

grafik

Code show this:

i.sendCommand(calVal);

It’s only working if I do this to get getItem back:

grafik

items.getItem(items.getItem(i).name).sendCommand(calVal);

Thanks

What is i? Meaning how is it populated?

I don’t know if this has changed or not, but the send command block requires the name of an Item, not the Item itself.

I send from a rule this to a script

and that is i result from.

grafik

That should be a String then. Can you log i to verify?

Get this error

2023-08-14 20:26:07.157 [INFO ] [ation.script.ui.dewPointCalcFunction] - SchlafzimmerTaupunkt
2023-08-14 20:26:07.164 [ERROR] [b.automation.script.javascript.stack] - Failed to execute script:
org.graalvm.polyglot.PolyglotException: TypeError: i.sendCommand is not a function
	at <js>.:program(<eval>:19) ~[?:?]
	at org.graalvm.polyglot.Context.eval(Context.java:399) ~[bundleFile:?]
	at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:458) ~[bundleFile:?]
	at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:426) ~[bundleFile:?]
	at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:262) ~[java.scripting:?]
	at org.openhab.automation.jsscripting.internal.scriptengine.DelegatingScriptEngineWithInvocableAndAutocloseable.eval(DelegatingScriptEngineWithInvocableAndAutocloseable.java:53) ~[bundleFile:?]
	at org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.eval(InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.java:78) ~[bundleFile:?]
	at org.openhab.automation.jsscripting.internal.scriptengine.DelegatingScriptEngineWithInvocableAndAutocloseable.eval(DelegatingScriptEngineWithInvocableAndAutocloseable.java:53) ~[bundleFile:?]
	at org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.eval(InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.java:78) ~[bundleFile:?]
	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) ~[?:?]
	at org.openhab.core.automation.internal.RuleEngineImpl.executeActions(RuleEngineImpl.java:1188) ~[?:?]
	at org.openhab.core.automation.internal.RuleEngineImpl.runNow(RuleEngineImpl.java:1039) ~[?:?]
	at <js>.runRule(@openhab-globals.js:2) ~[?:?]
	at <js>.:program(<eval>:4) ~[?:?]
	at org.graalvm.polyglot.Context.eval(Context.java:399) ~[bundleFile:?]
	at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:458) ~[bundleFile:?]
	at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:426) ~[bundleFile:?]
	at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:262) ~[java.scripting:?]
	at org.openhab.automation.jsscripting.internal.scriptengine.DelegatingScriptEngineWithInvocableAndAutocloseable.eval(DelegatingScriptEngineWithInvocableAndAutocloseable.java:53) ~[bundleFile:?]
	at org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.eval(InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.java:78) ~[bundleFile:?]
	at org.openhab.automation.jsscripting.internal.scriptengine.DelegatingScriptEngineWithInvocableAndAutocloseable.eval(DelegatingScriptEngineWithInvocableAndAutocloseable.java:53) ~[bundleFile:?]
	at org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.eval(InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.java:78) ~[bundleFile:?]
	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) ~[?:?]
	at org.openhab.core.automation.internal.RuleEngineImpl.executeActions(RuleEngineImpl.java:1188) ~[?:?]
	at org.openhab.core.automation.internal.RuleEngineImpl.runNow(RuleEngineImpl.java:1039) ~[?:?]
	at org.openhab.core.automation.rest.internal.RuleResource.runNow(RuleResource.java:381) ~[?:?]
	at org.openhab.core.automation.rest.internal.RuleResource.runNow(RuleResource.java:398) ~[?:?]
	at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
	at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[?:?]
	at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
	at java.lang.reflect.Method.invoke(Method.java:568) ~[?:?]
	at org.apache.cxf.service.invoker.AbstractInvoker.performInvocation(AbstractInvoker.java:179) ~[bundleFile:3.4.5]
	at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:96) ~[bundleFile:3.4.5]
	at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:201) ~[bundleFile:3.4.5]
	at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:104) ~[bundleFile:3.4.5]
	at org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:59) ~[bundleFile:3.4.5]
	at org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:96) ~[bundleFile:3.4.5]
	at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308) ~[bundleFile:3.4.5]
	at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121) ~[bundleFile:3.4.5]
	at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:265) ~[bundleFile:3.4.5]
	at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:234) ~[bundleFile:3.4.5]
	at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:208) ~[bundleFile:3.4.5]
	at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:160) ~[bundleFile:3.4.5]
	at org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:225) ~[bundleFile:3.4.5]
	at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:298) ~[bundleFile:3.4.5]
	at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doPost(AbstractHTTPServlet.java:217) ~[bundleFile:3.4.5]
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:517) ~[bundleFile:4.0.4]
	at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:273) ~[bundleFile:3.4.5]
	at org.ops4j.pax.web.service.spi.servlet.OsgiInitializedServlet.service(OsgiInitializedServlet.java:102) ~[bundleFile:?]
	at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:799) ~[bundleFile:9.4.50.v20221201]
	at org.eclipse.jetty.servlet.ServletHandler$ChainEnd.doFilter(ServletHandler.java:1656) ~[bundleFile:9.4.50.v20221201]
	at org.ops4j.pax.web.service.spi.servlet.OsgiFilterChain.doFilter(OsgiFilterChain.java:100) ~[bundleFile:?]
	at org.ops4j.pax.web.service.jetty.internal.PaxWebServletHandler.doHandle(PaxWebServletHandler.java:310) ~[?:?]
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143) ~[bundleFile:9.4.50.v20221201]
	at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:600) ~[bundleFile:9.4.50.v20221201]
	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127) ~[bundleFile:9.4.50.v20221201]
	at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:235) ~[bundleFile:9.4.50.v20221201]
	at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1624) ~[bundleFile:9.4.50.v20221201]
	at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:233) ~[bundleFile:9.4.50.v20221201]
	at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1440) ~[bundleFile:9.4.50.v20221201]
	at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:188) ~[bundleFile:9.4.50.v20221201]
	at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:505) ~[bundleFile:9.4.50.v20221201]
	at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1594) ~[bundleFile:9.4.50.v20221201]
	at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:186) ~[bundleFile:9.4.50.v20221201]
	at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1355) ~[bundleFile:9.4.50.v20221201]
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) ~[bundleFile:9.4.50.v20221201]
	at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:234) ~[bundleFile:9.4.50.v20221201]
	at org.ops4j.pax.web.service.jetty.internal.PrioritizedHandlerCollection.handle(PrioritizedHandlerCollection.java:96) ~[?:?]
	at org.eclipse.jetty.server.handler.gzip.GzipHandler.handle(GzipHandler.java:722) ~[bundleFile:9.4.50.v20221201]
	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127) ~[bundleFile:9.4.50.v20221201]
	at org.eclipse.jetty.server.Server.handle(Server.java:516) ~[bundleFile:9.4.50.v20221201]
	at org.eclipse.jetty.server.HttpChannel.lambda$handle$1(HttpChannel.java:487) ~[bundleFile:9.4.50.v20221201]
	at org.eclipse.jetty.server.HttpChannel.dispatch(HttpChannel.java:732) ~[bundleFile:9.4.50.v20221201]
	at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:479) ~[bundleFile:9.4.50.v20221201]
	at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:277) ~[bundleFile:9.4.50.v20221201]
	at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:311) ~[bundleFile:9.4.50.v20221201]
	at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:105) ~[bundleFile:9.4.50.v20221201]
	at org.eclipse.jetty.io.ChannelEndPoint$1.run(ChannelEndPoint.java:104) ~[bundleFile:9.4.50.v20221201]
	at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.runTask(EatWhatYouKill.java:338) ~[bundleFile:9.4.50.v20221201]
	at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:315) ~[bundleFile:9.4.50.v20221201]
	at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.tryProduce(EatWhatYouKill.java:173) ~[bundleFile:9.4.50.v20221201]
	at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:131) ~[bundleFile:9.4.50.v20221201]
	at org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:409) ~[bundleFile:9.4.50.v20221201]
	at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:883) ~[bundleFile:9.4.50.v20221201]
	at org.eclipse.jetty.util.thread.QueuedThreadPool$Runner.run(QueuedThreadPool.java:1034) ~[bundleFile:9.4.50.v20221201]
	at java.lang.Thread.run(Thread.java:833) ~[?:?]
2023-08-14 20:26:07.201 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'dewPointCalcFunction' failed: org.graalvm.polyglot.PolyglotException: TypeError: i.sendCommand is not a function

That’s not what I asked for. I need you to log out i so I can guess at what it is since it doesn’t seem to be a String.

What code is generated by the send command block?

I don’t understand you. :frowning: What are you mean with log out i? I send you everything… I send you my rule and the script which is requested by the rule.

Doesn’t work:

var t, h, calVal, i, a, b, sdd, dd, v, r;


t = ctx['temp'];
h = ctx['hum'];
i = ctx['dewItem'];
a = 7.6;
b = 240.7;
if (t >= 0) {
  a = 7.5;
  b = 237.3;
}
sdd = 6.112 * Math.pow(10,(a * t) / (b + t));
dd = sdd * (h / 100);
v = Math.log(dd / 6.107) / Math.log(10);
r = (b * v) / (a - v);
calVal = Math.round((r * 10)) / 10;
i.sendCommand(calVal);

and this is the working code:

var t, h, i, a, b, sdd, dd, v, r, calVal;


t = ctx['temp'];
h = ctx['hum'];
i = ctx['dewItem'];
a = 7.6;
b = 240.7;
if (t >= 0) {
  a = 7.5;
  b = 237.3;
}
sdd = 6.112 * Math.pow(10,(a * t) / (b + t));
dd = sdd * (h / 100);
v = Math.log(dd / 6.107) / Math.log(10);
r = (b * v) / (a - v);
calVal = Math.round((r * 10)) / 10;
items.getItem(items.getItem(i).name).sendCommand(calVal);

As per my understanding the issue is that in oh3.x the same block produces different code.

In oh3.4 I get for snedCommand block:

events.sendCommand(<itemname as string>,<newState>)

While in oh4 you have

<Item as object>.sendCommand(<newState>)

@stefan.hoehn maybe you can add more details?

image

where m1 is i.

What does that add to openhab.log?

What do you mean with “issue”. If you are on oh4, just save the blocks again and it will generate the right blocks for you.

In general, variables are tricky for Blockly because it does not know what the type of the content of the variable is. If you add real blocks it will only allow those it knows ans then tries to be smart and generate the right code for you.

With variables there is the requirement to provide the right type.

From my understanding the issue is, that the same block produces different code.

In oh3 you had to pass the item name as string to send command block, in oh4 you obviously need a real item, not just a name.

Therefore users need to change the script and not only press the save button, if I’m understanding correct.

The send command block is changing its behaviour if you use with a variable

With Item

items.getItem('Fassade_state').sendCommand('value');

with variable

i.sendCommand('value');

So, yes you need to tell the block that your variable is actually a item
Unbenannt

you should not need the “get name of item” block because you stored the name of the item already in the dictionary as “KuecheTaupunkt”

1 Like

Yes that also do the trick. That sendCommand can’t handle variables as before with OH3.4. You must set get item, that sendCommand handle it write. But does this make any sense? sendCommand must handle always an item, i don’t have a clue in which case you don’t need it.
Guys what do you guess?

A command is always sent to an item, if you use a variable you need “get item”
The only case a variable is working without “get item” is if you loop through a group of items like this

Unbenannt

producing this code

var i;
var i_list = items.getItem('Vorhaus_Licht').members;
for (var i_index in i_list) {
  i = i_list[i_index];
  i.sendCommand('ON');
}

OK agree. Then it’s solved for me. Thanks.

I don’t think that you have defined an item and that’s why i.sendCommand is not working.

You need to properly define an item like this:
image
Then you can use “i.sendCommand” in an easy way:
image

The resulting code is correct:

var i;
i = items.getItem('TestItem');
i.sendCommand('OFF');

Yes, sure that’s also a possible way to do this. Thanks.

I believe it would work to send the Item in the calling rule instead of just the name of the item by changing

image

to a get item. Then you wouldn’t need the get item in the called rule.

OK that is the next solution maybe a better one. Cause you can pick the item and you can’t do a mistake with the name here.

I have just found another Blocky issue following an update to OH 4.0.1 This is a simplified version of a rule that I use a lot.

This the following code

if (event.itemState.toString() == 'OFF') {
  cache.private.put('Liters', items.getItem('IrrL1').state);
  items.getItem('IrrL1').sendCommand(((cache.private.get('Liters')) + 27));
} else {
  items.getItem('IrrL1').sendCommand(123);
}

The rule is triggered by a switch. When the switch goes “ON”, it should set the point variable “IrrL1” to 123. When the switch goes “OFF”, it should add 27 to the variable, giving 150. Instead it gives the value 12327. So the code is appending the value rather than adding it.

023-10-04 15:28:42.598 [INFO ] [openhab.event.ItemCommandEvent ] - Item ‘IrrigationEnable’ received command ON
2023-10-04 15:28:42.599 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item ‘IrrigationEnable’ changed from OFF to ON
2023-10-04 15:28:42.601 [INFO ] [openhab.event.ItemCommandEvent ] - Item ‘IrrL1’ received command 123
2023-10-04 15:28:42.603 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item ‘IrrL1’ changed from 12327 to 123

Is this a bug, or do I have to take some action to ensure that “IrrL1” is treated as a numeric variable rather than a string.

PJG