openhab / openhab-js

openHAB JavaScript Library for JavaScript Scripting Automation
https://www.openhab.org/addons/automation/jsscripting/
Eclipse Public License 2.0
38 stars 31 forks source link

The Event object for file based rules does not create many of the properties as per the documentation #305

Closed mjcumming closed 6 months ago

mjcumming commented 8 months ago

Reviewing this part of rules.js

  case 'org.openhab.core.items.events.ItemCommandEvent':
    data.itemName = event.getItemName();
    data.eventType = 'command';
    data.triggerType = 'ItemCommandTrigger';
    break;
  case 'org.openhab.core.items.events.GroupItemStateChangedEvent':
  case 'org.openhab.core.items.events.ItemStateChangedEvent':
    data.itemName = event.getItemName();
    data.eventType = 'change';
    data.triggerType = 'ItemStateChangeTrigger';
    break;

The expected properties, as per the documentation, are missing such as newState, receivedCommand, etc.

https://www.openhab.org/addons/automation/jsscripting/#event-object

It seems that _addFromHashMap is not working correctly as none of these properties are being set.

florian-h05 commented 7 months ago

I cannot reproduce this on the latest snapshot #3723 and with the openhab-js version that comes with it.

I have tested this with a file-based rule:

rules.when().item('test_switch').changed().then((e) => {
    console.log(e);
}).build();

The log output is as expected:

{
  "oldState": "NULL",
  "newState": "ON",
  "itemName": "KNX_Aktor_Licht",
  "eventType": "change",
  "triggerType": "ItemStateChangeTrigger",
  "eventClass": "org.openhab.core.items.events.ItemStateChangedEvent",
  "payload": {
    "type": "OnOff",
    "value": "ON",
    "oldType": "UnDef",
    "oldValue": "NULL"
  },
  "module": "90f9a354-1746-4878-9593-261196c5d34e"
}
florian-h05 commented 7 months ago

Can you please provide information about your system (the issue templates exist for a reason, please follow them) and re-test?

mjcumming commented 7 months ago

Hi, sorry for the delay - I hope to get an example code block. The error is happening with the GenericEventTrigger - does something need to change in the code the builds the Event object?

florian-h05 commented 6 months ago

Not sure what you expect when using GenericEventTrigger, but for this special trigger the available properties heavily depend on which events it triggers. You cannot expect to find itemName on the event object of GenericEventTrigger if GenericEventTrigger only triggers on non-Item related events.

I will update the docs accordingly to make that clear.