I recently startet to write some automations in Javascript. I installed the JavaScript Scripting Addon and got some basic scripts to run
Now I am stuck at probaply some very simple issue.
I try to use a GroupItem to trigger the rule. That works. But i cant find out how i can find out what item of the group triggered the rule…
I tried this:
rules.JSRule({
name: "test",
description: "test ",
triggers: [triggers.GroupStateChangeTrigger('gTestGroup')],
execute: data => {
triggeritem = items.getItem("gTestGroup").members.sortBy["lastUpdate"].last
console.log("test","tescript has fired because of "+triggeritem);
}
});
What i get in the log is:
2023-01-15 19:03:29.753 [ERROR] [tomation.script.file.calculations.js] - Failed to execute rule test-e4630687-7a34-48be-ac11-09bb0e56b583: TypeError: Cannot read property "lastUpdate" from undefined: TypeError: Cannot read property "lastUpdate" from undefined
Obviously my aproach is wrong.
Is there a way to get the item that Triggered the rule?
Hi Thanks for the answer. I tried it but it did not work.
Logfile says
2023-01-15 20:00:49.324 [ERROR] [tomation.script.file.calculations.js] - Failed to execute rule test-7c2eb777-9228-4012-a1b9-fd37c0d27403: ReferenceError: "event" is not defined: ReferenceError: "event" is not defined
at execute (calculations.js:22)
There is no real difference. They are both common names for the single parameter that the execute function accepts, you just need to use the right variable name inside this function.
You can basically name the parameter anything, but it makes sense to name it data or event because it gets the event data passed.