JSScripting & event.itemCommand

I have started migrating all my rules from ECMA (Nashorn) to new JSScripting. Can someone help me why below test rule doesn’t work anymore?

“console.error(event.itemCommand)” returns:

2021-12-30 18:23:12.011 [ERROR] [org.openhab.automation.script       ] - ON

But the If-Statement does not work. Never get a “Test 1” or “Test 2” for command OFF. Tried both:

== ‘ON’
== “ON”

switch (event.itemName){
  case "Fernseher" :
        console.error(event.itemCommand)
        if (event.itemCommand == 'ON') {
            console.error("Test 1");     
        } else if (event.itemCommand == 'OFF') {
            console.error("Test 2");
        }

OK, seems that event.itemCommand is a different type.

This works, but not sure if it is the right approach:

var myCommand = String(event.itemCommand);
if (myCommand  == "ON") {
// Do something
}

Doesn’t work:

var myCommand = event.itemCommand;
if (myCommand  == "ON") {
// Do something
}
var myCommand = event.itemCommand;
if (myCommand  == 'ON') {
// Do something
}

Any other ideas or comments?

"ON" is a string. So you will want to compare it to a String.

event.itemCommand is an openHAB Java Command Object, not a String.

So either the Command needs to be come a String, or the String needs to become a Command.

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