BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

My home automation projects built with MQTT and Node Red

node-red
mqtt
home_automation

#61

do you want me to change any thing in your original flow before I start


#62

Yes, do the following:

Update Calc Brightness with this code:

// initialise PingFailCount_1 to 0 if it doesn't exist already
var rr = flow.get('rr')||0; //existing red value
var gg = flow.get('gg')||0; //existing green value
var bb = flow.get('bb')||0; //existing blue value

var V = msg.payload; // Brightness value

var rrr = Math.round(rr*V/100); // dimmed red value
var ggg = Math.round(gg*V/100); // dimmed green value
var bbb = Math.round(bb*V/100); // dimmed blue value

node.status({text: V + "% " + "[" + rrr + ", " + ggg + ", " + bbb + "]"});  

// Convert the parts into Hex values...
var RED_HEX = (rrr).toString(16);
if (RED_HEX == "0")
{
    RED_HEX="00"
}    

var GREEN_HEX = (ggg).toString(16);
if (GREEN_HEX == "0")
{
    GREEN_HEX="00"
}  

var BLUE_HEX = (bbb).toString(16);
if (BLUE_HEX == "0")
{
    BLUE_HEX="00"
}   

// Build a message called hex with the RGB Hex value, to mimick the zeRGBa Widget output
msg.hex = "#" +  RED_HEX + GREEN_HEX + BLUE_HEX;

return msg;

Update Output Dimmed RGB values to MagicHome with this code:

return [{'color': msg.hex}]; 

Update Output On/Off command to MagicHome with this code:

if (msg.payload =="0")
{
   return [{'on': "false"}]; 
}

if (msg.payload =="1")
{
   return [{'on': "true"}]; 
}

Add this sync node (It’ll be V40 on your system rather than V1):

image

Pete.


#63

4 Nov 16:21:27 - [error] [MagicHome UFO:Living Room Strip] TypeError: Cannot read property ‘on’ of undefined
4 Nov 16:21:42 - [error] [MagicHome UFO:Living Room Strip] TypeError: Cannot read property ‘on’ of undefined
4 Nov 16:22:11 - [error] [MagicHome UFO:Living Room Strip] TypeError: Cannot read property ‘on’ of undefined
4 Nov 16:22:26 - [error] [MagicHome UFO:Living Room Strip] TypeError: Cannot read property ‘on’ of undefined


#64

Why is your MagicHome UFO node showing as Offline? Is it correctly configured?

Pete.


#65

Yes, I just tried it.

because I don’t know js I can’t help you.
few days ago I found something interesting for you.
how to backup original firmware from ESP8266 or ESP8255 before flashing a new sketch.
you could have done it before flashing your Magic Home.


#66

I’ve spent most of the evening trying to get the RGB controller that I hacked to work with Blynk re-flashed with Tasmota and working as a MagicHome device (which it’s supposed to support).
Although I can control it through Tasmota using MQTT, I can’t connect to it with either the MagicHome UFO node or the Magic home app on an iPhone.

What I have discovered is that while the MagicHome UFO node is in Disconnected state, Node-Red will keep restarting. If you open a Putty console and type node-red it will show the Node-Red console messages and as soon as the connection to the IP address of the MagicHome device is refused it throws an unhandled error and Node-Red restarts and tries again.

I’m not sure how to take this any further, as I wont be able to get hold of any working Node-Red hardware in the next couple of days and after that I’l be in Spain for a while.

I control my RGB devices by sending separate MQTT messages containing colour information for each channel. After what I’ve learned with this exercise, I’m going to change the way that I do this, with a single RRGGBB message instead. I’ll adopt the code that I’ve developed here so that I have full Alexa control out of the box, without the need to do any more processing on it.
Once I’ve got this working I’ll share the ESP sketch and if you want to re-flash your devices then you could do that. In the meantime, it would probably be worth you dong a bit more testing with the code to see if you can get any further and make sense of what’s happening when the messages are sent to the MagicHome device.

Pete.

Pete.


#67

@PeteKnight Well Done Pete

IT IS WORKING now :laughing:

I adjusted the output to the same format I am receiving from the injected nodes.

Clipboard Copy

[
    {
        "id": "18134620.f45efa",
        "type": "blynk-ws-zergba",
        "z": "15e37b6b.837485",
        "name": "",
        "pin": "40",
        "client": "85df424e.f59f5",
        "x": 150,
        "y": 140,
        "wires": [
            [
                "55f7817.a5af98"
            ]
        ]
    },
    {
        "id": "2edf8d81.029a92",
        "type": "alexa-home",
        "z": "15e37b6b.837485",
        "conf": "bc4b6845.74d4e8",
        "device": "35277",
        "acknoledge": true,
        "name": "Room",
        "topic": "",
        "x": 110,
        "y": 280,
        "wires": [
            [
                "2cda05a9.7061ea"
            ]
        ]
    },
    {
        "id": "2cda05a9.7061ea",
        "type": "function",
        "z": "15e37b6b.837485",
        "name": "Process Alexa Output",
        "func": "if (msg.command ==\"TurnOffRequest\")\n{\n    node.status({ text: 'OFF' });\n    return [null, null, {'payload': '0' }];\n}\n\nif (msg.command ==\"TurnOnRequest\")\n{\n    node.status({ text: \"ON\" });  \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": 300,
        "y": 280,
        "wires": [
            [
                "55f7817.a5af98",
                "45697ad0.4b5514"
            ],
            [
                "46db1b3.03d04e4",
                "5174c31b.a215ac"
            ],
            [
                "2f03c1d6.64b66e",
                "6fde450e.6fb65c"
            ]
        ],
        "inputLabels": [
            "Input from Alexa or zeRGBa"
        ],
        "outputLabels": [
            "RGB Data",
            "Brightness Data",
            "On/Off Command"
        ]
    },
    {
        "id": "55f7817.a5af98",
        "type": "function",
        "z": "15e37b6b.837485",
        "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": 390,
        "y": 140,
        "wires": [
            [
                "1c673b95.812c54"
            ]
        ]
    },
    {
        "id": "46db1b3.03d04e4",
        "type": "function",
        "z": "15e37b6b.837485",
        "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: V + \"% \" + \"[\" + 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": 600,
        "y": 360,
        "wires": [
            [
                "60e885a6.fe926c"
            ]
        ],
        "outputLabels": [
            "On/Off"
        ]
    },
    {
        "id": "21169dbc.504a62",
        "type": "blynk-ws-in-write",
        "z": "15e37b6b.837485",
        "name": "Slider Widget on V70  (0-100)",
        "pin": "70",
        "pin_all": 0,
        "client": "85df424e.f59f5",
        "x": 180,
        "y": 360,
        "wires": [
            [
                "46db1b3.03d04e4"
            ]
        ]
    },
    {
        "id": "5174c31b.a215ac",
        "type": "blynk-ws-out-write",
        "z": "15e37b6b.837485",
        "name": "Slider Widget V70 - Update Brightness value",
        "pin": "70",
        "pinmode": 0,
        "client": "85df424e.f59f5",
        "x": 690,
        "y": 280,
        "wires": []
    },
    {
        "id": "2f03c1d6.64b66e",
        "type": "function",
        "z": "15e37b6b.837485",
        "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}\n",
        "outputs": 1,
        "noerr": 0,
        "x": 680,
        "y": 480,
        "wires": [
            [
                "779ca125.dcda6",
                "d5aad9b5.91ef98",
                "6b39eb1d.0d9064"
            ]
        ],
        "outputLabels": [
            "On/Off"
        ]
    },
    {
        "id": "6fde450e.6fb65c",
        "type": "blynk-ws-out-write",
        "z": "15e37b6b.837485",
        "name": "Button Widget V71 - Update On/Off satus",
        "pin": "71",
        "pinmode": 0,
        "client": "85df424e.f59f5",
        "x": 680,
        "y": 420,
        "wires": []
    },
    {
        "id": "5f319640.d67c88",
        "type": "blynk-ws-in-write",
        "z": "15e37b6b.837485",
        "name": "Button Widget on V71 (0/1)",
        "pin": "71",
        "pin_all": 0,
        "client": "85df424e.f59f5",
        "x": 170,
        "y": 480,
        "wires": [
            [
                "2f03c1d6.64b66e"
            ]
        ]
    },
    {
        "id": "45697ad0.4b5514",
        "type": "blynk-ws-zergba",
        "z": "15e37b6b.837485",
        "name": "zeRGBa Widget V40 - Update Colour value",
        "pin": "40",
        "client": "85df424e.f59f5",
        "x": 680,
        "y": 220,
        "wires": [
            []
        ]
    },
    {
        "id": "779ca125.dcda6",
        "type": "debug",
        "z": "15e37b6b.837485",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "x": 1190,
        "y": 480,
        "wires": []
    },
    {
        "id": "60e885a6.fe926c",
        "type": "function",
        "z": "15e37b6b.837485",
        "name": "Output Dimmed RGB values to MagicHome",
        "func": "return [{'payload':{ color :  msg.hex + \"00\"}}];\n",
        "outputs": 1,
        "noerr": 0,
        "x": 890,
        "y": 360,
        "wires": [
            [
                "779ca125.dcda6",
                "6b39eb1d.0d9064"
            ]
        ]
    },
    {
        "id": "1c673b95.812c54",
        "type": "blynk-ws-out-sync",
        "z": "15e37b6b.837485",
        "name": "",
        "pin": "70",
        "pinmode": 0,
        "client": "85df424e.f59f5",
        "x": 600,
        "y": 140,
        "wires": []
    },
    {
        "id": "6b39eb1d.0d9064",
        "type": "MagicHome UFO",
        "z": "15e37b6b.837485",
        "name": "Living Room Strip",
        "ip": "192.168.xxx.xxx",
        "style": "RGBW",
        "x": 1230,
        "y": 360,
        "wires": [
            []
        ]
    },
    {
        "id": "d5aad9b5.91ef98",
        "type": "blynk-ws-out-sync",
        "z": "15e37b6b.837485",
        "name": "",
        "pin": "40",
        "pinmode": 0,
        "client": "85df424e.f59f5",
        "x": 990,
        "y": 580,
        "wires": []
    },
    {
        "id": "64f47c0b.236424",
        "type": "inject",
        "z": "15e37b6b.837485",
        "name": "OFF",
        "topic": "",
        "payload": "{\"on\":false,\"brightness\":0,\"color\":\"#00000000\"}",
        "payloadType": "json",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 990,
        "y": 260,
        "wires": [
            [
                "6b39eb1d.0d9064"
            ]
        ]
    },
    {
        "id": "297a5ea5.534f32",
        "type": "inject",
        "z": "15e37b6b.837485",
        "name": "BLUE",
        "topic": "",
        "payload": "{\"on\":true,\"brightness\":0,\"color\":\"#0000FF\"}",
        "payloadType": "json",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 990,
        "y": 220,
        "wires": [
            [
                "6b39eb1d.0d9064"
            ]
        ]
    },
    {
        "id": "fad78792.045ec8",
        "type": "inject",
        "z": "15e37b6b.837485",
        "name": "GREEN",
        "topic": "",
        "payload": "{ \"on\":  true,\"brightness\": 0,\"color\": \"#00FF0000\"}",
        "payloadType": "json",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 990,
        "y": 180,
        "wires": [
            [
                "6b39eb1d.0d9064",
                "9d692024.c1487"
            ]
        ]
    },
    {
        "id": "e88303d8.e579c",
        "type": "inject",
        "z": "15e37b6b.837485",
        "name": "RED",
        "topic": "",
        "payload": "{ \"on\":  true,\"brightness\": 0,\"color\": \"#FF000000\"}",
        "payloadType": "json",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 990,
        "y": 140,
        "wires": [
            [
                "6b39eb1d.0d9064"
            ]
        ]
    },
    {
        "id": "9d692024.c1487",
        "type": "debug",
        "z": "15e37b6b.837485",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "x": 1190,
        "y": 180,
        "wires": []
    },
    {
        "id": "85df424e.f59f5",
        "type": "blynk-ws-client",
        "z": "",
        "name": "",
        "path": "ws://xxx.xxx.xxx.xxx/websockets",
        "key": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx5eb45",
        "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": "bc4b6845.74d4e8",
        "type": "alexa-home-conf",
        "z": "",
        "username": "xxxx"
    }
]

PuTTy output

5 Nov 17:09:04 - [info] [MagicHome UFO:Living Room Strip] #00ff0000
5 Nov 17:09:04 - [info] [MagicHome UFO:Living Room Strip] 0, 255, 0, 0, 0
5 Nov 17:09:11 - [info] [MagicHome UFO:Living Room Strip] #00ff0000
5 Nov 17:09:11 - [info] [MagicHome UFO:Living Room Strip] 0, 255, 0, 0, 0
5 Nov 17:09:25 - [info] [MagicHome UFO:Living Room Strip] #ff000000
5 Nov 17:09:25 - [info] [MagicHome UFO:Living Room Strip] 255, 0, 0, 0, 0
5 Nov 17:09:31 - [info] [MagicHome UFO:Living Room Strip] #0000ff00
5 Nov 17:09:31 - [info] [MagicHome UFO:Living Room Strip] 0, 0, 255, 0, 0
5 Nov 17:09:39 - [info] [MagicHome UFO:Living Room Strip] #00008000
5 Nov 17:09:39 - [info] [MagicHome UFO:Living Room Strip] 0, 0, 128, 0, 0
5 Nov 17:09:46 - [info] [MagicHome UFO:Living Room Strip] #00808000
5 Nov 17:09:46 - [info] [MagicHome UFO:Living Room Strip] 0, 128, 128, 0, 0


#68

Hey @Ze_Pico that’s great!
Well-done for sticking with it. I’m sure there’s plenty of tweaking to do to get it working the way you want with all your devices and extra Blynk sliders etc, but at least we know that the basics now work.

Pete.