@salvacalatayud
@EvJd
The posted issues look similar.
I suspect that messages do not come through, as the the created array might not be correct.
I do not see it in my system, but the cause might be that my system is a single threaded device.
I expected that the main path is faster and so arrives earlier than the path through the Function Node and the HSV > RGB node
That means that the main path is in array[0] and the RGB info in array[1]. If coincidentally it is the other way around the Function node will fail to execute the code.
Therefore I took the code, published by @xury, February 12, 2020, at page 4 in this thread. Similar code I also found in another Javascript forum
I modified both Function nodes and inserted the code in the Function node:
To test:
Remove
- Remove Function node: Create RGB array
- Remove Conversion node: RGB > HSV
- Remove join node
Replace Fucntion In, by new Function node with the following code.
Code: Select all
msg.topic = msg.payload.idx;
function rgb2hsv (r, g, b) {
let rabs, gabs, babs, rr, gg, bb, h, s, v, diff, diffc, percentRoundFn;
rabs = r / 255;
gabs = g / 255;
babs = b / 255;
v = Math.max(rabs, gabs, babs);
diff = v - Math.min(rabs, gabs, babs);
diffc = c => (v - c) / 6 / diff + 1 / 2;
percentRoundFn = num => Math.round(num * 100) / 100;
if (diff === 0) {
h = s = 0;
} else {
s = diff / v;
rr = diffc(rabs);
gg = diffc(gabs);
bb = diffc(babs);
if (rabs === v) {
h = bb - gg;
} else if (gabs === v) {
h = (1 / 3) + rr - bb;
} else if (babs === v) {
h = (2 / 3) + gg - rr;
}
if (h < 0) {
h += 1;
}else if (h > 1) {
h -= 1;
}
}
return {
hue: Math.round(h * 360),
saturation: percentRoundFn(s),
value: percentRoundFn(v)
};
}
if (typeof msg.payload.Color === 'undefined') {
msg.payload.color = {};
}
if (msg.payload.nvalue !== 0) {
msg.payload = {
on : true,
brightness : msg.payload.Level,
color : { spectrumHsv : rgb2hsv(msg.payload.Color.r,msg.payload.Color.g,msg.payload.Color.b)}
}
}
else if (msg.payload.nvalue === 0) {
msg.payload = {
on : false,
brightness : msg.payload.Level,
color : { spectrumHsv : rgb2hsv(msg.payload.Color.r,msg.payload.Color.g,msg.payload.Color.b)}
}
}
return msg;
Do the same with:
Remove
- Remove Function node: Create HSV array
- Remove Conversion node: HSV > RGB
- Remove join node
Replace Function Out, by new Function node with the following code.
Code: Select all
var idx = parseInt(msg.topic);
var cc = {};
function mix(a, b, v)
{
return (1-v)*a + v*b;
}
function HSVtoRGB(H, S, V)
{
var V2 = V * (1 - S);
var r = ((H>=0 && H<=60) || (H>=300 && H<=360)) ? V : ((H>=120 && H<=240) ? V2 : ((H>=60 && H<=120) ? mix(V,V2,(H-60)/60) : ((H>=240 && H<=300) ? mix(V2,V,(H-240)/60) : 0)));
var g = (H>=60 && H<=180) ? V : ((H>=240 && H<=360) ? V2 : ((H>=0 && H<=60) ? mix(V2,V,H/60) : ((H>=180 && H<=240) ? mix(V,V2,(H-180)/60) : 0)));
var b = (H>=0 && H<=120) ? V2 : ((H>=180 && H<=300) ? V : ((H>=120 && H<=180) ? mix(V2,V,(H-120)/60) : ((H>=300 && H<=360) ? mix(V,V2,(H-300)/60) : 0)));
return {
r : Math.round(r * 255),
b : Math.round(b * 255),
g : Math.round(g * 255)
};
}
//msg.payload.color = HSVtoRGB(msg.payload.color.spectrumHsv.hue, msg.payload.color.spectrumHsv.saturation, msg.payload.color.spectrumHsv.value);
//cc = HSVtoRGB(msg.payload.color.spectrumHsv.hue, msg.payload.color.spectrumHsv.saturation, msg.payload.color.spectrumHsv.value);
if ( msg.payload.on === true )
{
msg.payload = {
"command" : "setcolbrightnessvalue",
"idx" : idx,
"brightness" : msg.payload.brightness,
"color" : {...{"m":3,"t":0,"cw":0,"ww":0},...HSVtoRGB(msg.payload.color.spectrumHsv.hue, msg.payload.color.spectrumHsv.saturation, msg.payload.color.spectrumHsv.value)}
};
}
else if ( msg.payload.on === false )
{
msg.payload = {
"command": "switchlight",
"idx": idx,
"switchcmd": "Off"
};
}
return msg
Has something to do with merging two objects into one object, but the desired result is produced. So it works.
Awaiting the test results.
Regards