parseFloat doesn't output zero values

I have this function receiving a stream of data from a script running an analog to digital converter.

var t=msg.payload;
if(t!=="")
{
k=t.split(':');
for (i = 0; i < k.length; i++)
{
    k[i]=parseFloat(k[i]);
}

msg.topic="Analogues";
	if(k[0]&&k[1]&&k[2]&&k[3]&&k[4]&&k[5]&&k[6]&&k[7]&&k[8])
		{
		msg.payload={"AP0":k[0],"AP1":k[1],"AP2":k[2],"AP3":k[3],"Ah":k[4],"InstP":k[5],"SOC4":k[6],"SOC3":k[7],"SOC5":k[8]};
		return msg;

    }
}

The function is working fine until any of the values in the stream of data is a 0.
Then, if any of the values is a 0(zero), the function doesn't output anything.

This is the payload:

"26.443:3.296:0.749:1.000:0.010:19.815:100.01:100.01:100.01"

Since can happen that a specific value is just a 0, how can I solve this inconvenience?
Any help will be appreciated.
Thanks

0 is falsey in JavaScript so this if clause will be skipped.

1 Like

Got it. Thanks

At the beginning of the cycle the script doesn't output all the values. This until all filters have stored enough values to start computing the output. So the first 10 second I have only a couple of variables with values.
Removing the if would cause a lot of errors:

Error: A 400 Bad Request error occurred: {"error":"unable to parse '102 AP0=26.486082000000003,AP1=undefined,AP2=undefined,AP3=undefined,Ah=undefined,InstP=undefined,SOC3=undefined,SOC4=undefined,SOC5=undefined': invalid boolean"}

I need to find another way... However now I do understand better what is happening.
Thanks for your help.

If you know there must be (for example) 9 or more items then test the length of k immediately after the split.

if(k.length < 9) {
  return null;
}
1 Like

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