Javascript - get the item of a group that triggered the rule

Hello there,

I recently startet to write some automations in Javascript. I installed the JavaScript Scripting Addon and got some basic scripts to run :slight_smile:

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:

    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?


you can get the Item name with

const triggeritem = event.itemName;

the description you find here: Event object

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)

Also Visual Studio says

am i missing anything to get it to work?

I see there is a fault in your rule the line

    execute: data => {

should be

execute: (event) => {
1 Like

Thanks. It works :slight_smile:

Can you explain what the difference between data and event is in that place?

I build all my rules with “data” in that place, just copied always fro the first working rule… :upside_down_face:

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.

(abcd) => {
1 Like