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

Flow Info

created 3 months, 2 weeks ago

Node Types

Core
  • comment (x8)
  • debug (x2)
  • delay (x1)
  • function (x4)
  • inject (x1)
Other
  • counter (x1)
  • join (x1)
  • subflow (x1)
  • subflow:a787ea25.6e2f08 (x1)
  • tab (x1)

Tags

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