JSScript compare event.itemState with Number

OHM3

How do I compare an event.itemState that is a Number in a JavaScript GUI based script? I can convert to a string, but that seems suboptimal:

if (event.itemState.toString == "2") {

The itemState should be an object, with getItemState function you can get the state object. State will have a compare function…

Which JS are you using? If it’s GraalVM JS Scripting ECMAScript 11, it depends whether the Item’s state has units or not.

// without units
if(event.itemState.intValue() == 2)

// with units
var state = Quantity(event.itemState);
var constant = Quantity("2 W"); // where you replace "W" with what ever units are being used
if(state.equals(constant))
2 Likes