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:
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?
stefan13
(Stefan)
September 11, 2023, 3:38pm
22
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!
rlkoshak
(Rich Koshak)
September 11, 2023, 3:43pm
23
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?
stefan13
(Stefan)
September 11, 2023, 7:19pm
24
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.
stefan13
(Stefan)
September 11, 2023, 7:22pm
25
That was a good hint - seems I messed something up.
I changed it to ItemCommandTrigger
and it is working now.