ECMA Script 2021 rules - which trigger triggered a rule?

Hello,
I’m aware that event.itemName gets me access to an item which has triggered a rule through update or command.

Is there a way, to achieve something similar for cron-triggered rules?

In the following rule snippet:

configuration: {}
triggers:
  - id: "1"
    configuration:
      cronExpression: 0/10 * * * * ? *
    type: timer.GenericCronTrigger
  - id: "2"
    configuration:
      cronExpression: 5/10 * * * * ? *
    type: timer.GenericCronTrigger
conditions: []
actions:
  - inputs: {}
    id: "3"
    configuration:
      type: application/javascript;version=ECMAScript-2021
      script: |-
        console.log("Id of trigger",triggers.id)
    type: script.ScriptAction

I’m wondering whether it’s possible to determine, which of the two cron expresions (or more generally, which triggers.id) has triggered the rule. (Of course triggers.id doesn’t work, otherwise, I wouldn’t have asked :wink: )

Since you’re using ECMA-2021 the event object is handled by the openhab-js library:

To my knowledge, but that might have changed since my last update, cron does not give you an event.itemName type of trigger.
But you can use the following in your code:

this.event === undefined in combination with this.event = undefined

The code will then trigger if the event is undefined (which for cron is the case) and reset after the code did run to undefined.

I use this methodology based on this previous discussion.

Above will only help though with one cron trigger, not be able to decide which of the two triggered.
You could do that though maybe with a if clause that looks into the timestamp?

If there is an easier way, I am all ears though :slight_smile:

The short answer is no. If you have two time based triggers, there is no way to distinguish which one triggered the rule. You also cannot tell the difference between a time based trigger and a manual trigger.

You can tell the difference between an Item/Channel/Thing trigger by testing for what variables are defined on event like @chrismast mentions.

That’s actually not the case in UI rules and it’s a source of consternation. I’m not sure what to do about that yet. But in the UI, you get the raw event, same as you’d get in the JSR223 type rules (Nashorn, Jython, etc.). I don’t like it when it works that differently.