Integrating sonoff,tuya and others using alexa, node-red and virtual http switch plugin

To integrate sonoff, Tuya, Smartlife, Magichome and any other wifi devices that is connected to Amazon Alexa, you can also trigger alexa routines. There is no need to flash the devices.

This will give you the ability to turn on/off devices via vera and get status updates when using alexa voice control (still working on full status updates)

Sonoff devices will also give status feed back when turned on/off by Ewelink app

Requirements:-

  1. node-red Getting Started : Node-RED
    running locally with nodes -
    node-red-contrib-alexa-remote2.
    node-red-contrib-ewelink.
    Installed via palette in node-red browser GUI.
  2. Amazon echo, dot or show, account.
  3. Vera Virtual http switch plugin.
  4. Ewelink android App, account

Flow Import json

[{"id":"222893d1.979df4","type":"tab","label":"Flow 4","disabled":false,"info":""},{"id":"3381cb80.18158c","type":"comment","z":"222893d1.979df4","name":"Context updater - used when you need to add/remove new device context data","info":"","x":1010,"y":260,"wires":[]},{"id":"9cb99e2.debcb6","type":"function","z":"222893d1.979df4","name":"global context set","func":"var ids = {\n            //ewelink data\n            \"1000298253\": [146, \"Status\", \"urn:upnp-org:serviceId:SwitchPower1\"],\n            \"10009fc033\": [153, \"Status\", \"urn:upnp-org:serviceId:SwitchPower1\"],\n            \"10008186bb\": [154, \"Status\", \"urn:upnp-org:serviceId:SwitchPower1\"],\n            \"100008a221\": [159, \"Status\", \"urn:upnp-org:serviceId:SwitchPower1\"],\n            \"1000a4a4b3\": [160, \"Status\", \"urn:upnp-org:serviceId:SwitchPower1\"],\n            \"1000a40bf5\": [161, \"Status\", \"urn:upnp-org:serviceId:SwitchPower1\"],\n            \"1000056d37\": [169, \"Status\", \"urn:upnp-org:serviceId:SwitchPower1\"],\n            \"100007985b\": [173, \"Status\", \"urn:upnp-org:serviceId:SwitchPower1\"],\n            \"1000a48dd2\": [178, \"Tripped\", \"urn:micasaverde-com:serviceId:SecuritySensor1\"],\n            \n            //alexa data\n            //tv\n            \"turn_on_lounge_t_v\": [171, \"Status\", \"urn:upnp-org:serviceId:SwitchPower1\", 1],\n            \"turn_off_lounge_t_v\": [171, \"Status\", \"urn:upnp-org:serviceId:SwitchPower1\", 0],\n            // fountain light\n            \"turn_on_water_light\": [172, \"Status\", \"urn:upnp-org:serviceId:SwitchPower1\", 1],\n            \"turn_off_water_light\": [172, \"Status\", \"urn:upnp-org:serviceId:SwitchPower1\", 0],\n            \"water_light_on\": [172, \"Status\", \"urn:upnp-org:serviceId:SwitchPower1\", 1],\n            \"water_light_off\": [172, \"Status\", \"urn:upnp-org:serviceId:SwitchPower1\", 0]\n}\nglobal.set(\"ids\", ids)\nreturn msg;","outputs":1,"noerr":0,"x":940,"y":300,"wires":[[]]},{"id":"15c0584a.06b15","type":"comment","z":"222893d1.979df4","name":"Vera device staus updaters","info":"","x":790,"y":340,"wires":[]},{"id":"83cb06d.ccf77f8","type":"inject","z":"222893d1.979df4","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":740,"y":300,"wires":[["9cb99e2.debcb6"]]},{"id":"dc5dbac6.f2ce48","type":"alexa-remote-event","z":"222893d1.979df4","name":"","account":"f4d129f8.797b8","event":"ws-device-activity","x":750,"y":380,"wires":[["821b0c9.debe57"]]},{"id":"821b0c9.debe57","type":"function","z":"222893d1.979df4","name":"create payload","func":"var ids = global.get(\"ids.\" + msg.payload.description.summary.replace(/^computer |\\./g, \"\").replace(/ /g, \"_\"));\nif (ids && msg.payload.activityStatus === \"SUCCESS\") {msg.payload = {\n    \"id\": \"variableset\",\n    \"DeviceNum\":ids[0],\n    \"serviceId\":ids[2],\n    \"Variable\": ids[1],\n    \"Value\": ids[3]\n    }\n}else{\n    msg.payload = null\n}\nreturn msg;\n","outputs":1,"noerr":0,"x":960,"y":380,"wires":[["2d0707d4.c58bc"]]},{"id":"d8da6592.ddb058","type":"ewelink-event-listener","z":"222893d1.979df4","name":"ewelink listener","deviceId":"","auth":"8c44a06e.fd78e","x":740,"y":420,"wires":[["3c400666.ee3a0a"]]},{"id":"3c400666.ee3a0a","type":"function","z":"222893d1.979df4","name":"create payload","func":"var ids = global.get(\"ids.\" + msg.payload.deviceid)\nif (ids) {msg.payload = {\n    \"id\": \"variableset\",\n    \"DeviceNum\":ids[0],\n    \"serviceId\":ids[2],\n    \"Variable\": ids[1],\n    \"Value\":(msg.payload.params.switch == \"on\" ? 1 : 0)\n    }\n}\nelse{\nmsg.payload = null\n}\nreturn msg;\n","outputs":1,"noerr":0,"x":940,"y":420,"wires":[["2d0707d4.c58bc"]]},{"id":"2d0707d4.c58bc","type":"switch","z":"222893d1.979df4","name":"","property":"payload","propertyType":"msg","rules":[{"t":"nnull"},{"t":"null"}],"checkall":"true","repair":false,"outputs":2,"x":1110,"y":420,"wires":[["40e3f6b.e458c88"],[]]},{"id":"40e3f6b.e458c88","type":"http request","z":"222893d1.979df4","name":"","method":"GET","ret":"txt","paytoqs":true,"url":"http://192.168.1.11:3480/data_request","tls":"","persist":false,"proxy":"","authType":"","x":1270,"y":420,"wires":[["f49e7a32.45b43"]]},{"id":"f49e7a32.45b43","type":"debug","z":"222893d1.979df4","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1420,"y":420,"wires":[]},{"id":"38efec8b.c073e4","type":"http in","z":"222893d1.979df4","name":"get request","url":"/routine","method":"get","upload":false,"swaggerDoc":"","x":730,"y":520,"wires":[["3658c0ff.1f683"]]},{"id":"a6e6ad67.1c859","type":"comment","z":"222893d1.979df4","name":"Trigger routines wia http request","info":"","x":810,"y":480,"wires":[]},{"id":"3658c0ff.1f683","type":"function","z":"222893d1.979df4","name":"create json","func":"msg.payload = {\"type\": msg.payload.type,\n               \"payload\": {\"routine\": msg.payload.device.replace(/_/g,\" \"),\n                            \"device\": \"Lounge\"\n               }\n}\n\nreturn msg;\n","outputs":1,"noerr":0,"x":910,"y":520,"wires":[["ba2ee031.ec9c4"]]},{"id":"ba2ee031.ec9c4","type":"alexa-remote-routine","z":"222893d1.979df4","name":"","account":"f4d129f8.797b8","routineNode":{"type":"custom","payload":{"type":"msg","value":"payload"}},"x":1110,"y":520,"wires":[["c2456b.d64aea98"]]},{"id":"c2456b.d64aea98","type":"function","z":"222893d1.979df4","name":"create payload","func":"msg.payload = {\n    \"id\":\"variableset\",\n    \"DeviceNum\":msg.req.query.vid,\n    \"serviceId\":\"urn:upnp-org:serviceId:SwitchPower1\",\n    \"Variable\":\"Status\",\n    \"Value\":((msg.payload.success && msg.req.query.value == 1) ? 1 : 0)\n    }\nreturn msg;\n","outputs":1,"noerr":0,"x":1320,"y":520,"wires":[["41a7117.175f17"]]},{"id":"5ecd2fa4.03e5a8","type":"http in","z":"222893d1.979df4","name":"post request","url":"/routine","method":"post","upload":false,"swaggerDoc":"","x":730,"y":560,"wires":[["3658c0ff.1f683"]]},{"id":"6851a524.f58a94","type":"comment","z":"222893d1.979df4","name":"Turn on/off alexa devices via http request","info":"","x":850,"y":600,"wires":[]},{"id":"31de3be2.d8fcfc","type":"http in","z":"222893d1.979df4","name":"get request","url":"/alexa","method":"get","upload":false,"swaggerDoc":"","x":730,"y":640,"wires":[["f9e9744e.88fa88"]]},{"id":"5d6be223.4ffa44","type":"http in","z":"222893d1.979df4","name":"post request","url":"/alexa","method":"post","upload":false,"swaggerDoc":"","x":730,"y":680,"wires":[["f9e9744e.88fa88"]]},{"id":"f9e9744e.88fa88","type":"function","z":"222893d1.979df4","name":"create json","func":"msg.payload = [{\"entity\":msg.payload.device.replace(/_/g,\" \"),\n                          \"action\": msg.payload.state,\n                            \"vid\": msg.payload.vid,\n                          \"value\": msg.payload.value\n               }\n]\nreturn msg;\n","outputs":1,"noerr":0,"x":910,"y":680,"wires":[["894f6e2d.049308"]]},{"id":"894f6e2d.049308","type":"alexa-remote-smarthome","z":"222893d1.979df4","name":"","account":"f4d129f8.797b8","config":{"option":"action","value":[]},"outputs":1,"x":1120,"y":680,"wires":[["b7cbb349.ea73e8"]]},{"id":"b7cbb349.ea73e8","type":"function","z":"222893d1.979df4","name":"create payload","func":"msg.payload = {\n    \"id\":\"variableset\",\n    \"DeviceNum\":msg.payload[0].vid,\n    \"serviceId\":\"urn:upnp-org:serviceId:SwitchPower1\",\n    \"Variable\":\"Status\",\n    \"Value\":((msg.code =\"SUCCESS\") ? msg.payload[0].value : 0)\n    }\nreturn msg;\n\n","outputs":1,"noerr":0,"x":1340,"y":680,"wires":[["41a7117.175f17"]]},{"id":"41a7117.175f17","type":"http request","z":"222893d1.979df4","name":"","method":"GET","ret":"txt","paytoqs":true,"url":"http://192.168.1.11:3480/data_request","tls":"","persist":false,"proxy":"","authType":"","x":1270,"y":600,"wires":[["adfffd39.89496"]]},{"id":"adfffd39.89496","type":"http response","z":"222893d1.979df4","name":"response","statusCode":"","headers":{"content-type":"application/json"},"x":1440,"y":600,"wires":[]},{"id":"f4d129f8.797b8","type":"alexa-remote-account","z":"","name":"alexaremote2","authMethod":"cookie","proxyOwnIp":"192.168.1.25","proxyPort":"3456","cookieFile":"","refreshInterval":"15","alexaServiceHost":"layla.amazon.co.uk","amazonPage":"amazon.co.uk","acceptLanguage":"en-UK","userAgent":"","useWsMqtt":"on","autoInit":"on"},{"id":"8c44a06e.fd78e","type":"ewelink-credentials","z":""}]

Setting up context data on Node-red:-
To store persistent data in the context data on node-red, you will need to edit the node-red settings.json.

Where to find settings.json Settings file : Node-RED

what setting needs changing Working with context : Node-RED

Ok, the inject node is there to simply trigger the storing of the data. Once you have edited the data in the function node(Global context set), just deploy the updated node and press the button on the inject node. A drop down widow will confirm the context data was deployed.

Example of context data

{"sonoff deviceid or alexa spoken phrase/s": ["vera deviceId, variable name", "vera serviceid for variable", "value to send need for non sonoff devices"]}

Sonoff deviceid’s can be found in the ewelink app.

You can enter multiple voice commands for same alexa device.

var ids = {
            //ewelink data
            "1000294563": [146, "Status", "urn:upnp-org:serviceId:SwitchPower1"],
            "10009f77d3": [153, "Status", "urn:upnp-org:serviceId:SwitchPower1"],
            "10008f88wb": [154, "Status", "urn:upnp-org:serviceId:SwitchPower1"],
            "10000dd881": [159, "Status", "urn:upnp-org:serviceId:SwitchPower1"],
            "1000a23d43": [160, "Status", "urn:upnp-org:serviceId:SwitchPower1"],
            "1000a45jh5": [161, "Status", "urn:upnp-org:serviceId:SwitchPower1"],
            "10000234e7": [169, "Status", "urn:upnp-org:serviceId:SwitchPower1"],
            "100007345b": [173, "Status", "urn:upnp-org:serviceId:SwitchPower1"],
            "100022jdd2": [178, "Tripped", "urn:micasaverde-com:serviceId:SecuritySensor1"],
            
            //alexa data
            //tv
            "turn_on_lounge_t_v": [171, "Status", "urn:upnp-org:serviceId:SwitchPower1", 1],
            "turn_off_lounge_t_v": [171, "Status", "urn:upnp-org:serviceId:SwitchPower1", 0],
            // fountain light
            "turn_on_water_light": [172, "Status", "urn:upnp-org:serviceId:SwitchPower1", 1],
            "turn_off_water_light": [172, "Status", "urn:upnp-org:serviceId:SwitchPower1", 0],
            "water_light_on": [172, "Status", "urn:upnp-org:serviceId:SwitchPower1", 1],
            "water_light_off": [172, "Status", "urn:upnp-org:serviceId:SwitchPower1", 0]
}
global.set("ids", ids)
return msg;

Virtual http switch plugin:-

You will need to install a Virtual http switch plugin device for each alexa connect device you wish to control.

You can control a routine with a http call to

http://[IP-of-node-red]:1880/routine?type=routine&device=fountain_light_on&value=1&vid=172

to turn on and off devices via routines you will need two routines, one for on and one for off.
device is the name of the routine.
vid is the vera deviceid.
value is 1 or 0.

A smart device
on

http://[IP-of-node-red]:1880/alexa?type=smarthome&device=Charger&state=turnOn&value=1&vid=146

off

http://[IP-of-node-red]:1880/alexa?type=smarthome&device=Charger&state=turnOff&value=0&vid=146

device is the name of the device in alexa or ewelink apps.
status is tunOn or turnOff
value 0 or 1
vid is vera deviceid

Vera device status updaters:-

You will need to enter you veras ip address in the http request node

Routine/smarthome flows:-

You will need to edit the http request node with you veras ip.

You will need to enter your alexa and ewelink user and password into the romote2 and ewelink config nodes.


Then go to

http://[IP-of-node-red]:3456

and enter amazon user and password

One last thing i use “computer” as my alexa wake word. If you use another you can edit here

Once this is all done deploy the full flow.

Hope you find this helpful. i will attempt to answer all question.

1 Like

Nice write-up! The more I hear about Node-RED, the more I suspect it’s becoming an indispensable tool for hard core HA enthusiasts.

So here’s my n00b question: Since Node-RED requires an “always on” platform (Windows, Raspberry Pi, etc.) to run it, what are the chances that one of the upcoming ezlo controllers could serve in that capacity??

After all, the internals we’ve seen on the ezlo Plus bear more than a passing resemblance to the components found on any decent Pi board, no? Makes me wonder if it could be deployed as a headless VM of sorts for this purpose.

That would be a dream HA controller, and the reason more proccesor and memory is always better. Node-red should run under linux, but i am no expert.

I have Node-red running in a RaspPi with HASS and MQTT. Note the word running, I haven’t had the time to get it to operate…

1 Like

I’m virtually certain I could have Node-RED running under Docker on a VM inside my Synology NAS on my LAN … but I don’t understand half the words I just said, so I’m not gonna try. :slight_smile:

1 Like

Stick a UZB on that with Zway, tie them all together, and you are living the dream.

I have a UZB, all it needs is a licence for Zway.
I’m holding out now for the release of the GEN7 Top Hat and I’ll give that a try.

Ezlo now has a Sonoff plugin!

3 Likes