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

[rules] ItemStateChangeTrigger event data incorrect if the Item is a Group. #145

Closed florian-h05 closed 2 years ago

florian-h05 commented 2 years ago

ItemStateChangeTrigger event data incorrect if the Item is a Group.
It appears that rules.js/getTriggeredData() does not handle the case when the triggered Item is a Group.

https://www.openhab.org/docs/configuration/items.html#groups

When using a Group Item the event triggerType and itemName are missing and the event class is org.openhab.core.items.events.GroupItemStateChangedEvent instead of org.openhab.core.items.events.ItemStateChangedEvent.

I have included test code to reproduce below.

Group:Switch:OR(ON,OFF) g2 
Switch b1 "test b1" (g2)
Switch b2 "test b2" (g2)
Switch b3 "test b3" (g2)
rules.JSRule({
    name: "Event Trigger Test",
    description: "When state of group changes log event details"
    triggers: [
        triggers.ItemStateChangeTrigger('g2', 'OFF', 'ON'),
        triggers.ItemStateChangeTrigger('b1', 'OFF', 'ON')
    ],
    execute: event => {
        console.log(event);
    }
});
2022-07-02 09:16:18.563 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'b1' received command ON
2022-07-02 09:16:18.572 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'b1' changed from OFF to ON
2022-07-02 09:16:18.594 [INFO ] [hab.event.GroupItemStateChangedEvent] - Item 'g2' changed from OFF to ON through b1
2022-07-02 09:16:18.623 [INFO ] [enhab.automation.script.file.test.js] - {
  "oldState": "OFF",
  "newState": "ON",
  "itemName": "b1",
  "eventType": "change",
  "triggerType": "ItemStateChangeTrigger",
  "eventClass": "org.openhab.core.items.events.ItemStateChangedEvent",
  "payload": {
    "type": "OnOff",
    "value": "ON",
    "oldType": "OnOff",
    "oldValue": "OFF"
  },
  "module": "18dd392e-28f5-4286-9f52-552dd8c8cc0e"
}

2022-07-02 09:16:18.634 [INFO ] [enhab.automation.script.file.test.js] - {
  "oldState": "OFF",
  "newState": "ON",
  "eventClass": "org.openhab.core.items.events.GroupItemStateChangedEvent",
  "payload": {
    "type": "OnOff",
    "value": "ON",
    "oldType": "OnOff",
    "oldValue": "OFF"
  },
  "module": "aa2b5da4-2a51-431a-87d2-b622a90753b8"
}

Originally posted by @rickoman in https://github.com/openhab/openhab-js/issues/136#issuecomment-1172928136

florian-h05 commented 2 years ago

@rickoman I am having a look at this right now, I now what the problem is.

When using a Group Item the event triggerType and itemName are missing

I will fix that.

event class is org.openhab.core.items.events.GroupItemStateChangedEvent instead of org.openhab.core.items.events.ItemStateChangedEvent.

I am not sure if you consider this as an issue, but this is no issue or bug.

florian-h05 commented 2 years ago

@rickoman I have a PR opened with a fix, it would be nice if you could test it (see #146).

You can install the PR with npm install git+https://github.com/florian-h05/openhab-js.git#rules-fix-get-triggered-data.