Rollershutter Rule from DSL to JSS

Has somebody already found out how to use “receivedCommand” inside a JSS rule in the UI? From old ECMA tutorials it should be “command” but the UI does not know how to proceed. Additionally would be nice how achieve the same behavior: “instanceof Number” in JSS.

Thanks for your help!

Thats the old DSL rule:

rule "Rollershutter"
when
    Item Sonoff15_rollershutter received command   
then
       
        if(!(receivedCommand instanceof Number)){
        switch receivedCommand 
        {
            case DOWN : Sonoff15_Number.sendCommand(0)
            case UP : Sonoff15_Number.sendCommand(100)
            default : {}
        }
        }else{
        val Number rollershutter = Sonoff15_rollershutter as DecimalType).intValue
        Sonoff15_Number.sendCommand(rollershutter)
        }
end

EDIT:
New JS-Rule:

switch (String(event.itemCommand)){
  case "DOWN" : items.getItem("Sonoff15_Number").sendCommand("0")
    break;
  case "UP" : items.getItem("Sonoff15_Number").sendCommand("100")
    break;
  case "STOP" : items.getItem("Sonoff15_ShutterStop").sendCommand("STOP")
    break;
  default:
    items.getItem("Sonoff15_Number").sendCommand(items.getItem("Sonoff15_rollershutter").state)
    break;
}

Good question. Trying to find out myself, too, at the moment.

So far I only understand that you have the triggeringItem name and state available within the following:

event
event.payload
event.topic

However, I thought that event is JSR223, and not new JSscripting.
Let’s see, when the expert are coming up what they say.

For old JS see here:

console.log("receivedCommand: ", event.itemCommand);
console.log("triggeringItemName: ", event.itemName);

works also with “new” JS

Hello Oliver,

I was dealing with a similar issue wanting to access the receivedCommand for an ItemCommandTrigger and stumbled upon this thread. Unfortunately my very simple rule fails to execute. Here’s the code:

var {event, items, log, rules, triggers} = require("openhab")

let logger = log("tests");


rules.JSRule({
    name: "[TESTING] Test Switch Received Command",
    description: "Log the item name, state and received command",
    triggers: [
        triggers.ItemCommandTrigger("testSwitch")
    ],
    execute: data => {
        let itemName = event.itemName;
    }
});

I’m running openhab 3.2.0.M2 in a Docker container on a Raspberry Pi 3 running raspbian lite. I have configured the JSScriping Extension through Main UI to not use built-in variables. Any idea why this doesn’t work?

you are declaring a variable (itemName) but you are not doing anything with it.
If you write it into your log, what happens?

It produced an error with only that statement. I have elaborated further in this post. I was accessing an undefined variable, that problem is fixed now. Unfortunately, that the property of that variable I’m trying to access is somehow undefined and I have no idea why.

in the other post Rich advised to change var into const.
I would try without imports anyway first.

The problem was much deeper than expected but I found a solution which I have described in detail as a reply to my other post. I’ll just leave the link here in case anyone stumbles upon this post while dealing with a similar issue.