Function Node keeps throwing error (Node-Red)

Hi All,

I’ve build a “Function Node” that connects to an “All Events Node”. If I deploy Node Red I get an error in de debug log: “TypeError: Cannot read properties of undefined (reading ‘device_id’)”, which refers to the “Funciton Node”.

I can’t seem to figure out why this error is thrown

Code: Function Node

var p1 = msg.payload.event_type;

var p2 = msg.payload.event.device_id;

if (p1 == "zha_event" && p2 == "089da0ea7e55279c0a0fc2cd6a8b4304") {

    msg.payload = true;

    return [msg, null];

}

Payload:
payload

Error:
2023-04-28 16_26_35-Node-RED – Home Assistant

Maybe there are some messages coming through that don’t have an event property? You could use optional chaining (?.) to get rid of the error:

var p2 = msg.payload.event?.device_id || "empty";