Ok, I have written this subflow.
It listens to MQTT on a couple of topics.
The idea is it receives a message telling it the frequency at which it functions.
Say 20 seconds.
It has an inbuilt setting to set it to 30 seconds if noting else is received.
I have deployed it on 2 machines and it works.
It is only working at 20 second.
The global
is set to 20000
.
I now put it on the main machine. I call it main, but at that part it is exactly the same as other machines. Just the code that adjusts the global
is on this machine.
I had to add an extra function
node to convert the message to a number as MQTT
doesn't like sending numbers.
So the sub-flow code.
[{"id":"481914cc.775324","type":"subflow","name":"Heartbeat","info":"","category":"","in":[{"x":90,"y":330,"wires":[{"id":"ec2e9708.9a6b5"}]}],"out":[{"x":1120,"y":220,"wires":[{"id":"334b4b03.30d5bc","port":0}]},{"x":1120,"y":270,"wires":[{"id":"add64ced.cd78f8","port":0}]},{"x":1120,"y":320,"wires":[{"id":"2b3acd1c.d30bfa","port":0}]}],"env":[],"color":"#3FADB5","outputLabels":["Signal","Pulse","Frequency"],"icon":"font-awesome/fa-heartbeat","status":{"x":1120,"y":370,"wires":[{"id":"949ea6cb.d81748","port":0}]}},{"id":"ec2e9708.9a6b5","type":"switch","z":"481914cc.775324","name":"","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"ALL/SCAN_FREQUENCY","vt":"str"},{"t":"eq","v":"RESET","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":200,"y":330,"wires":[["234b3f57.731a7"],["ad23cbbf.f206a8"]]},{"id":"234b3f57.731a7","type":"change","z":"481914cc.775324","name":"","rules":[{"t":"set","p":"interval","pt":"msg","to":"payload","tot":"msg"},{"t":"set","p":"topic","pt":"msg","to":"interval","tot":"str"},{"t":"set","p":"SCAN_TIME","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":430,"y":300,"wires":[["f39c5358.61c5c"]]},{"id":"f39c5358.61c5c","type":"function","z":"481914cc.775324","name":"Minimum","func":"x = msg.payload;\nif (x >4999)\n{\n // All good\n node.status({fill:\"green\",shape:\"dot\",text:\"Good\"});\n} else\n{\n // Too small.\n node.status({fill:\"red\",shape:\"dot\",text:\"Too small\"});\n x = 5000;\n}\nmsg.payload = x;\nreturn msg;","outputs":1,"noerr":0,"x":450,"y":260,"wires":[["856f574e.d68b58"]]},{"id":"856f574e.d68b58","type":"function","z":"481914cc.775324","name":"Variable repeat time","func":"if (msg.topic === \"interval\") {\n context.set(\"interval\", msg.payload);\n msg = null;\n} else {\n var interval = context.get(\"interval\");\n if (interval) msg.delay = interval;\n}\nreturn msg;","outputs":1,"noerr":0,"x":650,"y":260,"wires":[["58acd068.09e108"]]},{"id":"e0a14b00.511f48","type":"inject","z":"481914cc.775324","name":"Default","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"30","payloadType":"num","x":370,"y":220,"wires":[["b81a570d.b8e1c8"]]},{"id":"b81a570d.b8e1c8","type":"function","z":"481914cc.775324","name":"Convert to milliseconds","func":"var x = msg.payload;\nvar y = x * 1000;\nmsg = {delay: y, payload: x};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":495,"y":220,"wires":[["58acd068.09e108"]],"l":false},{"id":"58acd068.09e108","type":"delay","z":"481914cc.775324","name":"Variable delay","pauseType":"delayv","timeout":"10","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":670,"y":220,"wires":[["2b3acd1c.d30bfa","ac5bea96.b6ef7","856f574e.d68b58"]]},{"id":"2b3acd1c.d30bfa","type":"function","z":"481914cc.775324","name":"convert to seconds","func":"// var a = parseInt(\"10\")\n//var z = parseInt(msg.payload);\nvar z = msg.delay;\n//node.warn(z);\nvar x = z / 1000;\nmsg.payload = x;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1015,"y":300,"wires":[[]],"l":false},{"id":"ac5bea96.b6ef7","type":"function","z":"481914cc.775324","name":"ENABLE","func":"if (msg.topic === \"RESET\")\n{\n // Detect what the ENABLE command is.\n if (msg.payload === 'ACTIVATE')\n {\n // If it is \"true\" indicate it is enabled and set 'enable'\n node.status({fill:\"yellow\",shape:\"dot\",text:\"ENABLED\"});\n context.set('enable',1);\n }\n //else\n if (msg.payload === 'DEACTIVATE')\n {\n // Otherwise mark as disabled and reset 'enable'\n context.set('enable',0);\n node.status({fill:\"red\",shape:\"dot\",text:\"DISABLED\"});\n }\n}\n// Now act of message which is not the \"ENABLE\" command.\nif (msg.topic != \"ENABLE\")\n{\n // Check if 'enable' is set to 1\n if (context.get('enable') == 1)\n {\n // Pass message through\n node.status({fill:\"green\",shape:\"dot\",text:\"ENABLED\"});\n return msg;\n }\n}\n","outputs":1,"noerr":0,"x":900,"y":220,"wires":[["334b4b03.30d5bc","635d723c.2e5fe4"]]},{"id":"334b4b03.30d5bc","type":"change","z":"481914cc.775324","name":"Heartbeat","rules":[{"t":"set","p":"payload","pt":"msg","to":"HEARTBEAT","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1015,"y":220,"wires":[[]],"l":false},{"id":"635d723c.2e5fe4","type":"trigger","z":"481914cc.775324","name":"Signal indicator","op1":"1","op2":"0","op1type":"str","op2type":"str","duration":"2","extend":false,"units":"s","reset":"","bytopic":"all","outputs":1,"x":880,"y":260,"wires":[["add64ced.cd78f8"]]},{"id":"add64ced.cd78f8","type":"function","z":"481914cc.775324","name":"Make icons for heart beat","func":"//msg1 = {payload: '<font color = \\\"red\\\"><i class=\"fa fa-bullseye fa-2x\"></i></font>', topic:device + \"/Off-line\",device_ID:device, background:\"brown\"};\nif (msg.payload == 0)\n{\n msg = {payload: '<font color = #333333><i class=\"fa fa-heart fa-2x\"></i></font>'};\n} else\nif (msg.payload == 1)\n{\n msg = {payload: '<font color = \\\"lime\\\"><i class=\"fa fa-heartbeat fa-2x\"></i></font>'};\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1015,"y":260,"wires":[[]],"l":false},{"id":"ad23cbbf.f206a8","type":"fan","z":"481914cc.775324","name":"","x":735,"y":330,"wires":[["ac5bea96.b6ef7"]],"l":false},{"id":"949ea6cb.d81748","type":"status","z":"481914cc.775324","name":"","scope":["ac5bea96.b6ef7"],"x":910,"y":370,"wires":[[]]},{"id":"276ed772.2093c","type":"mqtt in","z":"675e227d.d158b4","name":"Scan time","topic":"ALL/SCAN_FREQUENCY","qos":"2","datatype":"auto","broker":"bbf26a6c.b7922","x":110,"y":220,"wires":[["daea214b.d93e78"]]},{"id":"daea214b.d93e78","type":"function","z":"675e227d.d158b4","name":"","func":"msg.payload = parseInt(msg.payload);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":275,"y":220,"wires":[["2fd5cce9.ddfff4"]],"l":false},{"id":"2fd5cce9.ddfff4","type":"subflow:481914cc.775324","z":"675e227d.d158b4","name":"","x":390,"y":270,"wires":[["ba769d38.a08f28","2be90578.5c4632","eee0a218.99e3e","70b917c9.b5ffc"],["e255f2c0.c9fa68"],["94a3f505.e1ed98","96395dc2.75b5b"]]},{"id":"4e2081dd.ff947","type":"inject","z":"675e227d.d158b4","name":"Stop","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"RESET","payload":"DEACTIVATE","payloadType":"str","x":240,"y":310,"wires":[["2fd5cce9.ddfff4"]]},{"id":"4a05fb21.37880c","type":"inject","z":"675e227d.d158b4","name":"Go","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":"2","topic":"RESET","payload":"ACTIVATE","payloadType":"str","x":240,"y":350,"wires":[["2fd5cce9.ddfff4"]]},{"id":"bbf26a6c.b7922","type":"mqtt-broker","name":"TIMEPI MQTT","broker":"192.168.0.99","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"SOM","birthQos":"0","birthRetain":"true","birthPayload":"BedPi UP","closeTopic":"EOM","closeQos":"0","closeRetain":"true","closePayload":"BedPi Shutting down","willTopic":"EOM","willQos":"0","willRetain":"false","willPayload":"BedPi COMMS FAIL"}]
3 outputs.
A signal.
An output to drive an icon to indicate the flow is working
An output with the value (in seconds) of the rate.
global.SCAN_FREQUENCY
is needed too!
It works on the other two. Tonight I put it on this machine.
The CPU load was pegging out and I noticed the SCAN_FREQUENCY is changing from 20000
to 30000
.
I used this to make it 21
seconds, waited for it to propagate to all machines.
(The other two didn't mind)
Their global
context changed from 20000
to 21000
.
This machine's kept cycling.
Here is that code:
[{"id":"3c1afcb4.570224","type":"function","z":"e2bd5a4e.5597e8","name":"Convert to ms","func":"var i = msg.payload;\nmsg.payload = i * 1000;\nreturn msg;","outputs":1,"noerr":0,"x":2630,"y":270,"wires":[["45d33d18.2ac44c","66fa5451.e1cdbc"]]},{"id":"7f067c3.bf1d884","type":"ui_numeric","z":"e2bd5a4e.5597e8","name":"","label":"Sec's","tooltip":"","group":"59b1e7ef.533e8","order":1,"width":"3","height":"1","wrap":false,"passthru":true,"topic":"","format":"{{value}}","min":"20","max":"120","step":1,"x":2430,"y":270,"wires":[["99913bc7.5a8dd8","3c1afcb4.570224"]]},{"id":"45d33d18.2ac44c","type":"mqtt out","z":"e2bd5a4e.5597e8","name":"Scan time","topic":"ALL/SCAN_FREQUENCY","qos":"2","retain":"true","broker":"931f34a.34a47c8","x":2880,"y":270,"wires":[]},{"id":"66fa5451.e1cdbc","type":"debug","z":"e2bd5a4e.5597e8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":2890,"y":220,"wires":[]},{"id":"99913bc7.5a8dd8","type":"ui_slider","z":"e2bd5a4e.5597e8","name":"Seconds slider","label":"Seconds","tooltip":"","group":"59b1e7ef.533e8","order":2,"width":0,"height":0,"passthru":true,"outs":"end","topic":"","min":"20","max":"120","step":1,"x":2430,"y":220,"wires":[["7f067c3.bf1d884"]]},{"id":"f6b64094.2ad29","type":"inject","z":"e2bd5a4e.5597e8","name":"","repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"20","payloadType":"num","x":2250,"y":220,"wires":[["99913bc7.5a8dd8"]]},{"id":"59b1e7ef.533e8","type":"ui_group","name":"Scan Frequency","tab":"c4158e69.633b08","order":3,"disp":true,"width":"6","collapse":false},{"id":"931f34a.34a47c8","type":"mqtt-broker","name":"MQTT host","broker":"192.168.0.99","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"SOM","birthQos":"2","birthPayload":"TimePi Comms Up","closeTopic":"EOM","closePayload":"TimePi shutting down","willTopic":"EOM","willQos":"0","willPayload":"TimePi Comms Failure"},{"id":"c4158e69.633b08","type":"ui_tab","name":"Tweak","icon":"mi-videogame_asset","order":10,"disabled":false,"hidden":false}]
As all control is via MQTT
why is only one machine getting the value changing from 20000
to 30000
?
Your help would be appreciated.
Oh, but it is now 23:22 local. I'm off to bed.
Oh, this is the original code from which I made the sub-flow.
The trigger
node is (was) modified to be the second output and the disabled text
node at the bottom with a link
beside it is the third output.
The "main" output is the function
node called "enable".
[{"id":"d0223860.6e52e8","type":"function","z":"e2bd5a4e.5597e8","name":"ENABLE","func":"if (msg.topic == \"RESET\")\n{\n // Detect what the ENABLE command is.\n if (msg.payload == 'ACTIVATE')\n {\n // If it is \"true\" indicate it is enabled and set 'enable'\n node.status({fill:\"yellow\",shape:\"dot\",text:\"ENABLED\"});\n context.set('enable',1);\n }\n //else\n if (msg.payload == 'DEACTIVATE')\n {\n // Otherwise mark as disabled and reset 'enable'\n context.set('enable',0);\n node.status({fill:\"red\",shape:\"dot\",text:\"DISABLED\"});\n }\n}\n// Now act of message which is not the \"ENABLE\" command.\nif (msg.topic != \"ENABLE\")\n{\n // Check if 'enable' is set to 1\n if (context.get('enable') == 1)\n {\n // Pass message through\n node.status({fill:\"green\",shape:\"dot\",text:\"On-line\"});\n return msg;\n }\n}\n","outputs":1,"noerr":0,"x":3520,"y":1140,"wires":[["6d0a5922.f550a8","6ccfb9b8.03ed7","482e23f0.7fe324"]]},{"id":"d683d7d2.9b7b2","type":"mqtt in","z":"e2bd5a4e.5597e8","name":"Local reading enable","topic":"RESET","qos":"2","datatype":"auto","broker":"931f34a.34a47c8","x":3270,"y":1050,"wires":[["d0223860.6e52e8"]]},{"id":"7227acd4.a8c4fc","type":"change","z":"e2bd5a4e.5597e8","name":"Foo","rules":[{"t":"set","p":"topic","pt":"msg","to":"RESET","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":3510,"y":1190,"wires":[["d0223860.6e52e8"]]},{"id":"3dc62aeb.573f56","type":"inject","z":"e2bd5a4e.5597e8","name":"Stop","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"DEACTIVATE","payloadType":"str","x":3510,"y":1240,"wires":[["7227acd4.a8c4fc"]],"icon":"font-awesome/fa-hand-paper-o"},{"id":"d8df1f1e.74da38","type":"inject","z":"e2bd5a4e.5597e8","name":"Go","repeat":"","crontab":"","once":true,"onceDelay":"3","topic":"","payload":"ACTIVATE","payloadType":"str","x":3511,"y":1280,"wires":[["7227acd4.a8c4fc"]]},{"id":"67c20e34.d3dc7","type":"debug","z":"e2bd5a4e.5597e8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":3150,"y":1280,"wires":[]},{"id":"bb83546b.e1a16","type":"delay","z":"e2bd5a4e.5597e8","name":"Variable delay","pauseType":"delayv","timeout":"10","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":3290,"y":1140,"wires":[["d1cda040.005908","101058cc.12d17f","d0223860.6e52e8","c660a1a1.bad89"]]},{"id":"d1cda040.005908","type":"function","z":"e2bd5a4e.5597e8","name":"Variable repeat time","func":"var pl;\nif (msg.topic == \"interval\") {\n node.warn(\"Value set\");\n node.status({fill:\"green\",shape:\"dot\",text:\"Value Set\"});\n context.set(\"interval\", msg.payload);\n// msg.payload = msg.payload / 1000;\n msg = null;\n} else {\n var interval = context.get(\"interval\");\n if (interval) msg.delay = interval;\n msg.payload = msg.delay/1000;\n}\nreturn msg;","outputs":1,"noerr":0,"x":3270,"y":1190,"wires":[["bb83546b.e1a16","6b630b81.0604d4"]]},{"id":"101058cc.12d17f","type":"change","z":"e2bd5a4e.5597e8","name":"Delay value","rules":[{"t":"set","p":"SCAN_TIME","pt":"global","to":"delay","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":3300,"y":1100,"wires":[[]]},{"id":"c4f765b8.e4c64","type":"mqtt in","z":"e2bd5a4e.5597e8","name":"Scan time","topic":"ALL/SCAN_FREQUENCY","qos":"2","datatype":"auto","broker":"931f34a.34a47c8","x":2860,"y":1140,"wires":[["741b10eb.15a03"]]},{"id":"3717f6ee.290d32","type":"change","z":"e2bd5a4e.5597e8","name":"","rules":[{"t":"set","p":"interval","pt":"msg","to":"payload","tot":"msg"},{"t":"set","p":"topic","pt":"msg","to":"interval","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":2910,"y":1190,"wires":[["9378e8d8.d77da"]]},{"id":"9378e8d8.d77da","type":"function","z":"e2bd5a4e.5597e8","name":"","func":"x = msg.payload;\nif (x >4999)\n{\n // All good\n node.status({fill:\"green\",shape:\"dot\",text:\"Good\"});\n} else\n{\n // Too small.\n node.status({fill:\"red\",shape:\"dot\",text:\"Too small\"});\n x = 5000;\n}\nmsg.payload = x;\nreturn msg;","outputs":1,"noerr":0,"x":3070,"y":1190,"wires":[["67c20e34.d3dc7","d1cda040.005908"]]},{"id":"c7e8b3f7.f3e9f","type":"inject","z":"e2bd5a4e.5597e8","name":"Start frequency","repeat":"","crontab":"","once":true,"onceDelay":"5","topic":"","payload":"30","payloadType":"num","x":2900,"y":1100,"wires":[["9c38fb0b.6370a","4d1bff30.82036"]]},{"id":"9c38fb0b.6370a","type":"function","z":"e2bd5a4e.5597e8","name":"","func":"var x = msg.payload;\nvar y = x * 1000;\nmsg = {delay: y, payload: x};\nreturn msg;","outputs":1,"noerr":0,"x":3070,"y":1140,"wires":[["bb83546b.e1a16"]]},{"id":"c660a1a1.bad89","type":"function","z":"e2bd5a4e.5597e8","name":"","func":"// var a = parseInt(\"10\")\n//var z = parseInt(msg.payload);\nvar z = msg.delay;\n//node.warn(z);\nvar x = z / 1000;\nmsg.payload = x;\nreturn msg;","outputs":1,"noerr":0,"x":3180,"y":1240,"wires":[["48521518.f0e7ec","e989799c.0c84","a283f6bd.5d1358"]]},{"id":"48521518.f0e7ec","type":"debug","z":"e2bd5a4e.5597e8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":3310,"y":1280,"wires":[]},{"id":"ecf16f5c.f8b6a","type":"inject","z":"e2bd5a4e.5597e8","name":"Reset","props":[{"p":"reset","v":"reset","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":3070,"y":1060,"wires":[["8d19503f.d2e048","bb83546b.e1a16"]]},{"id":"8d19503f.d2e048","type":"change","z":"e2bd5a4e.5597e8","d":true,"name":"","rules":[{"t":"set","p":"reset","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":3050,"y":1010,"wires":[["bb83546b.e1a16"]]},{"id":"56f696ad.4cfe4","type":"debug","z":"e2bd5a4e.5597e8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":2880,"y":1230,"wires":[]},{"id":"e989799c.0c84","type":"ui_text","z":"e2bd5a4e.5597e8","d":true,"group":"f1986590.13afa8","order":6,"width":"4","height":"1","name":"Scan frequency","label":"Scan frequency","format":"{{msg.payload}}","layout":"row-spread","x":3340,"y":1240,"wires":[]},{"id":"d1a7af6b.ec77d8","type":"inject","z":"e2bd5a4e.5597e8","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"18000","payloadType":"num","x":2870,"y":1280,"wires":[["3717f6ee.290d32"]]},{"id":"6b630b81.0604d4","type":"debug","z":"e2bd5a4e.5597e8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":3460,"y":1050,"wires":[]},{"id":"4d1bff30.82036","type":"debug","z":"e2bd5a4e.5597e8","name":"Chk","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":3070,"y":1100,"wires":[]},{"id":"741b10eb.15a03","type":"function","z":"e2bd5a4e.5597e8","name":"","func":"msg.payload = parseInt(msg.payload);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":2965,"y":1140,"wires":[["3717f6ee.290d32","56f696ad.4cfe4"]],"l":false},{"id":"a283f6bd.5d1358","type":"link out","z":"e2bd5a4e.5597e8","name":"Scan frequency","links":["14168e13.c63562","e71427d4.51f2b8"],"x":3275,"y":1320,"wires":[]},{"id":"6ccfb9b8.03ed7","type":"trigger","z":"e2bd5a4e.5597e8","name":"","op1":"1","op2":"0","op1type":"str","op2type":"str","duration":"800","extend":false,"units":"ms","reset":"","bytopic":"all","outputs":1,"x":3660,"y":1090,"wires":[[]]},{"id":"931f34a.34a47c8","type":"mqtt-broker","name":"MQTT host","broker":"192.168.0.99","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"SOM","birthQos":"2","birthPayload":"TimePi Comms Up","closeTopic":"EOM","closePayload":"TimePi shutting down","willTopic":"EOM","willQos":"0","willPayload":"TimePi Comms Failure"},{"id":"f1986590.13afa8","type":"ui_group","name":"TimePi Local Telemetry","tab":"173dcde5.96e88a","order":2,"disp":true,"width":"8","collapse":false},{"id":"173dcde5.96e88a","type":"ui_tab","name":"Local Readings","icon":"dvr","order":3,"disabled":false,"hidden":false}]