Values Difference

Hey there!
I would like to do a flow that calculates the difference between the current value of an inject node and the previous value (with delay of 1 minute).
I tried to do 2 functions with the second one delayed for 1 minute, and then a function with a subtraction code, but somehow the result is NaN!
how can I do this flow?
Thank you,

Hi,
As multiple messages NEVER arrive at a the input of node at the same time (meaning your 2 values to compare are never avaiable at the function at the same time) you either need to store the previous value (in context for example), use a join node or some other means of getting the values you wish to compare into the same message.

See this article in the cookbook for an example of how to join messages into one object.


If you are new to node-red, I recommend watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

Hi,
This is the flow I did, I want to calculate the difference of ValueA for an interval of 1 minute. What changes should I make to make that happen?
Thank you,



[{"id":"23d311f.3d074ee","type":"tab","label":"Flow 11","disabled":false,"info":""},{"id":"2d5ba471.fa222c","type":"function","z":"23d311f.3d074ee","name":"Current Reading","func":"//set a topic to join later\nmsg.topic = 'curr'; \n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":300,"y":120,"wires":[["706bea03.89c674"]]},{"id":"b6139f33.34282","type":"delay","z":"23d311f.3d074ee","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"minutes","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"x":200,"y":320,"wires":[["c70554b8.d33f88"]]},{"id":"c70554b8.d33f88","type":"function","z":"23d311f.3d074ee","name":"Previous Reading","func":"//set a topic to join later\nmsg.topic = 'prev'; \n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":390,"y":320,"wires":[["706bea03.89c674"]]},{"id":"706bea03.89c674","type":"join","z":"23d311f.3d074ee","name":"Join each topic","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":620,"y":200,"wires":[["1ea8e754.6d5b99"]]},{"id":"1ea8e754.6d5b99","type":"function","z":"23d311f.3d074ee","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,"initialize":"","finalize":"","x":850,"y":200,"wires":[["58baa679.f5a198"]]},{"id":"f8c67071.d924a","type":"inject","z":"23d311f.3d074ee","name":"INJECT","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"ValueA\":90}","payloadType":"json","x":55,"y":220,"wires":[["2d5ba471.fa222c","b6139f33.34282"]],"l":false},{"id":"58baa679.f5a198","type":"debug","z":"23d311f.3d074ee","name":"Difference","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1050,"y":200,"wires":[]}]


This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.