What is going on here with `parsint(`

I seem to have played with this so much I have broken it.

Sample flow/nodes:

[{"id":"6ffac257e6553cb2","type":"inject","z":"65c9b63cb09879a0","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":180,"y":2880,"wires":[["844b6e6c.aadbc8"]]},{"id":"844b6e6c.aadbc8","type":"change","z":"65c9b63cb09879a0","name":"","rules":[{"t":"set","p":"name","pt":"flow","to":"payload","tot":"msg"},{"t":"set","p":"HeartBeat-1","pt":"flow","to":"rgb,0,0,0,9","tot":"str"},{"t":"set","p":"HeartBeat-2","pt":"flow","to":"rgb,0,0,9,0","tot":"str"},{"t":"set","p":"HeartBeat-3","pt":"flow","to":"rgb,0,0,0,1","tot":"str"},{"t":"set","p":"HeartBeat-4","pt":"flow","to":"rgb,0,0,1,0","tot":"str"},{"t":"set","p":"Heartbeat-5","pt":"flow","to":"rbg,0,0,0,0","tot":"str"},{"t":"set","p":"REQUEST","pt":"flow","to":"100,014,000","tot":"str"},{"t":"set","p":"REQUEST2","pt":"flow","to":"100,000,000","tot":"str"},{"t":"set","p":"DETECTED","pt":"flow","to":"010,010,000","tot":"str"},{"t":"set","p":"FULL","pt":"flow","to":"000,010,000","tot":"str"},{"t":"set","p":"REMOVED","pt":"flow","to":"000,000,000","tot":"str"},{"t":"set","p":"LED_SPEED","pt":"flow","to":"300","tot":"num"},{"t":"set","p":"LEDSTRIP","pt":"flow","to":"[\"0,0,0\",\"0,0,0\",\"0,0,0\",\"0,0,0\",\"0,0,0\",\"0,0,0\",\"0,0,0\",\"0,0,0\",\"0,0,0\",\"0,0,0\"]","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":2880,"wires":[[]]},{"id":"9d13b82ce45b6455","type":"function","z":"65c9b63cb09879a0","name":"ADD values","func":"//  Define variables here/now\nlet n_red = \"\";\nlet n_green = \"\";\nlet n_blue = \"\";\nlet e_red = \"\";\nlet e_green = \"\";\nlet e_blue =\"\";\nlet x_red = \"\";\nlet x_green = \"\";\nlet x_blue = \"\";\n\nlet payload = \"\";\nlet blah = \"\";\nlet led = \"\";\n\nif (msg.debug != undefined)\n{\n    context.set(\"DEBUG\",msg.debug);\n    node.warn(\"Debug set to \" + msg.debug);\n    return;\n}\n\nlet debug = context.get(\"DEBUG\") || 0;\n\nif (msg.mode != undefined)\n{\n    //\n    if (debug == 1)\n        node.warn(\"Mode being changed\");\n    context.set(\"mode\",msg.mode);\n    if (msg.mode == 0)\n        node.status({fill: \"green\",text:\"OR\"});\n    else\n        node.status({fill: \"red\",text:\"AND\"});\n    return;\n}\n\nlet mode = context.get(\"mode\") || 0;\n\n//  ----------------  Do not put code before here.\n\n//  Get LED number from `payload`\n//let led = parseInt(msg.payload.split(\",\")[1]);\npayload = msg.payload\nblah = payload.split(\",\")[1];\n\nled = parseInt(blah);\nif (debug == 1)\n    node.warn(\"LED \" + led);\n\n//if (led > 4)\nif (led > 1)\n{\n    if (debug == 1)\n        node.warn(\"LED Colour adjustment happening\");\n    //  Get RGB values from `payload`\n    n_red = parseInt(payload.split(\",\")[2])\n    n_green = parseInt(payload.split(\",\")[3])\n    n_blue = parseInt(payload.split(\",\")[4])\n    if (debug == 1)\n    {\n        node.warn(\"==== New values below ====\");\n        node.warn(\"R \" + n_red + \" G \" + n_green + \" B \" + n_blue);\n    }\n\n    let existing_value = msg.existing[led];\n    //  Now get existing RGB values.\n    if (existing_value == undefined)\n    {\n        existing_value = \"0,0,0\";\n    }\n    x_red = parseInt(existing_value.split(\",\")[0]);\n    x_green = parseInt(existing_value.split(\",\")[1]);\n    x_blue = parseInt(existing_value.split(\",\")[2]);\n    \n    if (debug == 1)\n    {\n        node.warn(\"---- Exiting values below ----\");\n        node.warn(\"Read from file \" + existing_value + \"<\");\n        node.warn(\"R \" + x_red + \" G \" + x_green + \" B \" + x_blue);\n    }\n\n    //  Old code\n    //red = n_red + x_red;\n    //green = n_green + x_green;\n    //blue = n_blue + x_blue;\n    //  New code 2021 12 05         was `&&` changed to `||`\n    if (mode == 0)\n    {\n        red = n_red || x_red;\n        green = n_green || x_green;\n        blue = n_blue || x_blue;\n    } else\n    if (mode == 1)\n    {\n        red = n_red && x_red;\n        green = n_green && x_green;\n        blue = n_blue && x_blue;\n    }\n    \n    msg.payload = \"rgb,\" + led + \",\" + red + \",\" + green + \",\" + blue\n}\n\nif (msg.wipe == \"OVERWRITE\")\n{\n    //\n    msg.payload = \"rgb,\" + led + \",\" + n_red + \",\" + n_green + \",\" + n_blue\n}\n\n\n\n//if (msg.payload.who == \"All clear\")       //  Old code\nif (msg.wipe == \"WIPE\")\n{\n    //\n    //  Wipe the alarm LEDs\n    //\n    //  work needed\n    msg.payload = \"rgb,\" + led + \",0,0,0\";\n\n}\nif (msg.clear == \"CLEAR\")\n{\n    //  `flow.LEDSTRIP`\n    flow.set(\"LEDSTRIP[\"+led+\"],0,0,0\")\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":540,"y":2930,"wires":[["f135414eee99642b"]]},{"id":"ffa890cc6ed08f18","type":"change","z":"65c9b63cb09879a0","name":"get `existing`","rules":[{"t":"set","p":"existing","pt":"msg","to":"LEDSTRIP","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":2930,"wires":[["9d13b82ce45b6455"]]},{"id":"2c627d90.accd32","type":"inject","z":"65c9b63cb09879a0","name":"","props":[{"p":"payload","v":"rgb,3,30,30,30","vt":"str"},{"p":"topic","v":"","vt":"string"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"rgb,3,30,30,30","payloadType":"str","x":170,"y":2930,"wires":[["ffa890cc6ed08f18"]]},{"id":"f135414eee99642b","type":"debug","z":"65c9b63cb09879a0","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":710,"y":2930,"wires":[]},{"id":"366e3c64c2a3a967","type":"inject","z":"65c9b63cb09879a0","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"rgb,3,0,0,0","payloadType":"str","x":160,"y":2980,"wires":[["ffa890cc6ed08f18"]]}]

Press the top inject node first to set flow context.

Then press the middle injectnode.

You will see a nice message come out the other side.

Now press the bottom inject node.

You get NaN messages.

Why is it that parseInt( works when the numbers are ... non-zero.
But as soon as the value is 0 it fails.

Weirdly I am 98% sure it was working but suddenly it decided to stop working.

And I don't know why.

Someone - please.

Oh for the sake of helping what I am talking about: (Piccie)

parseInt works according to its specification 100% of the time. Use node.warn statements before each parseInt - you should find the answer.

1 Like

Yeah, thanks Steve.....

Remember: You are talking to the village idiot.

ITMT, I have found the Number which seems to work better, but still isn't perfect.

I'm suspicious parseInt("0") and 0 isn't an integer. (Shrug)

But now I've put Number in, the BLUE value is always giving NaN.

So I really have a screwed up perception of what is going on.

Ok, parseInt("0") works. I get a 0.

So this is what I see now.

Look at the outputs and what the lines are.

So in one case Number works for all but not BLUE.
Where as parseInt doesn't like any value.
Also what it read in as the existing value from the context.

Also what it read in as the existing value from the context.

You might want to check that part. I tried to digest your function node, but was unable to parse it, just like the parseInt :wink:

Have you added node.warn() immediately before the parseInt or Number as suggested?

So this part seems to be problematic.

    let existing_value = msg.existing[led];
    //  Now get existing RGB values.
    if (existing_value == undefined)
        existing_value = "0,0,0";
    node.warn("====> " + existing_value)

Because I am only seeing ,

So msg.existing isn't constructed correctly allowing me to index it with the first line here.

led is correct and I see the 3 and as it is just after the parseInt() it is a number.
And as subsequent testing where I changed the "0,0,0" to "1,2,3" and wasn't seeing that coming out....

So.... What is wrong with how msg.existing is constructed/formatted?

Add a debug node showing what is going into the message set to output complete message, and see what msg.existing contains.


If you think that is setting flow.LEDSTRIP to an array, it isn't.

1 Like

Well, maybe along that line, this is what I have been trying.

[{"id":"c8a05e3924c0a81e","type":"function","z":"65c9b63cb09879a0","name":"","func":"let existing = [\"0,0,0\",\"0,0,0\",\"0,0,0\",\"1,2,3\",\"0,0,0\",\"0,0,0\",\"0,0,0\",\"0,0,0\",\"0,0,0\",\"0,0,0\"];\n\n//existing = flow.get(\"LEDSTRIP[]\");\n\nlet led = 3;\n\nlet existing_value = existing[led];\n\nnode.warn(\"====> \" + existing_value)\nx_red = parseInt(existing_value.split(\",\")[0]);\nx_green = parseInt(existing_value.split(\",\")[1]);\nx_blue = parseInt(existing_value.split(\",\")[2]);\n\n\nmsg.payload = 8;\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":350,"y":3330,"wires":[["178fb431e798d168"]]},{"id":"5e04db53e6f7984a","type":"inject","z":"65c9b63cb09879a0","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":190,"y":3330,"wires":[["c8a05e3924c0a81e"]]},{"id":"178fb431e798d168","type":"debug","z":"65c9b63cb09879a0","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":530,"y":3330,"wires":[]}]

If I define the existing values in the node it works fine.

So to me it is something to do with how the flow's context is . . . constructed.
I am not helping myself with what it is: array, object, (or something else)

But what I posted works. I don't get why I can't get that from the flow and use it.

So how do I make it set it as an array?

Do you realise what it is setting it to?

It is setting it to a string. You are providing it with a JSON string, so to tell the change node that you want it to convert that to a javascript object (or array in this case) then use

Probably just text.

Honestly I have NO IDEA what I am doing.
That last bit of code (the three nodes) where I set up the array in the line and then things works is what I am wanting to do.

Ok, only recently I added that line in the changed node because for some reason some of the elements weren't getting defined and .... (what ever)

It was working - and if I take that out it probably will again.

I'm stupid. But.

I see a big disconnect with how I can have/use/make this array in the function node and things work.
But I can't create it as a flow.context thing.

I'm missing something in how this is done.
If I knew that, none of this would be a problem.

Screenshot from 2021-12-23 20-47-20

Screenshot from 2021-12-23 20-47-16

So what am I missing here?

As far as I can see, I did what you said.... (Way back)
All I get is this error.

Oh, the 1,2,3 is only so I can see/check that it is reading the right index and getting the right values in the right name.

Why have you changed the double quotes to single quotes? JSON requires double.

1 Like

Ok, sorry. I am sure I tried it with double as well.
But then - seeing the error - though the double quotes were tripping up on themselves.

I'll give it another go.

Hang on.


I don't know what happened. Now it is working.

Will this be the end of these lookup problems and the NaN problems?
I'll find out soon enough.

I've got a whole lot of back pedalling to do.

Alas this is on a machine which is not at 99% CPU load 99% of the time. This is going to be slow.

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