Flow returning NaN

I am having an issue with this flow. It is returning NaN and I cannot see why. I use this flow successfully on another device and it works fine, the only difference is that in this flow I read a single value from a float table, where in the flow that works the value comes from a single float variable.

[{"id":"d014f738.c3a538","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"9846f5f7.1f33a8","type":"pac-read","z":"d014f738.c3a538","device":"a1c6a2d4.8871f","dataType":"float-table","tagName":"Scales_Final","tableStartIndex":"1","tableLength":"1","value":"","valueType":"msg.payload","topic":"","topicType":"none","name":"","x":520,"y":180,"wires":[["bff3716e.21bef","643f2b4b.d0f324"]]},{"id":"4b8e09cc.7ba198","type":"inject","z":"d014f738.c3a538","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"10","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":320,"y":180,"wires":[["9846f5f7.1f33a8"]]},{"id":"bff3716e.21bef","type":"function","z":"d014f738.c3a538","name":"WT4100 rate calc","func":"var WT4100 = msg.payload; //the new tank weight\nvar WT4100_wt = WT4100[0];\n\nvar WT4100_array = context.get(\"WT4100_array\")||[]; //array holding values \nvar WT4100_rate15;\nvar WT4100_rate30;\nvar WT4100_rate45;\nvar WT4100_rate60;\nvar WT4100_ratefinal;\n\nWT4100_array.length = 360;\nWT4100_array.unshift(WT4100_wt);\n\nWT4100_rate15 = (WT4100_array[89] - WT4100_array[0]) * 4;\nWT4100_rate30 = (WT4100_array[179] - WT4100_array[0]) * 2;\nWT4100_rate45 = (WT4100_array[279] - WT4100_array[0]) * 1.2857;\nWT4100_rate60 = (WT4100_array[359] - WT4100_array[0]);\n\nWT4100_ratefinal = ((WT4100_rate15 + WT4100_rate30 + WT4100_rate45 + WT4100_rate60) / 4);\nif (WT4100_ratefinal < 0.0) WT4100_ratefinal = 0.0;\n\nmsg.payload = WT4100_ratefinal;\nmsg.wtarray = WT4100_array;\nmsg.weight = WT4100_wt;\nmsg.wt4100 = WT4100_wt;\ncontext.set(\"WT4100_array\", WT4100_array);\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":760,"y":180,"wires":[["22956815.ab0eb8","3472e248.10dbce","7fdb619e.cdffa"]]},{"id":"22956815.ab0eb8","type":"debug","z":"d014f738.c3a538","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":980,"y":180,"wires":[]},{"id":"3472e248.10dbce","type":"debug","z":"d014f738.c3a538","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1000,"y":120,"wires":[]},{"id":"7fdb619e.cdffa","type":"debug","z":"d014f738.c3a538","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1020,"y":240,"wires":[]},{"id":"643f2b4b.d0f324","type":"debug","z":"d014f738.c3a538","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":780,"y":100,"wires":[]},{"id":"a1c6a2d4.8871f","type":"pac-device","z":"","address":"localhost","protocol":"https","msgQueueFullBehavior":"DROP_OLD"}]

Looking at this now…
Would you happen to have an array you could paste here?
It would be useful to test your code with an array that matches what you have.

EDIT. I’m confused.
Looking at your flow, its expecting an array, but your post mentions a single value?

Hi Beno,
does this help?

Just FYI, you don’t need the three debugs on the end.
Just one.
Double click on that one and change it from msg.payload to complete msg object and then deploy.

It looks like nothing is being put into the array:

The issue is with the way you are initializing your array, you end up with a 360 element array with 359 null values.

Maybe this would work:

var WT4100_array = context.get("WT4100_array")|| Array(360).fill(WT4100_wt); //array holding values

1 Like

EDIT: I was mistaken, this won’t fix it… back to testing!

I think the problem could be that you are reading in a single value (msg.payload) which is NOT an array, just a number, so the value WT4100[0] is actually undefined, since there is no [0]. You should be able to save it directly to WT400_wt like this:

var WT4100_wt = msg.payload; //the new tank weight

Once you replace your first two lines with that, it should fix the undefined value, which hopefully fixes the math coming back as NaN.

I believe lynden is attempting to read a value from a float table in this flow, so msg.payload would be a single element array.

Good catch philip, thankyou! I totally looked over that. I’ll get back to testing.

After running a few more tests I think you’re spot-on, the array hasn’t filled up yet.

We see that there are 360 values in the debug pane because the length is forced to be 360 on line 11: WT4100_array.length = 360;

So each time the code runs it does add to the front of the array, but until it has ran 360 times to fill in all those values, WT4100_array[359] on line 17 is going to be undefined for the first 359 times, which breaks the math because WT4100_rate60 is NaN, thus when you use that to calculate ratefinal, it’s also NaN. You won’t be able to have these four rate calculations rolling along until that array is full.

I tested this by adding each of the partial rate values to the message output, then injected 180 random values and only rate15 and rate30 are successfully calculated, rate45, rate60, and final rate are all NaN:
image

This makes sense because rate15 only requires 0-89 values and rate 30 requires 0-179, the exact number of values I inject.
Then I injected all 360 and I am getting all the rates properly (including final rate):
image

Does that make sense?

Yes it does Terry. thanks. I just wasn’t patient enough to wait for it to fill up the table…

Happy I could help. It was certainly not obvious to me the first time I looked at it!
You don’t need it but just for the record / if anyone finds this thread in the future, here’s the code I used to inject a set number of random values (180 and 360 for my tests):
image

[{"id":"e6f726d0.cac298","type":"inject","z":"6773a439.5cfddc","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":460,"y":1740,"wires":[["36f048e.00ed2b8"]]},{"id":"36f048e.00ed2b8","type":"function","z":"6773a439.5cfddc","name":"send values","func":"for (i = 0; i < 180; i++) {\n    node.send({payload : [Math.random()*50]});\n}\nreturn null;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":630,"y":1740,"wires":[["89c1a06f.fe73a"]]}]
1 Like

In case you missed it, the line I posted would prefill the array with the current value so you can start getting results right away.

2 Likes