Get ON or OFF status of a Color Item

Hi all,
I am running OpenHab 3.4.2 and I need to get ON-OFF status of a color Item. I have WLED binding that expose channel “Master Control”

This is a Color Item. It accept ON and OFF command, but when i got state, it return me HSB, something like 12,12,100. Instead of this i need ON and OFF status to use into a rule condition:


This do not work.
Is there a way to evaluate ON-OFF status here, or into a script (ie Blockly)?

Thanks in advance

You’ll need to use a Script Condition and there you can use getStateAs(). Rules DSL it would look something like:

WLEDSalottoLibreria_Segment_Master_Controls.getStateAs(OnOffType) == OFF

In JS Scriping in 3.4.2 with the default openhab-js

var {OnOffType} = require('@runtime');
items.getItem('WLEDSalottoLibreria_Segment_Master_Controls').rawItem.getStateAs(OnOffType).toString() == 'OFF';

With the latest 4.1 verison of openhab-js

var {OnOffType} = require('@runtime');
items.WLEDSalottoLibreria_Segment_Master_Controls.rawItem.getStateAs(OnOffType).toString == 'OFF';

There are of course lots of other ways to do this too but this is the most straight forward.

1 Like

HI,
is there a way to do it in Blockly?

Thanks

I don’t think there is a block for getStateAs(). You could use a custom script block, but at that point you may as well just use JS Scripting directly.

You can link a second item to your “Master Controls” channel with type switch.
Then you can add this item in your rule condition.

1 Like

Hi I did this:
image

into inline script there is this code:

var {OnOffType} = require('@runtime');
MasterState = items.getItem('WLEDSalottoLibreria_Segment_Master_Controls').rawItem.getStateAs(OnOffType).toString()

in the log I got this error:

2023-03-09 21:17:04.985 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'wled-salotto-turn-on-brightness' failed: <eval>:7:4 Expected ident but found {
var {OnOffType} = require('@runtime');
    ^ in <eval> at line number 7 at column number 4

I know that is not strictly necessary in my case, but I would like to understand the mechanism.

Thanks

That’s right, on OH 3.4.2 Blockly uses Nashorn JS Scripting but it’s been migrated to JS Scripting ECMAScript 2021 in OH 4. So you’ll have to use a different syntax.

ir.getItem('WLEDSalottoLibreria_Segment_Master_Controls').getStateAs(OnOffType) == ON;

Note there’s nothing to import or require because OnOffType is already imported.

Also note, all that “set MasterState” stuff is sulfurous. You don’t need it and I’m not certain the condition will work with it. It certainly won’t work as written.

Remember, the last line executed needs to evaluate to a boolean. In this case, you only need the one line. So you could make it more complicated and just have the one “inline script” block with that one line in it, or just use a JS script condition with the one line in it.

In this case, trying to use Blockly is making things more complicated. But if you insist

Which converts to

var state;


state = ir.getItem('MyItem').getStateAs(OnOffType).toString()

state == 'ON';

As opposed to choosing an ECMAScript 5.1 script condition which would just be

ir.getItem('MyItem').getStateAs(OnOffType).toString() == 'ON';

I don’t know if the toString() is required or not.

1 Like

Not work in Blockly get java errors. I insist to understand, but in this case I will move to js scripting.
Thanks

Marco

I can’t help without seeing the errors.

yes I wiil send soon, I went to sleep yesterday evening

Hi,
here is the exception:

2023-03-10 16:33:42.637 [WARN ] [e.automation.internal.RuleEngineImpl] - Fail to execute action: 2
java.lang.ClassCastException: Cannot cast jdk.dynalink.beans.StaticClass to java.lang.Class
	at java.lang.Class.cast(Class.java:3605) ~[?:?]
	at jdk.nashorn.internal.scripts.Script$Recompilation$2059$\^eval\_$cu1$restOf/0x0000000101c99840.:program(<eval>:6) ~[?:?]
	at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:655) ~[jdk.scripting.nashorn:?]
	at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:513) ~[jdk.scripting.nashorn:?]
	at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:527) ~[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:?]
	at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264) ~[java.scripting:?]
	at org.openhab.core.automation.module.script.internal.handler.ScriptActionHandler.lambda$0(ScriptActionHandler.java:71) ~[?:?]
	at java.util.Optional.ifPresent(Optional.java:183) ~[?:?]
	at org.openhab.core.automation.module.script.internal.handler.ScriptActionHandler.execute(ScriptActionHandler.java:68) ~[?:?]
	at org.openhab.core.automation.internal.RuleEngineImpl.executeActions(RuleEngineImpl.java:1180) ~[?:?]
	at org.openhab.core.automation.internal.RuleEngineImpl.runNow(RuleEngineImpl.java:1031) ~[?:?]
	at org.openhab.core.automation.rest.internal.RuleResource.runNow(RuleResource.java:328) ~[?:?]
	at org.openhab.core.automation.rest.internal.RuleResource.runNow(RuleResource.java:342) ~[?:?]
	at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
	at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:?]
	at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
	at java.lang.reflect.Method.invoke(Method.java:566) ~[?:?]
	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:707) ~[bundleFile:3.1.0]
	at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:273) ~[bundleFile:3.4.5]
	at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:799) ~[bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:550) ~[bundleFile:9.4.46.v20220331]
	at org.ops4j.pax.web.service.jetty.internal.HttpServiceServletHandler.doHandle(HttpServiceServletHandler.java:74) ~[bundleFile:?]
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143) ~[bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:600) ~[bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127) ~[bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:235) ~[bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1624) ~[bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:233) ~[bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1440) ~[bundleFile:9.4.46.v20220331]
	at org.ops4j.pax.web.service.jetty.internal.HttpServiceContext.doHandle(HttpServiceContext.java:294) ~[bundleFile:?]
	at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:188) ~[bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:501) ~[bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1594) ~[bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:186) ~[bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1355) ~[bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) ~[bundleFile:9.4.46.v20220331]
	at org.ops4j.pax.web.service.jetty.internal.JettyServerHandlerCollection.handle(JettyServerHandlerCollection.java:90) ~[bundleFile:?]
	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127) ~[bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.server.Server.handle(Server.java:516) ~[bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.server.HttpChannel.lambda$handle$1(HttpChannel.java:487) ~[bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.server.HttpChannel.dispatch(HttpChannel.java:732) [bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:479) [bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:277) [bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:311) [bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:105) [bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.io.ChannelEndPoint$1.run(ChannelEndPoint.java:104) [bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.runTask(EatWhatYouKill.java:338) [bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:315) [bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.tryProduce(EatWhatYouKill.java:173) [bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:131) [bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:409) [bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:883) [bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.util.thread.QueuedThreadPool$Runner.run(QueuedThreadPool.java:1034) [bundleFile:9.4.46.v20220331]
	at java.lang.Thread.run(Thread.java:829) [?:?]

Here is what I did:
image

var state;

var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);


state = ir.getItem('WLEDSalottoLibreria_Segment_Master_Controls').getStateAs(OnOffType).toString()
logger.error(('State Output: ' + String(state)));

That’s a weird error but that’s an invalid script condition. As I’ve said repeatedly, the last line executed must evaluate to a boolean. The last line here returns nothing, void. So that error is probably coming from OH saying it cant convert a void to a boolean.

Here’s a trick. If it doesn’t make sense to put it into an if statement it cannot be the last line of a condition script.

if(logger.error(('State Output: ' + String(state)))) {

That makes no sense.

if(state.toString() == 'ON') {

That makes sense. So the last line needs to something like

state.toString() == 'ON'

If the condition doesn’t give OH a boolean, it can’t determine whether or not to allow the rule to run.

1 Like

Hi @rlkoshak
thankks for your patience, it is quite difficult to me understand the concept to return a boolean, sorry if I bore you.
The concept of return a boolena is not pretty clear. I mean, if I finish a script with:

1 == 1

it return without any error.
If I add

state = ir.getItem('WLEDSalottoLibreria_Segment_Master_Controls').getStateAs(OnOffType).toString()
1 == 1

I got the java exception. So I think issue in in getStateAs function in this case, because if I well understood 1 == 1 return a boolean.

Thanks

Yes. 1 == 1 is true. true and false are booleans. == is a boolean operator, as is !=, <, >, <=, and >. These operations only and always return a boolean.

I don’t have Nashorn available any more. The error tells you what line of code in your script caused the error I think.

at jdk.nashorn.internal.scripts.Script$Recompilation$2059$\^eval\_$cu1$restOf/0x0000000101c99840.:program(<eval>:6) ~[?:?]

So look at line 6 of the JavaScript (or what ever line it says it is now with your changes.

Note, if the script consists of only those two lines you’ve got a problem because state doesn’t exist is a variable.