Foddy / node-red-contrib-huemagic

Philips Hue node to control bridges, lights, groups, motion sensors, temperature sensors and Lux sensors using Node-RED.
https://flows.nodered.org/node/node-red-contrib-huemagic
Apache License 2.0
201 stars 68 forks source link

Error - [red] Uncaught Exception with v4.0.4 #278

Closed ZEi20T closed 2 years ago

ZEi20T commented 2 years ago

Describe the bug NR fails to load with version huemagic 4.0.4

it crashes then reloads NR and loops this error

14 Jan 10:36:18 - [info] [hue-bridge:Philips hue] Initializing the bridge (192.168.10.16)… [10:36:18] INFO: Starting NGinx... 14 Jan 10:36:19 - [red] Uncaught Exception: 14 Jan 10:36:19 - [error] TypeError: Cannot read properties of undefined (reading 'on') at HueGroup.applyCommands (/config/node-red/node_modules/node-red-contrib-huemagic/huemagic/hue-group.js:320:46) at HueGroup._inputCallback (/config/node-red/node_modules/node-red-contrib-huemagic/huemagic/hue-group.js:95:54) at /opt/node_modules/@node-red/runtime/lib/nodes/Node.js:207:26 at Object.trigger (/opt/node_modules/@node-red/util/lib/hooks.js:149:13) at HueGroup.Node._emitInput (/opt/node_modules/@node-red/runtime/lib/nodes/Node.js:199:11) at HueGroup.Node.emit (/opt/node_modules/@node-red/runtime/lib/nodes/Node.js:183:25) at HueGroup.Node.receive (/opt/node_modules/@node-red/runtime/lib/nodes/Node.js:482:10) at Immediate. (/opt/node_modules/@node-red/runtime/lib/flows/Flow.js:814:52) at processImmediate (node:internal/timers:464:21)

Additional context appears to works fine on huemagic 3.0.0

not too sure if a bug or if its my config

thank you

philosoma commented 2 years ago

I am also getting this error on 2.1.4 and 2.1.5. So far all I have to do to trigger it is join the Hub via API key and restart Node-RED. Everything works fine until its restarted.

rvanbaalen commented 2 years ago

I can't use any light node due to this bug. Since I upgradaded from 3. to 4. NodeRED keeps crashing. When I removed all Hue Light nodes, it worked again.

In my case it crashed on the same error "Cannot read properties of undefined (reading 'on')" but from a different file (hue-light.js).

It's unfortunate because I needed the Dimmer Switch v2 support. I'll try to do some digging myself.

rvanbaalen commented 2 years ago

Did some more digging. My error is happening on this line

https://github.com/Foddy/node-red-contrib-huemagic/blob/master/huemagic/hue-light.js#L349

the code block on that line:

// SET LIGHT STATE SIMPLE MODE
if(msg.payload === true||msg.payload === false)
{
    if(msg.payload !== currentState.payload.on) { patchObject["on"] = { on: msg.payload }; }
}

I injected some console logs and these are the results:

msg {
  topic: '',
  payload: false,
  _msgid: '9355e346ea8b1f76',
  _event: 'node:a26c693eea765b47'
}

currentState false

patchObject {}

It seems that currentState.payload.on is accessed but currentState is not an object at that point.

Edit: furthering down the rabbit hole, I discovered that the currentState in hue-light.js is false because hue-bridge-config.js returns false on line 402: https://github.com/Foddy/node-red-contrib-huemagic/blob/master/huemagic/hue-bridge-config.js#L402

That's because here https://github.com/Foddy/node-red-contrib-huemagic/blob/master/huemagic/hue-bridge-config.js#L296 scope.resources is in fact an empty object.

hue-bridge-config.js has a this.start() function and a series of async responses to that. However, the code only hits the first "then" statement and after that the this.get() call is already made causing things to jam up.

@Foddy is this helpful for your debugging purposes?

Foddy commented 2 years ago

Can you guys please try to reconfigure your Hue Bridge configuration and try it again? It is sometimes better to delete the old configuration first and retry with a "new" bridge.

rvanbaalen commented 2 years ago

I did. I removed this plugin, went over all my flows and removed all Hue nodes, reinstalled and reconfigured the config node. Instant crash as soon as I deployed a flow with a Hue light.

My Hue bridge is the square one with software version 1.49.1949107040

shawnli87 commented 2 years ago

I have the same issue here. I'm running Node-RED 10.3.2 on HA Supervised. I can replicate the issue on all v4.0.0+. Unfortunately, v3.0.0 is unable to communicate with my bridge.

The issue only occurs for me when I have a "Hue Light" node on an enabled flow. I have "Hue Motion" nodes on a flow, but they are not causing the crash to occur. As a workaround, I am currently using an http request node to send commands directly to the Hue API.

rvanbaalen commented 2 years ago

@Foddy is there anything I can do/debug that will help you pinpoint this issue? Any tests I can do for you? I'd like to assist you to get this resolved asap.

Foddy commented 2 years ago

Hmm very strange, as I am unable to reproduce this. This must be a flow related issue. Do you fire an event right after you deploy a flow to a HueMagic node?

rvanbaalen commented 2 years ago

Let me look into that. I do know that I have one node-red-contrib-light-scheduler node that is talking to one of my Hue Switches to turn it on and off on a schedule. Let me strip it down to the bare basics. I'll comment when I'm done.

Update 1:

I've stopped node-red, uninstalled huemagic, removed all hue related nodes and installed huemagic again. Now I'll go ahead and set up the config node and a single hue light.

Update 2:

@Foddy yes, that's it! The light-scheduler is firing a payload directly when the flow starts and that happens before huemagic got the time to initialize properly. Hue works just fine when I don't have the light-schedule hooked up to it, and it keeps crashing as soon as I hook a light-schedule directly to the hue-light node.

@ZEi20T is something similar happening in your situation? Some of your Hue nodes (a Hue group perhaps) receiving payloads immediately?

I'll go ahead and see if I can prevent this issue by creating a delay when the flow starts for the first time.

Final update; the fix

I set up a 10s delay node between the light-schedule node and the Hue Light node. Now the hue-light node does not receive payloads before it's ready. It's not ideal but at least I can use the latest huemagic version without issues! 🥳

Foddy commented 2 years ago

Nice! I think that caused the error for the others as well. I just released a new updated/fixed version. The command is still not executed until HueMagic is connected to the bridge, but at least the fatal error (Node-RED restart) is no longer produced and a notification message is issued.

rvanbaalen commented 2 years ago

That's awesome, @Foddy! I'll update right away and give it a try without the delay node inbetween.

rvanbaalen commented 2 years ago

That does the trick. I do get the following message now:

"The light in not yet available. Please wait for the bridge to connect before sending any command."

That's a lot better than an indefinitely rebooting NodeRED ;-)

Foddy commented 2 years ago

Haha, yeah I think so too! :D Please feel free to reopen this issue if the error still occurs on your side @ZEi20T / @philosoma / @shawnli87.