Function: how to create a message with an array (beginner)

Hi programmers,

I'm trying to programm a function, which should transform the message of a dashboard-colopicker to a message, which is readable from a HUEmagic node.

The colorpicker sends this message:

colopicker

The HUEmagic group, which I want to control (I want to change the RGB values) sends this:

So I want to create a function, which takes the color-picker rgb values and makes a message with a payload.rgb.

My function doesn't work, here it is:

// Variablen

var colorR;
var colorG;
var colorB;

/// Programm

colorR = msg.payload.r; 
colorG = msg.payload.g;
colorB = msg.payload.b;

var msg_neu ={};
msg_neu.payload.rgb[0] = colorR;
msg_neu.payload.rgb[1] = colorG;
msg_neu.payload.rgb[2] = colorB;

return [msg_neu];


The debug node of the function says:

[TypeError: Cannot read property 'rgb' of undefined]

Hope I formatted everything nice and I hope you can help me. I think it's a format error, but don't know how to fix it..

Thanks for your advice!

Bye, Chris

You need to initialise msg_neu.payload = {} and msg_neu.payload.rgb = []

Thanks! That worked. Really had troubles to find a nice tutorial to this but now it works :slight_smile:

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