Javascript not working

I am trying to figure out why the outcome of the “if-statement” below is the same no matter if the virtual switch item is set to “ON” or “OFF”. What am I doing wrong?

I am using OpenHAB4. Obviously, the code below is just an example.

Any help appreciated, thanks.

var value10 = items.getItem(‘vOfficeHeating’).rawState;
console.info(value10)

if (value10 !== “ON”) {
console.info(“This code runs because value10 is not set to ‘ON’”);
} else {
console.info(“This code runs because value10 is set to ‘ON’”);
}

If “ON”:

2024-01-28 22:54:40.208 [INFO ] [nhab.automation.script.ui.b7d527c792] - ON
2024-01-28 22:54:40.209 [INFO ] [nhab.automation.script.ui.b7d527c792] - This code runs because value10 is not set to ‘ON’

If “OFF”:

2024-01-28 22:56:16.608 [INFO ] [nhab.automation.script.ui.b7d527c792] - OFF
2024-01-28 22:56:16.609 [INFO ] [nhab.automation.script.ui.b7d527c792] - This code runs because value10 is not set to ‘ON’

Try this instead:

var value10 = items.getItem('vOfficeHeating').state;

Or simplified:

var value10 = items.vOfficeHeating.state;

rawState returns the actual java type of the item state (in this case most likely OnOff Type). When you send that to the console it will print as if it were a string, but it is definitely not a string ( you can try console.log(typeof(value10)) to see this for yourself. Because it is not a string, value10 !== “ON” will always evaluate to true, as !== compares both value AND object type (OnOff type always does not equal string type).

Unless there is some very specific reason, you should never need to resort to rawState in your rules. Just use state instead as @laursen demonstrated. That always returns the state in a string format so you can easily control your types and in this case compare the state to 'ON'successfully.

A corollary to this is that you should probably avoid === and !== in most cases as well, again, unless you really mean it and know exactly what types are possible in your expressions. Just use == and != instead. The latter two do have some limited capacity to do automatic type conversion (although I don’t believe it would work in this case).

Both @laursen and @JustinG have the answers to your specific questions. But I want to expand on this whole situation.

Here is a table of all the different ways you can get the state of an Item in JS Scripting.

Attribute Type Usage Notes
items.MyItem.state String if(items.MyItem.state == "ON")
items.MyItem.isUninitialized Boolean if(items.MyItem.isUninitialized) true if the state is either NULL or UNDEF
items.MyItem.numericState Number if(items.MyItem.numericState > 20) null if the state does not represent a numeric value, a plain number otherwise (e.g. strips unit)
items.MyItem.quantityState Quantity if(Items.MyItem.quantityState.greaterThan(Quantity('20 °C')) null if the state is not a number or does not have units
items.MyItem.rawState org.openhab.core.types.State Just don’t This is only needed if you are doing something with the raw JSR223 API and need the Java State Object for some reason (e.g. using an Action)

Worked perfekt, thanks a lot. I don’t know why I came up with the rawState. I guess I found it in the REST API example I was working with.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.