Skip to content

Instantly share code, notes, and snippets.

@sirajmustafa
Last active November 26, 2020 20:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sirajmustafa/63dc333242b636ce42472cbf0ed1ab0d to your computer and use it in GitHub Desktop.
Save sirajmustafa/63dc333242b636ce42472cbf0ed1ab0d to your computer and use it in GitHub Desktop.
Get difference of previous vs current reading

This flow shows how quickly you can see the difference of a number between previous and current message cycle.

It uses a Sub Flow and makes the calculations look like a single node. I use this a lot of time. So built one like a virtual node. Hope this is useful.

Flow Visual:

[{"id":"a787ea25.6e2f08","type":"subflow","name":"Difference","info":"","in":[{"x":60,"y":120,"wires":[{"id":"c6c1a8f6.29f9a"},{"id":"1fb5778c.9ddfd"}]}],"out":[{"x":805,"y":283.5000305175781,"wires":[{"id":"2796226d.63c97e","port":0}]}],"outputLabels":["reading","difference"]},{"id":"1fb5778c.9ddfd","type":"function","z":"a787ea25.6e2f08","name":"Current Reading","func":"//set a topic to join later\nmsg.topic = 'curr'; \n\nreturn msg;","outputs":1,"noerr":0,"x":220,"y":180,"wires":[["b09d555.863fda8"]]},{"id":"c6c1a8f6.29f9a","type":"delay","z":"a787ea25.6e2f08","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"x":205.75,"y":327.0000305175781,"wires":[["4e42e8d7.cd4be"]]},{"id":"4e42e8d7.cd4be","type":"function","z":"a787ea25.6e2f08","name":"Previous Reading","func":"//set a topic to join later\nmsg.topic = 'prev'; \n\nreturn msg;","outputs":1,"noerr":0,"x":527,"y":326.00006103515625,"wires":[["b09d555.863fda8"]]},{"id":"b09d555.863fda8","type":"join","z":"a787ea25.6e2f08","name":"Join each topic","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","x":580,"y":179.25003051757812,"wires":[["2796226d.63c97e"]]},{"id":"2796226d.63c97e","type":"function","z":"a787ea25.6e2f08","name":"Difference","func":"var c = msg.payload.curr - msg.payload.prev;\n\n// Calculate\nif (msg.topic == 'curr'){\n msg.payload = c;\n return msg;\n}\n\n","outputs":1,"noerr":0,"x":828.75,"y":179.00003051757812,"wires":[[]]},{"id":"da79a4c0.ef6ae","type":"comment","z":"a787ea25.6e2f08","name":"input","info":"Connect your Reading as msg.payload","x":70,"y":80,"wires":[]},{"id":"24a6c50a.bd7c62","type":"comment","z":"a787ea25.6e2f08","name":"Delay 1msg / sec","info":"to be able to get previous value \none message later","x":207.5,"y":367.7500305175781,"wires":[]},{"id":"3d3075e7.af4442","type":"comment","z":"a787ea25.6e2f08","name":"Add Topic","info":"","x":556.25,"y":364.00006103515625,"wires":[]},{"id":"21ff46d1.635dba","type":"comment","z":"a787ea25.6e2f08","name":"Add Topic","info":"","x":240,"y":137.75003051757812,"wires":[]},{"id":"c9aeb317.ef8668","type":"comment","z":"a787ea25.6e2f08","name":"Calculate the Diff","info":"","x":840,"y":137.75003051757812,"wires":[]},{"id":"64216738.bfae5","type":"comment","z":"a787ea25.6e2f08","name":"Join / Combine msgs","info":"","x":580,"y":137.75003051757812,"wires":[]},{"id":"2fb21af.6acaae6","type":"comment","z":"a787ea25.6e2f08","name":"1st Out is Difference","info":"","x":930,"y":284.7500305175781,"wires":[]},{"id":"af671d82.d368a","type":"tab","label":"Calculate Difference","disabled":false,"info":""},{"id":"d05cca8e.eaf878","type":"subflow:a787ea25.6e2f08","z":"af671d82.d368a","name":"","x":680,"y":220,"wires":[["3c05a84e.0b7aa"]]},{"id":"a75ef7e0.831e6","type":"counter","z":"af671d82.d368a","name":"","init":"0","step":"1","lower":"","upper":"","mode":"increment","outputs":"2","x":480,"y":227,"wires":[["d05cca8e.eaf878","caff396f.823808"],[]]},{"id":"4b245ab.0e509a4","type":"function","z":"af671d82.d368a","name":"Random1-4","func":"// generate a random number between 4 & 1\nx = Math.floor((Math.random() * 4) + 1)\n\n// Increase the Counter x times\nmsg.increment = x;\n//msg.payload = x;\n\n//send msg object Asycnchonously multiple times \n//to create that many hits to the output\n// for (var i = 0; i < x; i++) {\n\n// node.send(msg);\n// }\n\nreturn msg;","outputs":1,"noerr":0,"x":290,"y":227,"wires":[["a75ef7e0.831e6"]]},{"id":"6e00a01c.fa3998","type":"inject","z":"af671d82.d368a","name":"every 1s","topic":"","payload":"","payloadType":"date","repeat":"1","crontab":"","once":true,"x":180,"y":127,"wires":[["4b245ab.0e509a4"]]},{"id":"3c05a84e.0b7aa","type":"debug","z":"af671d82.d368a","name":"Difference","active":true,"console":"false","complete":"payload","x":900,"y":220,"wires":[]},{"id":"caff396f.823808","type":"debug","z":"af671d82.d368a","name":"Current","active":true,"console":"false","complete":"payload","x":900,"y":300,"wires":[]},{"id":"b7f4362d.727388","type":"comment","z":"af671d82.d368a","name":"Difference node is a subflow","info":"Open and Edit Template to see details","x":660,"y":160,"wires":[]}]
@sirajmustafa
Copy link
Author

sirajmustafa commented Aug 15, 2017

Flow Visual

image

Please note this example uses a **counter** node, you can wire your own data source or download this node from **npm** or **manage palatte**

@SmurfyRaj
Copy link

Yeah !!! Your flow for the Difference Calculation was Awesome...!!!
This will be applicable for various IoT Applications in Updating the current value changes...

I want you to help me with the following Flow::

Application flow will output the following to the debug console:
*If the random number is less than 5 then output to the debug console:
Number is a low number
*If the random number is 5 or higher then output to the debug console:
Number is a high number

Where should be replaced with the random number generated

Number 6 is a high number

Number 2 is a low number

In the above flow I have generated a random number using 'Random' node. But I can't get the O/P as mentioned...
Pls Help me Buddy...!

@SmurfyRaj
Copy link

SmurfyRaj commented Aug 30, 2017

I have tried this... But I can't get the expected Output...

BTW am using Node Red on Cloud ... A Service by IBM Bluemix...

[{"id":"953c2ea7.1f54","type":"subflow","name":"Subflow 2","info":"","in":[{"x":260,"y":140,"wires":[{"id":"e886db5a.8c22c8"}]}],"out":[{"x":700,"y":80,"wires":[{"id":"330096b9.3ce54a","port":0}]},{"x":720,"y":200,"wires":[{"id":"9b692597.9f1858","port":0}]}]},{"id":"330096b9.3ce54a","type":"function","z":"953c2ea7.1f54","name":"Less than 5","func":"msg.payload = {\"The number is less than 5\" :msg.payload};\nreturn msg;","outputs":1,"noerr":0,"x":550,"y":80,"wires":[[]]},{"id":"e886db5a.8c22c8","type":"switch","z":"953c2ea7.1f54","name":"Check","property":"payload","propertyType":"msg","rules":[{"t":"lt","v":"5","vt":"num"},{"t":"gt","v":"5","vt":"str"}],"checkall":"true","outputs":2,"x":390,"y":140,"wires":[["330096b9.3ce54a"],["9b692597.9f1858"]]},{"id":"9b692597.9f1858","type":"function","z":"953c2ea7.1f54","name":"Greater than 5","func":"msg.payload = {\"The number is greater than 5\":msg.payload };\nreturn msg;","outputs":1,"noerr":0,"x":560,"y":200,"wires":[[]]},{"id":"b3bb27a.96775d8","type":"debug","z":"bbe9f8f7.981688","name":"","active":true,"console":"false","complete":"payload","x":710,"y":80,"wires":[]},{"id":"aef5b859.6941c8","type":"random","z":"bbe9f8f7.981688","name":"","low":"1","high":"10","inte":"true","x":240,"y":140,"wires":[["3b64752c.15930a"]]},{"id":"42807fc8.07378","type":"inject","z":"bbe9f8f7.981688","name":"","topic":"","payload":"","payloadType":"num","repeat":"5","crontab":"","once":false,"x":90,"y":140,"wires":[["aef5b859.6941c8"]]},{"id":"6ecec6af.2be458","type":"debug","z":"bbe9f8f7.981688","name":"","active":true,"console":"false","complete":"payload","x":710,"y":200,"wires":[]},{"id":"3b64752c.15930a","type":"subflow:953c2ea7.1f54","z":"bbe9f8f7.981688","name":"Check","x":430,"y":140,"wires":[["b3bb27a.96775d8"],["6ecec6af.2be458"]]}]

@anderskvist
Copy link

Nice module - I have an addition for you - I would have made a pull request if the repository wasn't private :(

In "Current Reading" add "msg.old_topic = msg.topic;" as the first thing
In "Difference" add " msg.topic = msg.old_topic;" before returning

That way, the topic will be restored :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment