Contol Sonoff with ESPurna from NR (New)

This flow shows how to control a Sonoff Basic running ESPurna firmware from Node Red.

For full details, see my Youtube video on this topic: https://youtu.be/EYeOX4J4yEk

[{"id":"1eceb1af.f728ee","type":"tab","label":"ESPurna","disabled":false,"info":""},{"id":"cf6e1c4e.3872f","type":"mqtt in","z":"1eceb1af.f728ee","name":"","topic":"/Sonoff2/#","qos":"0","broker":"cea5258a.b34038","x":120,"y":80,"wires":[["93260047.ae1ed"]]},{"id":"93260047.ae1ed","type":"debug","z":"1eceb1af.f728ee","name":"","active":true,"console":"false","complete":"false","x":350,"y":80,"wires":[]},{"id":"677fb0b0.c2b3f","type":"mqtt out","z":"1eceb1af.f728ee","name":"","topic":"/Sonoff2/relay/0/set","qos":"0","retain":"","broker":"cea5258a.b34038","x":350,"y":180,"wires":[]},{"id":"c39e1c90.fa4cc","type":"inject","z":"1eceb1af.f728ee","name":"","topic":"","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"x":130,"y":180,"wires":[["677fb0b0.c2b3f"]]},{"id":"54dca806.893e98","type":"inject","z":"1eceb1af.f728ee","name":"","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"x":130,"y":220,"wires":[["677fb0b0.c2b3f"]]},{"id":"4a861337.85625c","type":"inject","z":"1eceb1af.f728ee","name":"","topic":"","payload":"2","payloadType":"num","repeat":"","crontab":"","once":false,"x":130,"y":260,"wires":[["677fb0b0.c2b3f"]]},{"id":"c08cc8fe.292f48","type":"inject","z":"1eceb1af.f728ee","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":140,"y":360,"wires":[["cd526060.1930c"]]},{"id":"cd526060.1930c","type":"http request","z":"1eceb1af.f728ee","name":"Relay off","method":"GET","ret":"txt","url":"http://192.168.1.129/api/relay/0?apikey=9ADA0C102B8390D7&value=0","tls":"","x":340,"y":360,"wires":[[]]},{"id":"666eb832.90c2d8","type":"inject","z":"1eceb1af.f728ee","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":140,"y":400,"wires":[["af1fce4a.4a7b1"]]},{"id":"af1fce4a.4a7b1","type":"http request","z":"1eceb1af.f728ee","name":"Relay on","method":"GET","ret":"txt","url":"http://192.168.1.129/api/relay/0?apikey=9ADA0C102B8390D7&value=1","tls":"","x":340,"y":400,"wires":[[]]},{"id":"71ae71b4.3d053","type":"http request","z":"1eceb1af.f728ee","name":"Relay toggle","method":"GET","ret":"txt","url":"http://192.168.1.129/api/relay/0?apikey=9ADA0C102B8390D7&value=2","tls":"","x":350,"y":440,"wires":[[]]},{"id":"484de709.2d54e8","type":"inject","z":"1eceb1af.f728ee","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":140,"y":440,"wires":[["71ae71b4.3d053"]]},{"id":"3682c73b.b44b78","type":"http request","z":"1eceb1af.f728ee","name":"Status","method":"GET","ret":"txt","url":"http://192.168.1.129/api/relay/0?apikey=9ADA0C102B8390D7","tls":"","x":330,"y":500,"wires":[["4e091681.aa7e38"]]},{"id":"3f7b5690.059d0a","type":"inject","z":"1eceb1af.f728ee","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":140,"y":500,"wires":[["3682c73b.b44b78"]]},{"id":"4e091681.aa7e38","type":"debug","z":"1eceb1af.f728ee","name":"","active":true,"console":"false","complete":"false","x":530,"y":500,"wires":[]},{"id":"608c42a0.37622c","type":"comment","z":"1eceb1af.f728ee","name":"Subscribe to all messages","info":"This is just to subscribe to all messages \ncoming from ESPurna. You can turn this off\nonce you are completed the setup.\n\nHere I put the same topic ID as what I entered\non the UI and # at the end.","x":130,"y":40,"wires":[]},{"id":"c245bbb8.55cef8","type":"comment","z":"1eceb1af.f728ee","name":"MQTT control","info":"Controlling the relay via MQTT\n\nroot topic followed by relay/0/set\naccepted messages:\n0: off\n1: on\n2: toggle","x":90,"y":140,"wires":[]},{"id":"e96b6eda.d629d","type":"comment","z":"1eceb1af.f728ee","name":"HTTP control","info":"Controlling the relay via HTTP\nHTTP API in the admin page need to be turned off\nCopy the api key from the admin page\n\nURL http://<ip>/api/relay/0?apikey=<api key>&value=0\naccepted values (at the end):\n0: off\n1: on\n2: toggle","x":90,"y":320,"wires":[]},{"id":"bbdaab8e.c881b8","type":"comment","z":"1eceb1af.f728ee","name":"Control from UI","info":"This section shows a simple dashboard UI to \ncontrol and display the status of the unit.\n\nI use MQTT to control it, but it could also be\nHTTP as well.","x":100,"y":560,"wires":[]},{"id":"573a5e8b.7b9fe","type":"ui_switch","z":"1eceb1af.f728ee","name":"","label":"Switch relay","group":"a6b635bf.d985d8","order":1,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"1","onvalueType":"str","onicon":"","oncolor":"","offvalue":"0","offvalueType":"str","officon":"","offcolor":"","x":130,"y":600,"wires":[["f0112257.fbdc7"]]},{"id":"f0112257.fbdc7","type":"mqtt out","z":"1eceb1af.f728ee","name":"","topic":"/Sonoff2/relay/0/set","qos":"0","retain":"","broker":"cea5258a.b34038","x":510,"y":600,"wires":[]},{"id":"e0ee7812.3ad8e8","type":"ui_button","z":"1eceb1af.f728ee","name":"","group":"a6b635bf.d985d8","order":2,"width":0,"height":0,"passthru":false,"label":"Toggle relay","color":"","bgcolor":"","icon":"","payload":"2","payloadType":"str","topic":"","x":130,"y":640,"wires":[["f0112257.fbdc7"]]},{"id":"aa2e57b5.8374e8","type":"mqtt in","z":"1eceb1af.f728ee","name":"","topic":"/Sonoff2/uptime","qos":"0","broker":"cea5258a.b34038","x":140,"y":840,"wires":[["12e25fc8.48974"]]},{"id":"ac89355d.ec8158","type":"mqtt in","z":"1eceb1af.f728ee","name":"","topic":"/Sonoff2/rssi","qos":"0","broker":"cea5258a.b34038","x":130,"y":900,"wires":[["2570dcf3.9deed4","136faa92.d4de35"]]},{"id":"136faa92.d4de35","type":"function","z":"1eceb1af.f728ee","name":"Store last update","func":"var devicename = \"Sonoff2\";\n\nvar temp = global.get(devicename+\"_wifi\");\nvar current = new Date();\nif (temp!== undefined && temp!==null) {\n    msg.payload = current.getTime() - temp;\n    global.set(devicename+\"_wifi\",current.getTime());\n} else {\n    msg.payload = \"\";\n    global.set(devicename+\"_wifi\",current.getTime());\n}\n\n// Update the status with current timestamp\nvar now = new Date();\nvar yyyy = now.getFullYear();\nvar mm = now.getMonth() < 9 ? \"0\" + (now.getMonth() + 1) : (now.getMonth() + 1); // getMonth() is zero-based\nvar dd  = now.getDate() < 10 ? \"0\" + now.getDate() : now.getDate();\nvar hh = now.getHours() < 10 ? \"0\" + now.getHours() : now.getHours();\nvar mmm  = now.getMinutes() < 10 ? \"0\" + now.getMinutes() : now.getMinutes();\nvar ss  = now.getSeconds() < 10 ? \"0\" + now.getSeconds() : now.getSeconds();\nnode.status({fill:\"blue\",shape:\"ring\",text:\"Last update: \"+dd + \".\" + mm + \".\" + yyyy + \" \" + hh + \":\" + mmm + \":\" + ss});\n\nmsg.formattedtime = dd + \".\" + mm + \".\" + yyyy + \" \" + hh + \":\" + mmm + \":\" + ss;\n\nreturn msg;","outputs":1,"noerr":0,"x":390,"y":940,"wires":[[]]},{"id":"903d2cab.d5ff7","type":"inject","z":"1eceb1af.f728ee","name":"","topic":"","payload":"","payloadType":"date","repeat":"1","crontab":"","once":false,"x":150,"y":1000,"wires":[["3630b8e8.a77cd8"]]},{"id":"3630b8e8.a77cd8","type":"function","z":"1eceb1af.f728ee","name":"From last update","func":"var devicename = \"Sonoff2\";\n\nvar temp = global.get(devicename+\"_wifi\");\nvar current = new Date();\nmsg.payload = \"No data\";\nmsg.warning = false;\n\nif (temp!==undefined) {\n    current = current - temp;\n    current = Math.floor(current/1000);\n    var minute = Math.floor(current/60);\n    var hour = Math.floor(minute/60);\n    var day = Math.floor(hour/24);\n    if (current>24*60*60) {\n        msg.payload = \"Last update \" + day + \" days, \" + hour%24 + \" hours, \" + minute%60 + \" minutes, \" + current%60 + \" seconds ago\";\n    } else if (current>60*60) {\n        msg.payload = \"Last update \" + hour%24 + \" hours, \" + minute%60 + \" minutes, \" + current%60 + \" seconds ago\";\n    } else if (current>60) {\n        msg.payload = \"Last update \" + minute%60 + \" minutes, \" + current%60 + \" seconds ago\";\n    } else {\n        msg.payload = \"Last update \" + current%60 + \" seconds ago\";\n    }\n\n}\n\nnode.status({fill:\"blue\",shape:\"ring\",text:msg.payload});\n\n\nreturn msg;","outputs":1,"noerr":0,"x":390,"y":1000,"wires":[["31d57e0f.fed162"]]},{"id":"12e25fc8.48974","type":"ui_text","z":"1eceb1af.f728ee","group":"a6b635bf.d985d8","order":4,"width":0,"height":0,"name":"","label":"Uptime","format":"{{msg.payload}}","layout":"row-spread","x":600,"y":840,"wires":[]},{"id":"31d57e0f.fed162","type":"ui_text","z":"1eceb1af.f728ee","group":"a6b635bf.d985d8","order":9,"width":0,"height":0,"name":"Last Update","label":"","format":"{{msg.payload}}","layout":"row-spread","x":610,"y":1000,"wires":[]},{"id":"2570dcf3.9deed4","type":"ui_gauge","z":"1eceb1af.f728ee","name":"Wifi gauge","group":"a6b635bf.d985d8","order":8,"width":0,"height":0,"gtype":"gage","title":"","label":"","format":"{{value}}","min":"-100","max":"-50","colors":["#ff0000","#e6e600","#00b500"],"seg1":"","seg2":"","x":610,"y":900,"wires":[]},{"id":"17d81a82.7f2925","type":"comment","z":"1eceb1af.f728ee","name":"Diagnostic","info":"This section just displays some of the update\nmessages being sent by the ESPurna.\nThe two function node are used to calculate the \nlast update. If you are using multiple devices\nchange the devicename variable in the first line\nin both function nodes to the same (any text)","x":80,"y":780,"wires":[]},{"id":"21ac1447.a1409c","type":"mqtt in","z":"1eceb1af.f728ee","name":"","topic":"/Sonoff2/version","qos":"0","broker":"cea5258a.b34038","x":140,"y":1060,"wires":[["d7944ba8.99e998"]]},{"id":"d7944ba8.99e998","type":"ui_text","z":"1eceb1af.f728ee","group":"a6b635bf.d985d8","order":5,"width":0,"height":0,"name":"","label":"Version","format":"{{msg.payload}}","layout":"row-spread","x":600,"y":1060,"wires":[]},{"id":"e90d88a6.e1f2d8","type":"mqtt in","z":"1eceb1af.f728ee","name":"","topic":"/Sonoff2/host","qos":"0","broker":"cea5258a.b34038","x":130,"y":1120,"wires":[["955a53a0.417fb"]]},{"id":"955a53a0.417fb","type":"ui_text","z":"1eceb1af.f728ee","group":"a6b635bf.d985d8","order":6,"width":0,"height":0,"name":"","label":"Host","format":"{{msg.payload}}","layout":"row-spread","x":590,"y":1120,"wires":[]},{"id":"845b0ef7.27c74","type":"mqtt in","z":"1eceb1af.f728ee","name":"","topic":"/Sonoff2/ip","qos":"0","broker":"cea5258a.b34038","x":120,"y":1180,"wires":[["7bebc278.4010cc"]]},{"id":"7bebc278.4010cc","type":"ui_text","z":"1eceb1af.f728ee","group":"a6b635bf.d985d8","order":7,"width":0,"height":0,"name":"","label":"IP address","format":"{{msg.payload}}","layout":"row-spread","x":610,"y":1180,"wires":[]},{"id":"f7b1951b.0522e8","type":"ui_template","z":"1eceb1af.f728ee","group":"a6b635bf.d985d8","name":"Relay state","order":3,"width":"","height":"","format":"<div layout=\"row\" layout-align=\"space-between\">\n    <p>Relay state</p>\n    <p ng-style=\"{color: msg.payload === '1' ? 'blue' : 'grey'}\">\n        <b>{{msg.payload === '1' ? 'ON' : 'OFF'}}</b>\n    </p>\n</div>","storeOutMessages":false,"fwdInMessages":false,"templateScope":"local","x":490,"y":700,"wires":[[]]},{"id":"9972e556.31b988","type":"mqtt in","z":"1eceb1af.f728ee","name":"","topic":"/Sonoff2/relay/0","qos":"0","broker":"cea5258a.b34038","x":140,"y":700,"wires":[["f7b1951b.0522e8"]]},{"id":"cea5258a.b34038","type":"mqtt-broker","z":"","broker":"192.168.1.80","port":"1883","clientid":"node-red","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""},{"id":"a6b635bf.d985d8","type":"ui_group","z":"","name":"Sonoff Basic","tab":"8e327c7c.cb05b","disp":true,"width":"6"},{"id":"8e327c7c.cb05b","type":"ui_tab","z":"","name":"ESPurna","icon":"dashboard"}]

Flow Info

Created 6 years, 10 months ago
Rating: not yet rated

Owner

Actions

Rate:

Node Types

Core
  • comment (x5)
  • debug (x2)
  • function (x2)
  • http request (x4)
  • inject (x8)
  • mqtt in (x7)
  • mqtt out (x2)
  • mqtt-broker (x1)
Other
  • tab (x1)
  • ui_button (x1)
  • ui_gauge (x1)
  • ui_group (x1)
  • ui_switch (x1)
  • ui_tab (x1)
  • ui_template (x1)
  • ui_text (x5)

Tags

  • espurna
  • sonoff
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option