Node Red crash bug

I think I found a bug in the NodeRed code.

26 Oct 11:03:16 - [info] [server:Home Assistant] Connected to http://supervisor/core
26 Oct 11:03:47 - [error] [ha-get-entities:Get current scene] Attempted to invoke a non-function
26 Oct 11:03:47 - [red] Uncaught Exception:
26 Oct 11:03:47 - [error] Error: 
    at applyInner (/opt/node_modules/jsonata/jsonata.js:4959:29)
    at applyInner.next (<anonymous>)
    at apply (/opt/node_modules/jsonata/jsonata.js:4896:26)
    at apply.next (<anonymous>)
    at evaluateFunction (/opt/node_modules/jsonata/jsonata.js:4871:30)
    at evaluateFunction.next (<anonymous>)
    at evaluate (/opt/node_modules/jsonata/jsonata.js:3522:34)
    at evaluate.next (<anonymous>)
    at thenHandler (/opt/node_modules/jsonata/jsonata.js:5562:37)
[11:03:47] INFO: Service Node-RED exited with code 1 (by signal 0)
s6-rc: info: service legacy-services: stopping
s6-rc: info: service legacy-services successfully stopped

My HA node red add on has been crashing for a few days, I finally tracked this error - invalid input in a get entities node.

What I was trying to do
I set up an environment variable - roominfo - on the flow of a json object representing several room specific values:

{
  "area": "steves_office",
  "dimmers": [
    {
      "name": "dimmer1",
      "type": "Hue v1",
      "device_ieee": "00:17:88:01:04:af:95:a0"
    },
    {
      "name": "dimmer2",
      "type": "Hue v2",
      "device_ieee": "00:17:88:01:0b:59:ef:1a"
    }
  ],
  "illuminance_sensors": [
    {
      "name": "sensor.steves_office_sensor_illuminance"
    },
    {
      "name": "sensor.steves_office_sensor2_illuminance"
    }
  ],
  "light": "light.steves_office",
  "occupancy_sensors": [
    {
      "name": "binary_sensor.steves_office_sensor_occupancy"
    },
    {
      "name": "binary_sensor.steves_office_sensor2_occupancy"
    }
  ],
  "occupancy_timer": "timer.steves_office_occupancy",
  "scene_helper": "input_text.steves_office_last_scene",
  "scene_motion_sensor_enabled": "input_boolean.steves_office_motion_sensor_enabled"
}

I was trying to access the scene_helper entity id value using the following jsonata string $(roominfo.scene_helper) - I previously had a separate env variable called scene_helper and I could access the value using this jsonata $(scene_helper) - this was trying to reduce the number of env variables.

I corrected my flow to use a change node to set msg.payload to { "entityId": $$.roominfo.light } and leave the get entities node Property field blank.

I looks like the field is not checking for a valid jsonata string, and then crashes. No sure if this is bug in the HA nodes or node-red or the jsonata lib.

p.s. why is it entityId in some places and entity_id in others?

Your JSONata expression will certainly generate an error.

$ is the current context, however $name is a binding to a variable and with the () it becomes a function call to a function with the given name, which for $() generates “Attempted to invoke a non-function” for obvious reasons.

If you want to get an an environment variable in JSONata you should use $env(‘name’). I have just tested this in a flow using an inject node (and a get entities node). The environment name in the function call must be a ‘quoted’ string.

I have no idea how you managed to get $(scene_helper) to do anything except error; perhaps your code has always been incorrect but only now with the recent changes to how JSONata is called in the websocket nodes does it fail.

$env() is a Node-RED specific function added to JSONata just for NR. To get this to work, all JSONata expressions (in Node-RED) are first prepared (which parses/compiles the expression but also adds in these extra Node-RED functions) then executed. Execution of JSONata from the underlying Node version 3+ now requires a (previously optional) callback function.

You will see from your error log that you get ‘Attempted to invoke a non-function’ and then [red] goes into an Uncaught Exception. If I try $(test) in an inject node I get “[object Object]”, which is not very helpful but certainly not an uncaught exception.
However, when I try $(test) in a get entities node, yes Node-RED shuts down following an uncaught error. This kind of points to the home assistant websocket node JSONata calls perhaps not dealing with the expression error at the prepare stage but passing a corrupt JSONata expression on to the execution stage.

Given that this is due to a bug in your JSONata code, I’d first suggest this is a low priority ‘feature’ - a quick way to shut Node-RED down. Otherwise you may wish to raise it and add it to the issue list

https://github.com/zachowj/node-red-contrib-home-assistant-websocket

Thank you for the analysis.
I have raised a bug report

https://github.com/zachowj/node-red-contrib-home-assistant-websocket/issues/1130