Rules / ECMA Script - NullPointerException on event handling (ThreadedEventHandler)

Hi all,

I have set up rules that should process trigger events from my hue dimmer switches.

Example from rule 1:

triggers:
  - id: "1"
    configuration:
      thingUID: hue:0820:001788708acd:8
      channelUID: hue:0820:001788708acd:8:dimmer_switch_event
    type: core.ChannelEventTrigger

When i am trying to test the trigger channel i always get the following error log:

20:22:57.263 [ERROR] [.internal.events.ThreadedEventHandler] - Error on event handling.
java.lang.NullPointerException: null
        at java.lang.String.contains(String.java:2036) ~[?:?]
        at org.openhab.core.automation.internal.module.handler.ChannelEventTriggerHandler.apply(ChannelEventTriggerHandler.java:91) ~[?:?]
        at org.openhab.core.internal.events.EventHandler.dispatchEvent(EventHandler.java:143) ~[bundleFile:?]
        at org.openhab.core.internal.events.EventHandler.handleEvent(EventHandler.java:111) ~[bundleFile:?]
        at org.openhab.core.internal.events.EventHandler.handleEvent(EventHandler.java:84) ~[bundleFile:?]
        at org.openhab.core.internal.events.ThreadedEventHandler.lambda$0(ThreadedEventHandler.java:67) [bundleFile:?]
        at java.lang.Thread.run(Thread.java:834) [?:?]
20:22:57.276 [INFO ] [org.openhab.rule.dimSwBederoom       ] - reason before if clause is: null
20:22:57.283 [INFO ] [org.openhab.rule.dimSwBederoom       ] - event in if clause is: hue:0820:001788708acd:8:dimmer_switch_event triggered 2002.0
20:22:57.287 [INFO ] [org.openhab.rule.dimSwBederoom       ] - event.event in if clause is: 2002.0
20:22:57.293 [INFO ] [org.openhab.rule.dimSwBederoom       ] - reason is: 2002.0
20:22:57.298 [INFO ] [org.openhab.rule.dimSwBederoom       ] - DIM UP Button SHORT event
20:22:57.311 [INFO ] [openhab.event.ItemCommandEvent       ] - Item 'g_Schlafzimmer_Leuchten_Szene' received command 2

Here is the script code i am using:

var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID)
var bedroom_Scene = itemRegistry.getItem("g_Schlafzimmer_Leuchten_Szene");
// Proper init to avoid errors when ran without event available
var reason = null

logger.info("reason before if clause is: " + reason)

if(typeof event !== 'undefined' && typeof event.event !== 'undefined'){
  logger.info("event in if clause is: " + event)
  logger.info("event.event in if clause is: " + event.event)
  reason = event.event
}

logger.info("reason is: " + reason)

switch(reason){
  case "2002.0":
    logger.info("DIM UP Button SHORT event")
    events.sendCommand(bedroom_Scene, 2)
    break
  case "2003.0":
    logger.info("DIM UP Button LONG event")
    events.sendCommand(bedroom_Scene, 2)
    break
  case "3002.0":
    logger.info("DIM DOWN Button SHORT event")
    events.sendCommand(bedroom_Scene, 1)
    break
  case "3003.0":
    logger.info("DIM DOWN Button LONG event")
    events.sendCommand(bedroom_Scene, 1)
    break
  default:
    logger.info("Unknown Switch Event triggered. No action fired!")
}

As you can see the complete script is working anyway after the error message and commands are executed as wanted.
Did somebody face this NPE yet?

  • Platform information:
    • Hardware: CPUArchitecture/RAM/storage Virtual Machine with 4 GB Ram
    • OS: what OS is used and which version Ubuntu Server 20.04
    • Java Runtime Environment: which java platform is used and what version Zulu
    • openHAB version: 3.0.0

I don’t get the NPE, but I get an error, if I use this:

triggers:
  - id: "1"
    configuration:
      cronExpression: 0 0 7,12,18 * * ? *
    type: timer.GenericCronTrigger
  - id: "2"
    configuration:
      groupName: MQTTMOWASMeldungen
    type: core.GroupStateChangeTrigger
conditions: []
actions:
  - inputs: {}
    id: "3"
    configuration:
      type: application/javascript
      script: >-
        var logger =
        Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' +
        ctx.ruleUID);

        var NotificationAction = org.openhab.io.openhabcloud.NotificationAction;

        if (typeof(event) === undefined) {
          // es war ein Cron-Event, also mal alle losschicken
          logger.info("es ist Cron")
        } else {
          if(event.itemName !== undefined){
            var MowasNr = event.itemName.split("0")[1];
            logger.info("Es ist die Nummer: " + MowasNr)
          }
        }

it spits out this, if the cron sets in:

2021-01-16 18:45:08.977 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'MowasInformation' failed: ReferenceError: "event" is not defined in <eval> at line number 8

I’d like to check, if it was the CRON or it was the changing item, which triggered the rule. So I have to do two rules linking to the same script, which makes it a bit more clustered if I could use one rule…

I also tried if(event === undefined){ with the same outcome. Don’t know, if that helps you, but at least I don’t get an NPE out of it… :wink: