OH 5.1.1 Rules: core.GenericEventTrigger return event.topic undefined

Hello,

Since OH5.1.1 the following inline script rule not longer works. (was running fine in OH5.0.2)

configuration: {}
triggers:
  - id: "1"
    label: A Thing Changes Status
    description: Triggers when any Thing changes status
    configuration:
      topic: openhab/things/**
      types: ThingStatusInfoChangedEvent
      source: ""
      payload: ""
    type: core.GenericEventTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/javascript
      script: >
        console.info(">DBUG INFO"); 

        console.info(">event.topic " + event.topic); 

        console.info(">event.source " + event.source); 
        console.info(">event.eventName " + event.eventName); 

        console.info(">event.payload" + event.payload);

        var thingId = event.topic.split('/')[2]; 

        var thingState = JSON.parse(event.payload)[0].status; 

        var thingStateDetail = JSON.parse(event.payload)[0].statusDetail;

        var thingStateDescr = JSON.parse(event.payload)[0].description;

        console.info(">DBUG INFO " + thingId + " " + thingState + " " +
        thingStateDetail + " " + thingStateDescr); 
    type: script.ScriptAction

The following is shown in the console:

11:57:10.407 [INFO ] [hab.event.ThingStatusInfoChangedEvent] - Thing ‘evohome:heatingzone:home:5371702’ changed from INITIALIZING to OFFLINE (COMMUNICATION_ERROR): TempZoneActuatorLowBattery
11:57:10.407 [INFO ] [ation.jsscripting.rule.Temp_thingrule] - >DBUG INFO
11:57:10.408 [INFO ] [ation.jsscripting.rule.Temp_thingrule] - >event.topic undefined
11:57:10.408 [INFO ] [ation.jsscripting.rule.Temp_thingrule] - >event.source undefined
11:57:10.408 [INFO ] [ation.jsscripting.rule.Temp_thingrule] - >event.eventName ThingStatusInfoChangedEvent
11:57:10.409 [INFO ] [ation.jsscripting.rule.Temp_thingrule] - >event.payload[object Object],[object Object]

So the event.topic and event.payload are not being returned.

5.1 release notes mention:

JavaScript Automation
  • The event object in UI-based environments has been aligned with the file-based event object. Properties are now pure JavaScript types and property names have changed. Blockly users need to resave their scripts.

Where can I find more details on the above changes (JavaScript types and property names)?
Any help would be appreciated. :slight_smile:

Here is a code snippet from a rule I have to trigger on all Thing Status Changes. It’s redundant but I keep it around as an example I can refer to for how to use GenericTrigger. This is from a rule template, so I made sure it works with both the old event and the new event.

// Get the payload and the topic, the event Object might or might not be
// wrapped in JS
var parsed = (event.raw.get === undefined)? JSON.parse(event.payload) : event.payload;
var topic = (event.topic !== undefined) ? event.topic : event.raw.get('event').getTopic().toString();

var data = {};
data['thingID'] = topic.split('/')[2];
data['thing'] = things.getThing(data['thingID']);
data['oldStatus'] = parsed[1].status;
data['oldDetail'] = parsed[1].statusDetail;
data['newStatus'] = parsed[0].status;
data['newDetail'] = parsed[0].statusDetail;

Notice the payload is already parsed into an Object.

Also note you can restore the old raw Java event Object from `Settings → JavaScript Scripting → Show Advanced → toggle off Convert Event from Java to JavaScript type in Script Actions & Script Conditions scripts.

And as you can see here, you can get access to the raw Java event even if it’s wrapped using event.raw.

And if you find getting access to the event topic and data is important enough, you can file a feature request to add them as properties on the wrapped JS event Object. I’m not sure though whether that needs to be on openhab-js or on the jsscripting add-on. I think the former.

1 Like

Thank you very much.
I managed to get it working!