Thanks for your help. utils.dumObject() helped me find the solution. It seems like file-based rules handle the event object differently then rules defined using the Main UI editor. Be aware I only tested rules written in application/javascript;version=ECMAScript-2021.
In file-based rules it is a simple JavaScript Object, in UI rules it is an instance of a Java class. Using the built-in Javascript method Object.keys(event) let me iterate over all keys of these objects to log them and their values. I have done that in both file-based and UI rules.
Main UI Rule
configuration: {}
triggers:
- id: "1"
configuration:
itemName: testSwitch
type: core.ItemCommandTrigger
conditions: []
actions:
- inputs: {}
id: "2"
configuration:
type: application/javascript;version=ECMAScript-2021
script: |
let logger = log("rule_from_ui");
logger.info("logging event from ui rule");
const event_keys = Object.keys(event);
for (let i = 0; i < event_keys.length; i++) {
const key = event_keys[i];
logger.info("{}: {}", key, event[key]);
};
type: script.ScriptAction
The above rule reveals the following structure of the event object:
{
"getItemCommand": "org.openhab.core.items.events.ItemCommandEvent.getItemCommand",
"toString": "org.openhab.core.items.events.ItemCommandEvent.toString",
"getType": "org.openhab.core.items.events.ItemCommandEvent.getType",
"getItemName": "org.openhab.core.items.events.ItemCommandEvent.getItemName",
"getTopic": "org.openhab.core.items.events.ItemCommandEvent.getTopic",
"getPayload": "org.openhab.core.items.events.ItemCommandEvent.getPayload",
"getSource": "org.openhab.core.items.events.ItemCommandEvent.getSource",
"equals": "org.openhab.core.items.events.ItemCommandEvent.equals",
"hashCode": "org.openhab.core.items.events.ItemCommandEvent.hashCode",
}
To get the received command the following statement is needed:
const cmd = event.getItemCommand();
File Based Rule
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: event => {
logger.info("logging event from file based rule");
const event_keys = Object.keys(event);
for (let i = 0; i < event_keys.length; i++) {
const key = event_keys[i];
logger.info("{}: {}", key, event[key]);
};
}
});
The above rule reveals the following, slightly different, structure of the event object:
{
"eventType": "command",
"triggerType": "ItemCommandTrigger",
"receivedCommand": "ON",
"oldState": "null",
"newState": "null",
"itemName": "testSwitch",
"module": "e2960f39-b513-4144-aadd-2a0eea8e74aa"
}
To get the received command the following statement is needed:
const cmd = event.receivedCommand;
I find this to be a bit confusing. I would expect the Javascript to produce the same results, independent of where it’s written. I also think it’d be a good idea to expand the JSRule documentation to include a more detailed explanation of what the triggerConfig expected by the execute key actually is. It does say callback, but I think it’d be advantageous to explain that the callback has to expect exactly one argument which will contain information about the event triggering the rule. This could maybe help people avoiding the initial problem I had. Unfortunately I wasn’t able to find such a description. If it exists, maybe just add a link as a reply for future reference.