datech / node-red-contrib-amazon-echo

Alexa controlled Node-Red nodes supporting latest Amazon Echo devices. NO Alexa Skills required. NO cloud dependencies.
MIT License
140 stars 42 forks source link

Lightcolor for Node-RED #162

Closed SirFrozzy closed 7 months ago

SirFrozzy commented 3 years ago

Hello,

i want to ask how can i use the light color in node-red. I need a rgb value.

Thanks

nayneyT commented 3 years ago

Hi.

When you say "Alexa set the bedroom light to Red" the Emulated Hue device will send the message:

msg : Object
object
on: true
bri: 254
percentage: 100
hue: 43527
sat: 200
xy: array[2]
0: 0.13550301400290363
1: 0.03987867049354716
ct: 199
rgb: array[3]
0: 54
1: 57
2: 255
colormode: "hs"
meta: object
payload: "on"
deviceid: "abc"
topic: ""
_msgid: "abc.123"

If you are using Home Assistant like me then this is no good because the Home Assistant light service expects the RGB value in the msg.payload.data object.

So you will need to pass the output of the echo device into a function node. With something similar to this:

var data = {data: 
    {
        "transition":0,
        //"brightness": msg.bri,
        "brightness_pct": msg.percentage,
        "rgb_color": [msg.rgb[0], msg.rgb[1], msg.rgb[2]],
        //"color_name": ,
        //"hs_color": [msg.hue, msg.sat],
        //"xy_color": [msg.xy[0], msg.xy[1],
        //"color_temp": msg.ct,
        //"kelvin": ,
        //"white_value": ,
        //"brightness_step": ,
        //"brightness_step_pct": 10,
        //"profile": ,
        //"flash": ,
        //"effect": "
    }};

//var data = {"data":{"transition":"1","brightness_pct":"100"}};

msg.payload=data

return msg;

This works by reading msg.percentage from the original message and also the RGB values and then creating a new data object and returning the amended payload.

msg :
object
domain: "light"
service: "turn_on"
data: object
transition: 0
brightness_pct: 100
rgb_color: array[3]
0: 54
1: 57
2: 255
entity_id: "room.light"

Regards,

Tom

3ative commented 3 years ago

Here's my function node code. Works with RGB Colour and Colour Temperature:

if (msg.payload === "on") msg.payload ={"service": "turn_on",
    data:
    {"rgb_color": msg.rgb, "brightness_pct": msg.percentage}
};
if (msg.payload === "off") msg.payload = {"service":"turn_off"};

else if (msg.colormode === "ct")
msg.payload =
{
    "service": "turn_on",
    data:
    {"rgb_color": [254,254,254], "brightness": msg.bri}
}
return msg

In fact I did a whole series for Alexa | Home Assistant | Node-Red... but, here's the Node-Red tutorial: https://www.youtube.com/watch?v=l1as3tYVy64&list=PLWRTMby105biP9gE08iGLab1FP6n7_mka&index=2