My flow returning NaN

I need to read several float variables from snap devices, perform a calculation on them, then send the calculation back to a snap device. Attached is a simplified version of the flow to illustrate the problem. I am getting NaN in msg.payload. What am I missing in this flow, I am sure the solution is easy to someone that uses node-red a lot. thanks

[{"id":"9298f268.d0e2","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"6265ca54.0d8e34","type":"inject","z":"9298f268.d0e2","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":271.5,"y":237,"wires":[["5d550f10.7f6b6","d4249bb2.1af898"]]},{"id":"5d550f10.7f6b6","type":"pac-read","z":"9298f268.d0e2","device":"bc5622e6.56b0c8","dataType":"float-table","tagName":"HS_HHA","tableStartIndex":"1","tableLength":"1","value":"flow","valueType":"msg","topic":"","topicType":"none","name":"","x":465.5,"y":234,"wires":[["8496b64c.333448"]]},{"id":"c5ea0f66.3e27d","type":"debug","z":"9298f268.d0e2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":841.5,"y":255,"wires":[]},{"id":"d4249bb2.1af898","type":"pac-read","z":"9298f268.d0e2","device":"bc5622e6.56b0c8","dataType":"float-table","tagName":"HS_HHA","tableStartIndex":"2","tableLength":"1","value":"pressure","valueType":"msg","topic":"","topicType":"none","name":"","x":457.5,"y":285,"wires":[["8496b64c.333448"]]},{"id":"8496b64c.333448","type":"function","z":"9298f268.d0e2","name":"","func":"var flow = msg.flow;\nvar pressure = msg.pressure;\nvar final = (flow/pressure);\nmsg.payload = final;\n\n\nreturn msg;\n\n\n","outputs":1,"noerr":0,"x":656.5,"y":255,"wires":[["c5ea0f66.3e27d"]]},{"id":"bc5622e6.56b0c8","type":"pac-device","z":"","address":"","protocol":"http"}]

You’re incredibly close! The problem is that your flow forks and then rejoins, rather than gathering each piece of data sequentially.

You’ll notice that when you inject on this flow you get two messages in the debug:

One of these has msg.flow, the other has msg.pressure because the timestamp injects, splits into two separate paths, gets one of the two values, then brings that one value into the function node. You effectively have two messages each with half of the data. And so var final = (flow/pressure); is only ever getting one of those two values, it can’t do the division, so you get NaN.

The good thing is that the fix is easy – just rewire your flow to get msg.flow and msg.pressure one after another so there are the two properties in one msg object, rather than getting each on a separate msg:

In general this sequential approach is the way to go with Node-RED. There are absolutely cases where forking and/or joining is necessary, but for the most part having one input and one output per-node is going to work best (and be easier to debug).

1 Like

Ok. I see that now. thanks for your help again Terry.

1 Like