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

is there a way to do it in Blockly?


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:

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.


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.


I can’t help without seeing the errors.

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

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( ~[?:?]
	at jdk.nashorn.internal.scripts.Script$Recompilation$2059$\^eval\_$cu1$restOf/0x0000000101c99840.:program(<eval>:6) ~[?:?]
	at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke( ~[jdk.scripting.nashorn:?]
	at jdk.nashorn.internal.runtime.ScriptFunction.invoke( ~[jdk.scripting.nashorn:?]
	at jdk.nashorn.internal.runtime.ScriptRuntime.apply( ~[jdk.scripting.nashorn:?]
	at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl( ~[jdk.scripting.nashorn:?]
	at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl( ~[jdk.scripting.nashorn:?]
	at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl( ~[jdk.scripting.nashorn:?]
	at jdk.nashorn.api.scripting.NashornScriptEngine.eval( ~[jdk.scripting.nashorn:?]
	at javax.script.AbstractScriptEngine.eval( ~[java.scripting:?]
	at org.openhab.core.automation.module.script.internal.handler.ScriptActionHandler.lambda$0( ~[?:?]
	at java.util.Optional.ifPresent( ~[?:?]
	at org.openhab.core.automation.module.script.internal.handler.ScriptActionHandler.execute( ~[?:?]
	at org.openhab.core.automation.internal.RuleEngineImpl.executeActions( ~[?:?]
	at org.openhab.core.automation.internal.RuleEngineImpl.runNow( ~[?:?]
	at ~[?:?]
	at ~[?:?]
	at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
	at jdk.internal.reflect.NativeMethodAccessorImpl.invoke( ~[?:?]
	at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke( ~[?:?]
	at java.lang.reflect.Method.invoke( ~[?:?]
	at org.apache.cxf.service.invoker.AbstractInvoker.performInvocation( ~[bundleFile:3.4.5]
	at org.apache.cxf.service.invoker.AbstractInvoker.invoke( ~[bundleFile:3.4.5]
	at org.apache.cxf.jaxrs.JAXRSInvoker.invoke( ~[bundleFile:3.4.5]
	at org.apache.cxf.jaxrs.JAXRSInvoker.invoke( ~[bundleFile:3.4.5]
	at org.apache.cxf.interceptor.ServiceInvokerInterceptor$ ~[bundleFile:3.4.5]
	at org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage( ~[bundleFile:3.4.5]
	at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept( ~[bundleFile:3.4.5]
	at org.apache.cxf.transport.ChainInitiationObserver.onMessage( ~[bundleFile:3.4.5]
	at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke( ~[bundleFile:3.4.5]
	at org.apache.cxf.transport.servlet.ServletController.invokeDestination( ~[bundleFile:3.4.5]
	at org.apache.cxf.transport.servlet.ServletController.invoke( ~[bundleFile:3.4.5]
	at org.apache.cxf.transport.servlet.ServletController.invoke( ~[bundleFile:3.4.5]
	at org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke( ~[bundleFile:3.4.5]
	at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest( ~[bundleFile:3.4.5]
	at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doPost( ~[bundleFile:3.4.5]
	at javax.servlet.http.HttpServlet.service( ~[bundleFile:3.1.0]
	at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service( ~[bundleFile:3.4.5]
	at org.eclipse.jetty.servlet.ServletHolder.handle( ~[bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.servlet.ServletHandler.doHandle( ~[bundleFile:9.4.46.v20220331]
	at org.ops4j.pax.web.service.jetty.internal.HttpServiceServletHandler.doHandle( ~[bundleFile:?]
	at org.eclipse.jetty.server.handler.ScopedHandler.handle( ~[bundleFile:9.4.46.v20220331]
	at ~[bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.server.handler.HandlerWrapper.handle( ~[bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle( ~[bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.server.session.SessionHandler.doHandle( ~[bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle( ~[bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.server.handler.ContextHandler.doHandle( ~[bundleFile:9.4.46.v20220331]
	at org.ops4j.pax.web.service.jetty.internal.HttpServiceContext.doHandle( ~[bundleFile:?]
	at org.eclipse.jetty.server.handler.ScopedHandler.nextScope( ~[bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.servlet.ServletHandler.doScope( ~[bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.server.session.SessionHandler.doScope( ~[bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.server.handler.ScopedHandler.nextScope( ~[bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.server.handler.ContextHandler.doScope( ~[bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.server.handler.ScopedHandler.handle( ~[bundleFile:9.4.46.v20220331]
	at org.ops4j.pax.web.service.jetty.internal.JettyServerHandlerCollection.handle( ~[bundleFile:?]
	at org.eclipse.jetty.server.handler.HandlerWrapper.handle( ~[bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.server.Server.handle( ~[bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.server.HttpChannel.lambda$handle$1( ~[bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.server.HttpChannel.dispatch( [bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.server.HttpChannel.handle( [bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.server.HttpConnection.onFillable( [bundleFile:9.4.46.v20220331]
	at$ReadCallback.succeeded( [bundleFile:9.4.46.v20220331]
	at [bundleFile:9.4.46.v20220331]
	at$ [bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.runTask( [bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce( [bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.tryProduce( [bundleFile:9.4.46.v20220331]
	at [bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.util.thread.ReservedThreadExecutor$ [bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob( [bundleFile:9.4.46.v20220331]
	at org.eclipse.jetty.util.thread.QueuedThreadPool$ [bundleFile:9.4.46.v20220331]
	at [?:?]

Here is what I did:

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.


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.