TypeError: Cannot read property 'brightness' of undefined

Hi,
I'm using node-red-contrib-googlehome to connect my google home with node red and i have a problem
when using a node that simulates color light the function node that is after it throws this error: "TypeError: Cannot read property 'brightness' of undefined". And i dont know why because when using a debug node and setting it to display msg.payload.params.brightness it show right value. Even when using a function node that has:"msg.ledbrightness=msg.payload.params.brightness;"code inside it no error is shown. Please help!
Here is code from my function node that is throwing error:

if (msg.payload.params.color != undefined){
    msg.payload=msg.payload.params.color.spectrumRGB.toString(16);
    flow.ledcolor = msg.payload;
    if(msg.payload.length == 2){
        msg.payload = "0000"+msg.payload;
    }else if(msg.payload.length == 4){
        msg.payload = "00"+msg.payload;
    }
}else if(msg.payload.params.on != undefined){
    if(msg.payload.params.on){
        msg.payload=flow.ledcolor;
    }else{
        msg.payload="000000";
    }
}else{
    msg.payload=flow.ledcolor;
    msg.ledbrightness=msg.payload.params.brightness;
}

return msg;

Because you overwrite msg.payload in above code section, therefore msg.payload.brightness is no longer is defined.
[edit] may not be the case as i see you copy msg.payload and later replace it. But you may be replacing it with an empty value if the conditional where it's copied is not executed.

Yeah! thats it but now when multipyling number by flow.ledbrightness here:

msg.r=msg.payload[0]*4*flow.ledbrightness;
msg.g=msg.payload[1]*4*flow.ledbrightness;
msg.b=msg.payload[2]*4*flow.ledbrightness;

return msg;

Values of msg.r, msg.g, and msg.b is NaN instead of value. Why?
PS. I replaced:

msg.payload=flow.ledcolor;
msg.ledbrightness=msg.payload.params.brightness;

with:

flow.ledbrightness=msg.payload.params.brightness;
msg.payload=flow.ledcolor;

{edit} im using Hex to rgb converter that returnes array that is why there is [0], [1] and [2]

How did msg.payload become an array? as the code posted it was an object.
Is this a different function node?

im using node-red-contrib-color-convert which converts hex to arrayZrzut ekranu

Hard to say not enough info. post flow and input objects

Output from first node:
first image-when changing brightness
second image-when changing color
third image-when changing on - off

The idea is

  1. getting input from google home
  2. searching what was changed
if (msg.payload.params.color != undefined){
    msg.payload=msg.payload.params.color.spectrumRGB.toString(16);
    flow.ledcolor = msg.payload;
    if(msg.payload.length == 2){
        msg.payload = "0000"+msg.payload;
    }else if(msg.payload.length == 4){
        msg.payload = "00"+msg.payload;
    }
}else if(msg.payload.params.on != undefined){
    if(msg.payload.params.on){
        msg.payload=flow.ledcolor;
    }else{
        msg.payload="000000";
    }
}else{
    flow.ledbrightness=msg.payload.params.brightness;
    msg.payload=flow.ledcolor;
}

return msg;

3.Changing HEX to RGB {takes string returnes array}
4. making everything right value

msg.r=msg.payload[0]*4*flow.ledbrightness;
msg.g=msg.payload[1]*4*flow.ledbrightness;
msg.b=msg.payload[2]*4*flow.ledbrightness;

return msg;
  1. deleting unnecesarry objects (for example in first one deletes msg.g msg.b and move msg.r to payload)

on-off

i think i mis understood flow variables i will try with flow.get() and flow.set()

I was going to ask if they are supposed to be context storage vars.

thank you for you attention everything is working :smiley:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.