Flow to get output from a logical sequence of events

That was also what I came up with. Here's my take:

[{"id":"38412910c32a18b1","type":"tab","label":"Test Logic","disabled":false,"info":"","env":[]},{"id":"43d0d99b5d2f0d3d","type":"function","z":"38412910c32a18b1","name":"function 20","func":"let msg1 = {}\nlet msg2 = {}\nlet step = context.get('step') || 0\nlet output = context.get('output') || 0\nlet D1 = context.get('D1') || false\nlet D2 = context.get('D2') || false\n\nlet invar = msg.payload\nlet topic = msg.topic\nif(topic === 'D1'){D1 = invar}\nif(topic === 'D2'){D2 = invar}\n\nswitch (step) {\n    case 0:\n        if(D1 && D2) {\n            step = 1\n            output = 1\n        }\n        break\n    case 1:\n        if (!D1 && !D2) {\n        step = 0\n        output = 0\n        }\n        break\n    default:\n        step = 0\n}\nmsg1.payload = step\nmsg2.payload = output\nnode.send([msg1, msg2]);\n\ncontext.set('step', step)\ncontext.set('output', output)\ncontext.set('D1', D1)\ncontext.set('D2', D2)\n","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":490,"y":200,"wires":[["81c4ba6731e62855"],["4679d13c2f22cdea"]]},{"id":"445a0cf0c46fd714","type":"ui_switch","z":"38412910c32a18b1","name":"D1","label":"D1","tooltip":"","group":"edde913e86badb5a","order":3,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"topic","topicType":"msg","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","animate":false,"x":90,"y":160,"wires":[["8e71b1311a204d9f"]]},{"id":"965edb6a4621c41c","type":"ui_switch","z":"38412910c32a18b1","name":"","label":"D2","tooltip":"","group":"edde913e86badb5a","order":4,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"topic","topicType":"msg","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","animate":false,"x":90,"y":240,"wires":[["5545be1b77608ba3"]]},{"id":"81c4ba6731e62855","type":"ui_text","z":"38412910c32a18b1","group":"edde913e86badb5a","order":2,"width":0,"height":0,"name":"","label":"Step","format":"{{msg.payload}}","layout":"row-spread","x":690,"y":160,"wires":[]},{"id":"4679d13c2f22cdea","type":"ui_gauge","z":"38412910c32a18b1","name":"","group":"edde913e86badb5a","order":1,"width":0,"height":0,"gtype":"gage","title":"Output","label":"units","format":"{{value}}","min":0,"max":"1","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":690,"y":240,"wires":[]},{"id":"8e71b1311a204d9f","type":"function","z":"38412910c32a18b1","name":"function 21","func":"msg.topic = 'D1'\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":250,"y":160,"wires":[["43d0d99b5d2f0d3d"]]},{"id":"5545be1b77608ba3","type":"function","z":"38412910c32a18b1","name":"function 22","func":"msg.topic = 'D2'\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":250,"y":240,"wires":[["43d0d99b5d2f0d3d"]]},{"id":"2730b91a132b3ffe","type":"comment","z":"38412910c32a18b1","name":"dashboard-evi","info":"","x":470,"y":320,"wires":[]},{"id":"edde913e86badb5a","type":"ui_group","name":"Test","tab":"36910c33c3a088cb","order":8,"disp":true,"width":"6","collapse":false},{"id":"36910c33c3a088cb","type":"ui_tab","name":"Test Logic","icon":"dashboard","order":3,"disabled":false,"hidden":false}]
1 Like