Okay @Ze_Pico, I think i have something for you to try…
I gave this a bit more thought today and realised that it’s actually a lot more complicated than I originally imagined, because there can be on/off commands coming from Alexa and also (presumably) a Blynk switch widget. Also, brightness commands can come from both the Blynk slider and Alexa.
To overcome this I changed the function I wrote to have three outputs:
RGB
Brightness
On/Off
The RGB output is unchanged from what the function did yesterday, but ios only triggered if the command given to alesa was about colour.
The Brightness outputs the percentage brightness (Alexa limits this from 0-100 automatically, and the Blynk slider needs to be limited to 0-100 as well - which I think yours is).
On/Off simply outputs a 0 or 1 to mimic the output from a Blynk switch widget.
In the flow, I’m using these outputs to update the zeRGBa, brightness and on/off widgets so that they stay synchronised with Alexa.
When the RGB vales are changed by either Blynk or Alexa, these are being stored to global (well, flow-level actually) variables called rr, gg and bb. When these values change, I’m forcing a sync on the Brightness slider widget, which triggers the new dimmed RGB levels and outputs these to MagicHome. The “real” RGB values in the zeRGBa widget remain unchanged, so that you know what base value to use when re-calculating the brightness.
For example, setting the room to red, with 50% dimming gives an output to MagicHome of 800000, but the zeRGBa still shows 255,00,00. Changing the brightness to 100% changes the MagicHome output to ff0000. I assume that this is how your current system works?
I’ve tested the outputs intended for MagicHome in debug nodes and they seem okay, but I obviously can’t test the real thing.
Here’s what the flow looks like:
and here’s the the Node-Red flow:
[
{
"id": "5ca5bfb8.5917d",
"type": "tab",
"label": "Flow 3",
"disabled": false,
"info": ""
},
{
"id": "d1579cdd.7445",
"type": "blynk-ws-zergba",
"z": "5ca5bfb8.5917d",
"name": "",
"pin": "1",
"client": "859549ef.52e4b8",
"x": 130,
"y": 120,
"wires": [
[
"c5d51c9f.c7271"
]
]
},
{
"id": "1e2ec852.2be608",
"type": "alexa-home",
"z": "5ca5bfb8.5917d",
"conf": "c0110d92.aab95",
"device": "35106",
"acknoledge": true,
"name": "Room",
"topic": "",
"x": 90,
"y": 260,
"wires": [
[
"150ebc8c.7cf363"
]
]
},
{
"id": "150ebc8c.7cf363",
"type": "function",
"z": "5ca5bfb8.5917d",
"name": "Process Alexa Output",
"func": "if (msg.command ==\"TurnOffRequest\")\n{\n node.status({text: \"Off\"});\n return [null, null, {'payload': \"0\"}];\n}\n node.status({text: \"On\"});\nif (msg.command ==\"TurnOnRequest\")\n{\n \n return [null, null, {'payload': \"1\"}]; \n}\n\nif (msg.command ==\"SetPercentageRequest\")\n{\nnode.status({text: \"Brightness \" + msg.payload}); \n return [null, {'payload': msg.payload}, null]; \n}\n\n\nif (msg.command ==\"SetColorRequest\")\n{\n\nvar hue = msg.payload.hue /360;\nvar sat = msg.payload.saturation;\nvar bri = msg.payload.brightness;\n\nRGB=HSVtoRGB(hue, sat, bri) // Call the function below to convert Alexa output to RGB\n\n// Split the result from the function into its constituent parts...\nvar RED = RGB.r; \nvar BLUE = RGB.b;\nvar GREEN = RGB.g;\n\n// Convert the parts into Hex values...\nvar RED_HEX = (RGB.r).toString(16);\nif (RED_HEX == \"0\")\n{\n RED_HEX=\"00\"\n} \n\nvar GREEN_HEX = (RGB.g).toString(16);\nif (GREEN_HEX == \"0\")\n{\n GREEN_HEX=\"00\"\n} \n\nvar BLUE_HEX = (RGB.b).toString(16);\nif (BLUE_HEX == \"0\")\n{\n BLUE_HEX=\"00\"\n} \n\n\n// Build a message called hex with the RGB Hex value, to mimic the zeRGBa Widget output\nmsg.hex = \"#\" + RED_HEX + GREEN_HEX + BLUE_HEX;\n\n// Replace msg.payload with the same RGB Hex value, to mimic the zeRGBa Widget output\nmsg.payload = \"#\" + RED_HEX + GREEN_HEX + BLUE_HEX;\n\n// Build a message called rgb with the RGB Hex value in a different format, to mimic the zeRGBa Widget output\nmsg.rgb = RED_HEX + \";\" + GREEN_HEX + \";\" + BLUE_HEX;\n\n \n// Add the r, g & b objects to the message, to mimic the zeRGBa Widget output\nmsg.r=RED;\nmsg.g=GREEN;\nmsg.b=BLUE;\n\n// Update the text on the node to show the RGB colour\nnode.status({text: \"[\" + RED + \", \" + GREEN + \", \" + BLUE + \"]\"});\n\n\n// Output all the new message objects\nreturn [msg, null, null]; \n}\n\n\n// Function code taken from:\n// https://stackoverflow.com/questions/17242144/javascript-convert-hsb-hsv-color-to-rgb-accurately\n/* accepts parameters\n * h Object = {h:x, s:y, v:z}\n * OR \n *h, s, v\n*/\nfunction HSVtoRGB(h, s, v) {\n var r, g, b, i, f, p, q, t;\n if (arguments.length === 1) {\n s = h.s, v = h.v, h = h.h;\n }\n i = Math.floor(h * 6);\n f = h * 6 - i;\n p = v * (1 - s);\n q = v * (1 - f * s);\n t = v * (1 - (1 - f) * s);\n switch (i % 6) {\n case 0: r = v, g = t, b = p; break;\n case 1: r = q, g = v, b = p; break;\n case 2: r = p, g = v, b = t; break;\n case 3: r = p, g = q, b = v; break;\n case 4: r = t, g = p, b = v; break;\n case 5: r = v, g = p, b = q; break;\n }\n return {\n r: Math.round(r * 255),\n g: Math.round(g * 255),\n b: Math.round(b * 255)\n };\n}",
"outputs": 3,
"noerr": 0,
"x": 280,
"y": 260,
"wires": [
[
"c5d51c9f.c7271",
"9ffe84dc.6ab2b8"
],
[
"f13be880.025d38",
"3c57c82b.ccd298"
],
[
"464da668.1f3258",
"62cf19a5.6e6028"
]
],
"inputLabels": [
"Input from Alexa or zeRGBa"
],
"outputLabels": [
"RGB Data",
"Brightness Data",
"On/Off Command"
]
},
{
"id": "c5d51c9f.c7271",
"type": "function",
"z": "5ca5bfb8.5917d",
"name": "Store RGB values",
"func": "// store the red green and blue values to flow-level variables so thatwe can use them\n// later in the brightness calculation\n\nflow.set('rr',msg.r); // store the red value \nflow.set('gg',msg.g); // store the green value \nflow.set('bb',msg.b); // store the blue value \n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 370,
"y": 120,
"wires": [
[
"bf96436f.d2d65"
]
]
},
{
"id": "f13be880.025d38",
"type": "function",
"z": "5ca5bfb8.5917d",
"name": "Calc Brightness",
"func": "// initialise PingFailCount_1 to 0 if it doesn't exist already\nvar rr = flow.get('rr')||0; //existing red value\nvar gg = flow.get('gg')||0; //existing green value\nvar bb = flow.get('bb')||0; //existing blue value\n\nvar V = msg.payload; // Brightness value\n\nvar rrr = Math.round(rr*V/100); // dimmed red value\nvar ggg = Math.round(gg*V/100); // dimmed green value\nvar bbb = Math.round(bb*V/100); // dimmed blue value\n\nnode.status({text: \"Dimmed \" + rrr + \", \" + ggg + \", \" + bbb}); \n\n// Convert the parts into Hex values...\nvar RED_HEX = (rrr).toString(16);\nif (RED_HEX == \"0\")\n{\n RED_HEX=\"00\"\n} \n\nvar GREEN_HEX = (ggg).toString(16);\nif (GREEN_HEX == \"0\")\n{\n GREEN_HEX=\"00\"\n} \n\nvar BLUE_HEX = (bbb).toString(16);\nif (BLUE_HEX == \"0\")\n{\n BLUE_HEX=\"00\"\n} \n\n// Build a message called hex with the RGB Hex value, to mimick the zeRGBa Widget output\nmsg.hex = \"#\" + RED_HEX + GREEN_HEX + BLUE_HEX;\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 540,
"y": 400,
"wires": [
[
"e0b14d7f.457e2"
]
],
"outputLabels": [
"On/Off"
]
},
{
"id": "bf975384.c924",
"type": "blynk-ws-in-write",
"z": "5ca5bfb8.5917d",
"name": "Slider Widget on V2 (0-100)",
"pin": "2",
"pin_all": 0,
"client": "859549ef.52e4b8",
"x": 160,
"y": 400,
"wires": [
[
"f13be880.025d38"
]
]
},
{
"id": "3c57c82b.ccd298",
"type": "blynk-ws-out-write",
"z": "5ca5bfb8.5917d",
"name": "Slider Widget V2 - Update Brightness value",
"pin": "2",
"pinmode": 0,
"client": "859549ef.52e4b8",
"x": 670,
"y": 260,
"wires": []
},
{
"id": "464da668.1f3258",
"type": "function",
"z": "5ca5bfb8.5917d",
"name": "Output On/Off command to MagicHome",
"func": "if (msg.payload ==\"0\")\n{\n return [{'payload': '\"on\"' + \":\" + \"false\"}]; \n}\n\nif (msg.payload ==\"1\")\n{\n return [{'payload': '\"on\"' + \":\" + \"true\"}]; \n}",
"outputs": 1,
"noerr": 0,
"x": 620,
"y": 560,
"wires": [
[
"61da61d0.5c7af",
"6a3534a9.0c85fc"
]
],
"outputLabels": [
"On/Off"
]
},
{
"id": "62cf19a5.6e6028",
"type": "blynk-ws-out-write",
"z": "5ca5bfb8.5917d",
"name": "Button Widget V3 - Update On/Off satus",
"pin": "3",
"pinmode": 0,
"client": "859549ef.52e4b8",
"x": 660,
"y": 500,
"wires": []
},
{
"id": "90fd5b03.3816e8",
"type": "blynk-ws-in-write",
"z": "5ca5bfb8.5917d",
"name": "Button Widget on V3 (0/1)",
"pin": "3",
"pin_all": 0,
"client": "859549ef.52e4b8",
"x": 150,
"y": 560,
"wires": [
[
"464da668.1f3258"
]
]
},
{
"id": "9ffe84dc.6ab2b8",
"type": "blynk-ws-zergba",
"z": "5ca5bfb8.5917d",
"name": "zeRGBa Widget V1 - Update Colour value",
"pin": "1",
"client": "859549ef.52e4b8",
"x": 660,
"y": 200,
"wires": [
[]
]
},
{
"id": "61da61d0.5c7af",
"type": "debug",
"z": "5ca5bfb8.5917d",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"x": 1090,
"y": 560,
"wires": []
},
{
"id": "e0b14d7f.457e2",
"type": "function",
"z": "5ca5bfb8.5917d",
"name": "Output Dimmed RGB values to MagicHome",
"func": "return [{'payload': '\"color\"' + \":\" + msg.hex + \"00\"}]; ",
"outputs": 1,
"noerr": 0,
"x": 830,
"y": 400,
"wires": [
[
"61da61d0.5c7af",
"6a3534a9.0c85fc"
]
]
},
{
"id": "bf96436f.d2d65",
"type": "blynk-ws-out-sync",
"z": "5ca5bfb8.5917d",
"name": "",
"pin": "2",
"pinmode": 0,
"client": "859549ef.52e4b8",
"x": 580,
"y": 120,
"wires": []
},
{
"id": "6a3534a9.0c85fc",
"type": "MagicHome UFO",
"z": "5ca5bfb8.5917d",
"name": "",
"ip": "",
"style": "RGBW",
"x": 1150,
"y": 480,
"wires": [
[]
]
},
{
"id": "859549ef.52e4b8",
"type": "blynk-ws-client",
"z": "",
"name": "Development",
"path": "ws://blynk-cloud.com/websockets",
"key": "REDACTED",
"dbg_all": false,
"dbg_read": false,
"dbg_write": false,
"dbg_notify": false,
"dbg_mail": false,
"dbg_prop": false,
"dbg_sync": false,
"dbg_bridge": false,
"dbg_low": false,
"dbg_pins": "",
"multi_cmd": false,
"proxy_type": "no",
"proxy_url": ""
},
{
"id": "c0110d92.aab95",
"type": "alexa-home-conf",
"z": "",
"username": "REDACTED"
}
]
Copy the flow code to your clipboard then in Node-Red click the three horizontal bars in the top right hand cornet and choose Import then Clipboard. Paste the code in the box then click the Import to New Flow button then Import.
You’ll need to go into the Blynk and Amazon and MagicHome nodes to change them to your own stored connections, and change the virtual pins in the Blynk nodes.
Let me know how you get on. If the MagicHome stuff doesn’t work, but the debug message looks about right, then I guess the format of the code in the two functions that output data to MagicHome may need tweaking a little.
I realise that you have RGB sliders sitting alongside your zeRGBa widhet. We (or maybe you) can add these later, once this example works. Adding them in at this stage would just add to the complexity of the flow and make it difficult to get the basics working.
Cheers,
Pete.