Strange behaviour with JSScripting Rule

Aswering my own question, the line

console.log("ItemCommand", event.receivedCommand);

yields:

2022-01-18 23:07:47.996 [INFO ] [org.openhab.automation.script       ] - ItemCommand Test

But that is not the way I find it in other forum posts, See:

I run into similar issue, not sure what I’m doing wrong - everything worked in OH3.
In OH4 I updated the scripts to use a different “sendCommand” syntax as below:

// OH3
events.sendCommand( "item_HM_Sw4_Bewaesserung_" + this.irrigationSwitchID[this.key] + "_STATE", ON);

// OH4
items.getItem("item_HM_Sw4_Bewaesserung_" + this.irrigationSwitchID[this.key] + "_STATE").sendCommand("ON");

The items get changed as expected, however another rule I trigger on this item change, doesn’t work anymore. The itemCommand is always undefined.

The debug log I use

log.debug (">>>>> itemName" + this.event.itemName + " - itemCommand: " + this.event.itemCommand + " -  event: "+ this.event + " -  itemState: "+ this.event.itemState);

gets the following results:

2023-09-11 17:36:22.373 [DEBUG] [cript.Rules.Hauswasserwerk_PowerCtrl] - >>>>> itemNameitem_HM_Sw4_Bewaesserung_1_STATE - itemCommand: undefined -  event: Item 'item_HM_Sw4_Bewaesserung_1_STATE' changed from OFF to ON -  itemState: ON

I tried to convert the itemCommand.toString() but this is not valid for undefined of course.

Any idea what I did wrong?

Many thanks!

Your syntax is a strange mix of Nashorn JS and JS Scripting. Which are you using?

How is the rule triggered, on received command or changed?

Hello Rich,

the rule is triggered by ItemStateChangeTrigger.
I’m still migrating my old Java Scripts I built with OH3 to new JS Scripting on OH4.

That was a good hint - seems I messed something up. :frowning:
I changed it to ItemCommandTrigger and it is working now.