platform: PiZero W also running MQTT
SonOff S20 switch running ESPeasy mega-20181025
NR: v0.19.4
Node.js: v8.12.0
dashboard: v2.10.1
Extra node(s):
- node-red-dashboard
- node-red-contrib-mytimeout
- node-red-contrib-alexa-local (optional)
Goal: Shut off a coffee pot 20 minutes after it is turned on and allow it to be turned on/off remotely or by voice (Alexa)
The story: I make coffee for myself and sometimes (ok, enough times to make me come up with this solution) I forget to shut off the pot after pouring the last (or most of the last) of the coffee. When I come back several hours later, the bottom of the pot (glass pot) has coffee baked on to it and I have to scrub it off.
So I got a SonOff S20, flashed it with ESPeasy and configured it to use MQTT, defined the device as a switch, and added a rule (cnfiguration attached).
In Node-RED, I put in a dashboard button (using fa-coffee) colored green when on and red when off and a donut graph to show the state - green on, grey off and I added a count down timer:
Every thing works great, if I setup the coffee the night before, I can wake up, grab my phone and link to the dashboard, click on the coffee cup and by the time I get downstairs, my coffee is ready. And if I forget to shut off the pot, the countdown timer will shut it off for me.
I even added a small extra flow so you can have Alexa turn it on or off with voice commands!
Here is the flow:
[{"id":"6c29b57a.3e9e6c","type":"tab","label":"Coffee Pot","disabled":false,"info":""},{"id":"4de47888.28887","type":"tab","label":"CP - monitor S20","disabled":false,"info":""},{"id":"2cafff00.623622","type":"tab","label":"CP - countdown","disabled":false,"info":""},{"id":"b487bcf.89c704","type":"tab","label":"CP - Alexa code","disabled":false,"info":""},{"id":"c3a390f2.e60ef8","type":"mqtt-broker","z":"","name":"","broker":"192.168.48.100","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","closeTopic":"","closeQos":"0","closeRetain":"false","closePayload":"","willTopic":"","willQos":"0","willRetain":"false","willPayload":""},{"id":"cd1729e6.702738","type":"ui_group","z":"","name":"Coffee Pot","tab":"f973b669.331fa","order":2,"disp":false,"width":"6","collapse":false},{"id":"f973b669.331fa","type":"ui_tab","z":"","name":"Coffee Pot","icon":"dashboard","order":1},{"id":"9a726505.83a4b","type":"mqtt-broker","z":"","name":"","broker":"192.168.48.100","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","closeTopic":"","closeQos":"0","closeRetain":"false","closePayload":"","willTopic":"","willQos":"0","willRetain":"false","willPayload":""},{"id":"6dc3c9c2.d9fed8","type":"ui_group","z":"","name":"Coffee Pot","tab":"99d668e.48e2918","order":2,"disp":false,"width":"6","collapse":false},{"id":"99d668e.48e2918","type":"ui_tab","z":"","name":"Coffee Pot","icon":"dashboard","order":1},{"id":"13ed15eb.4bda0a","type":"ui_group","z":"","name":"Coffee Pot","tab":"2019b454.21de84","order":2,"disp":false,"width":"6","collapse":false},{"id":"2019b454.21de84","type":"ui_tab","z":"","name":"Coffee Pot","icon":"dashboard","order":1},{"id":"41088cb9.4f7424","type":"mqtt in","z":"6c29b57a.3e9e6c","name":"Monitor all messages","topic":"coffee30/#","qos":"2","broker":"c3a390f2.e60ef8","x":440,"y":400,"wires":[["7c0b3cd1.a3fc74"]]},{"id":"7c0b3cd1.a3fc74","type":"debug","z":"6c29b57a.3e9e6c","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":630,"y":400,"wires":[]},{"id":"a1456196.d699d","type":"mqtt out","z":"6c29b57a.3e9e6c","name":"Send 1/0 to the switch","topic":"","qos":"","retain":"","broker":"c3a390f2.e60ef8","x":680,"y":220,"wires":[]},{"id":"8b510e6f.bbb6b8","type":"comment","z":"6c29b57a.3e9e6c","name":"Flows to turn a SonOff S20 on/off via dashboard, Alexa, inject nodes or by manually pressing the switch. Turns it off after 20 minutes","info":"These flows allow various ways to turn on/off a SonOff S20 that has a coffee pot \nplugged into it. You can use the dashboard, you can manually press the switch's \nbutton or tell Alexa \"Alexa, turn the coffee pot on\". (You could easilly add other\nmethods if needed.) In all situations, it will automatically turn the pot off after \ntwenty (20) minutes (or the value you set in the editor.)\n\nThe flow create a dashboard with an on/off switch (a coffee cup icon), a donut chart \nthat changes from red to green and a countdown timer showing how long before the \nswitch will be turned off. You can change the amount of time by changing the value \nin the insert node.\n\nIf the switch is manually pressed, it will also start the countdown timer and shut \nthe plug off after the defined time limit. ","x":480,"y":40,"wires":[]},{"id":"1cbbdee8.36f261","type":"change","z":"6c29b57a.3e9e6c","name":"Setup switch topic","rules":[{"t":"set","p":"topic","pt":"msg","to":"coffee30/gpio/12","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":390,"y":220,"wires":[["a1456196.d699d","20d6f55.06afa8a"]]},{"id":"ca372d2d.479b78","type":"ui_switch","z":"6c29b57a.3e9e6c","name":"ON/OFF","label":"","group":"cd1729e6.702738","order":2,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"1","onvalueType":"num","onicon":"fa-coffee fa-5x","oncolor":"#00B500","offvalue":"0","offvalueType":"num","officon":"fa-coffee fa-5x","offcolor":"#CA3838","x":180,"y":220,"wires":[["1cbbdee8.36f261","9cf1c376.81537"]]},{"id":"20d6f55.06afa8a","type":"debug","z":"6c29b57a.3e9e6c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":370,"y":260,"wires":[]},{"id":"9e3c5771.e4cd98","type":"inject","z":"6c29b57a.3e9e6c","name":"manual on","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":360,"wires":[["ca372d2d.479b78"]]},{"id":"a53ae132.1387d8","type":"inject","z":"6c29b57a.3e9e6c","name":"manual off","topic":"","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":400,"wires":[["ca372d2d.479b78"]]},{"id":"2e8414f7.9b3454","type":"link in","z":"6c29b57a.3e9e6c","name":"on/off","links":["62f7139.fe0aa6c","e5273161.235928"],"x":55,"y":220,"wires":[["ca372d2d.479b78"]]},{"id":"414ace38.ce8618","type":"ui_text","z":"6c29b57a.3e9e6c","group":"cd1729e6.702738","order":1,"width":"6","height":"1","name":"filler - push ON/OFF down in dashboard","label":"","format":"{{msg.payload}}","layout":"row-spread","x":760,"y":320,"wires":[]},{"id":"8f5b6fef.68d02","type":"inject","z":"6c29b57a.3e9e6c","name":"Send 1200 to the timer","topic":"","payload":"1200","payloadType":"num","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":170,"y":120,"wires":[["2ad7dc36.2c3cdc"]]},{"id":"2ad7dc36.2c3cdc","type":"change","z":"6c29b57a.3e9e6c","name":"","rules":[{"t":"set","p":"coffeePotTimer","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":410,"y":120,"wires":[[]]},{"id":"727d4cf5.d52424","type":"comment","z":"6c29b57a.3e9e6c","name":"initilize the countdown timer to 20 minutes (60 seconds times 20 minutes = 1200 seconds)","info":"","x":350,"y":80,"wires":[]},{"id":"2b8983fc.8ed43c","type":"comment","z":"6c29b57a.3e9e6c","name":"Process on/off (1/0) and send it to the switch using the topic 'coffee30/gpio/12'","info":"","x":420,"y":180,"wires":[]},{"id":"36d3260f.04415a","type":"comment","z":"6c29b57a.3e9e6c","name":"Manual on/off for testing","info":"","x":150,"y":320,"wires":[]},{"id":"9cf1c376.81537","type":"change","z":"6c29b57a.3e9e6c","name":"","rules":[{"t":"set","p":"buttonState","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":320,"wires":[["2e1d7f12.627fe"]]},{"id":"2e1d7f12.627fe","type":"debug","z":"6c29b57a.3e9e6c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":450,"y":360,"wires":[]},{"id":"bdd9127d.92c99","type":"mqtt in","z":"4de47888.28887","name":"Coffee state: on/off?","topic":"coffee30/coffeeState/Switch","qos":"2","broker":"9a726505.83a4b","x":130,"y":80,"wires":[["e16ed6d2.80eb48","98c1ed4c.28211"]]},{"id":"e0b3b3d7.9af48","type":"debug","z":"4de47888.28887","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":590,"y":140,"wires":[]},{"id":"e16ed6d2.80eb48","type":"switch","z":"4de47888.28887","name":"check on or off","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"str"},{"t":"eq","v":"0","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":140,"y":140,"wires":[["242b1d64.e7f9c2"],["dfd6d68a.6f976"]]},{"id":"dfd6d68a.6f976","type":"change","z":"4de47888.28887","name":"if OFF, reset DELAY","rules":[{"t":"set","p":"payload","pt":"msg","to":"off","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":260,"wires":[["d560c1e9.9dd698","8fbeb053.569c5"]]},{"id":"98c1ed4c.28211","type":"ui_gauge","z":"4de47888.28887","name":"Coffee Pot Status","group":"6dc3c9c2.d9fed8","order":3,"width":0,"height":0,"gtype":"donut","title":"Coffee Pot Status","label":"","format":"{{value}}","min":"0","max":"1","colors":["#00B500","#00B500","#00B500"],"seg1":"","seg2":"","x":390,"y":80,"wires":[]},{"id":"f8fd4cc5.671fe8","type":"comment","z":"4de47888.28887","name":"This code monitors the status of the SonOff switch (the 'Pot') and sets or resets the delay","info":"","x":350,"y":40,"wires":[]},{"id":"81c8fa85.0e05d","type":"debug","z":"4de47888.28887","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":590,"y":200,"wires":[]},{"id":"242b1d64.e7f9c2","type":"change","z":"4de47888.28887","name":"if ON build DELAY json","rules":[{"t":"set","p":"coffeePotTimer","pt":"msg","to":"coffeePotTimer","tot":"global"},{"t":"set","p":"payload","pt":"msg","to":"{\t \"payload\": \"on\",\t \"timeout\": coffeePotTimer,\t \"warning\": 3\t}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":140,"wires":[["d560c1e9.9dd698","e0b3b3d7.9af48"]]},{"id":"e31228bb.2f8b4","type":"debug","z":"4de47888.28887","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":590,"y":320,"wires":[]},{"id":"f26c688e.16755","type":"link out","z":"4de47888.28887","name":"to timer","links":["11532c7f.408924"],"x":735,"y":320,"wires":[]},{"id":"8fbeb053.569c5","type":"debug","z":"4de47888.28887","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":190,"y":360,"wires":[]},{"id":"d5643832.dc186","type":"comment","z":"4de47888.28887","name":"Link to countdown timer","info":"","x":870,"y":320,"wires":[]},{"id":"d560c1e9.9dd698","type":"mytimeout","z":"4de47888.28887","name":"Coffee OFF delay","outtopic":"","outsafe":"1","outwarning":"","outunsafe":"0","warning":"5","timer":"4","debug":false,"ndebug":false,"ignoreCase":false,"repeat":false,"again":false,"x":590,"y":240,"wires":[["81c8fa85.0e05d","2f5efc86.2e8464"],["e31228bb.2f8b4","f26c688e.16755"]]},{"id":"6838d540.83973c","type":"link out","z":"4de47888.28887","name":"To on/off","links":["d2d55e9f.41c84"],"x":795,"y":260,"wires":[]},{"id":"30092471.27f35c","type":"comment","z":"4de47888.28887","name":"need this since timer returns strings","info":"","x":900,"y":160,"wires":[]},{"id":"287ac60d.fb413a","type":"comment","z":"4de47888.28887","name":"Link to Coffee Pot","info":"","x":910,"y":260,"wires":[]},{"id":"2f5efc86.2e8464","type":"change","z":"4de47888.28887","name":"convert literal number to numeric ","rules":[{"t":"set","p":"payload","pt":"msg","to":"$number(payload)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":900,"y":200,"wires":[["6838d540.83973c"]]},{"id":"a64bf2b1.bae428","type":"change","z":"2cafff00.623622","name":"change to hh:ss","rules":[{"t":"set","p":"payload","pt":"msg","to":"(payload-(payload%60))/60 & \":\" & $pad($string(payload%60),-2,\"0\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":260,"y":120,"wires":[["46327c49.6a3ae4"]]},{"id":"aea3a5fb.21606","type":"link in","z":"2cafff00.623622","name":"timer","links":["f2c3430.c53094"],"x":115,"y":120,"wires":[["a64bf2b1.bae428"]]},{"id":"4fbdbd02.131c74","type":"comment","z":"2cafff00.623622","name":"Displays countdown till pot turns off","info":"","x":240,"y":80,"wires":[]},{"id":"46327c49.6a3ae4","type":"ui_text","z":"2cafff00.623622","group":"13ed15eb.4bda0a","order":4,"width":0,"height":0,"name":"","label":"Time till auto shutoff","format":"{{msg.payload}}","layout":"col-center","x":480,"y":120,"wires":[]},{"id":"b05f3a86.70683","type":"debug","z":"b487bcf.89c704","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":130,"y":200,"wires":[]},{"id":"e66810dd.f9b4f","type":"link out","z":"b487bcf.89c704","name":"To on/off","links":["d2d55e9f.41c84"],"x":655,"y":140,"wires":[]},{"id":"791bf4f5.f8159c","type":"change","z":"b487bcf.89c704","name":"1","rules":[{"t":"set","p":"payload","pt":"msg","to":"1","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":490,"y":120,"wires":[["e66810dd.f9b4f"]]},{"id":"222970b9.bfcbf","type":"switch","z":"b487bcf.89c704","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":330,"y":140,"wires":[["791bf4f5.f8159c"],["10992e17.451aaa"]]},{"id":"10992e17.451aaa","type":"change","z":"b487bcf.89c704","name":"0","rules":[{"t":"set","p":"payload","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":490,"y":160,"wires":[["e66810dd.f9b4f"]]},{"id":"51ca5e06.a6026","type":"comment","z":"b487bcf.89c704","name":"Code to allow Alexa to turn the pot on/off","info":"","x":260,"y":60,"wires":[]},{"id":"f4445c86.044008","type":"comment","z":"b487bcf.89c704","name":"Link to Coffee Pot","info":"","x":770,"y":140,"wires":[]},{"id":"eb3c84a.3cbebf8","type":"alexa-local","z":"b487bcf.89c704","devicename":"Coffee Pot","inputtrigger":false,"x":150,"y":140,"wires":[["b05f3a86.70683","222970b9.bfcbf"]]}]
Here are images of SonOff configuration:
NOTE: for device 1 and 2 bottom of the page was cutoff - the name filed is 'switch' for both
NOTE: in order to see the rules menu, you must go to Tools->Advanced and check off the 'Rules' box
Any questions feel free to ask.
Paul