JSScripting: `Reference error "event" is not defined`

openHAB 3.2, fresh setup, first steps with JavaScript rules (ECMAScript-2021) - Style.

Rule - designed via UI

configuration: {}
triggers:
  - id: "2"
    configuration:
      itemName: SW_DUMMY
    type: core.ItemStateChangeTrigger
conditions: []
actions:
  - inputs: {}
    id: "1"
    configuration:
      type: application/javascript;version=ECMAScript-2021
      script: |+
        console.log(event.itemState, "->", event.oldItemState, "/")
    type: script.ScriptAction

When executed (a) via Rule Designer or (b) by toggling the Switch SW_DUMMY, openhab complains
Reference error "event" is not defined.

According to various topics, there should be a event providing access to oldItemState, itemState, itemName, … .

event is definitely not going to exist when the rule is triggered manually (or based on a time or system based trigger). So I would definitely expect the error when triggering the Item that way.

But it should be there when SW_DUMMY changed state. First, just log out event and see if it’s undefined or not. That will confirm something weird is going on as well as confirm that the rule is running.

It will never work if you exectute the rule via Rule Designer: event is only defined if an item (or a thing) triggers the rule.
This works for me:

if (typeof event !== 'undefined') {
  trigitm = items.getItem(event.itemName);
else {
  console.debug("Rule was run manually - no event found")
}
1 Like