# What is going on here with \`parsint(\`

**URL:** <https://discourse.nodered.org/t/what-is-going-on-here-with-parsint/55600>\
**Category:** General\
**Created:** [23 December 2021 08:17 UTC](https://discourse.nodered.org/t/what-is-going-on-here-with-parsint/55600 "2021-12-23T08:17:19Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [23 December 2021 08:17 UTC](https://discourse.nodered.org/t/what-is-going-on-here-with-parsint/55600/1 "2021-12-23T08:17:20Z")

</div>

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

Sample flow/nodes:

```auto
[{"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 `inject`node.

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)

 ![Screenshot from 2021-12-23 19-13-32](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/7/57a85d9ab3c7e20498a75fe2b659b4e795d7b831.png)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [23 December 2021 08:37 UTC](https://discourse.nodered.org/t/what-is-going-on-here-with-parsint/55600/2 "2021-12-23T08:37:31Z")

</div>

> [@Trying\_to\_learn](#):
>
> Why is it that `parseInt(` works when the numbers are ... non-zero.

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

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [23 December 2021 08:40 UTC](https://discourse.nodered.org/t/what-is-going-on-here-with-parsint/55600/3 "2021-12-23T08:40:30Z")

</div>

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.

 ![Screenshot from 2021-12-23 19-50-50](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/6/964b1cbec10e35f73eb5aa97d5ba7c709c8353a6.png)

 ![Screenshot from 2021-12-23 19-51-01](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/7/a7a1ddb09477aa303caded970da2387850e30075.png)

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [23 December 2021 08:59 UTC](https://discourse.nodered.org/t/what-is-going-on-here-with-parsint/55600/4 "2021-12-23T08:59:21Z")

</div>

> 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 😉

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 December 2021 09:01 UTC](https://discourse.nodered.org/t/what-is-going-on-here-with-parsint/55600/5 "2021-12-23T09:01:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [23 December 2021 09:12 UTC](https://discourse.nodered.org/t/what-is-going-on-here-with-parsint/55600/6 "2021-12-23T09:12:56Z")

</div>

So this part seems to be problematic.

```auto
    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?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 December 2021 09:38 UTC](https://discourse.nodered.org/t/what-is-going-on-here-with-parsint/55600/7 "2021-12-23T09:38:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 December 2021 09:40 UTC](https://discourse.nodered.org/t/what-is-going-on-here-with-parsint/55600/8 "2021-12-23T09:40:28Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/b/2bb15fe560a891d6b4177198ee03e50499d81491.png)

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

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [23 December 2021 09:40 UTC](https://discourse.nodered.org/t/what-is-going-on-here-with-parsint/55600/9 "2021-12-23T09:40:33Z")

</div>

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

```auto
[{"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.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [23 December 2021 09:41 UTC](https://discourse.nodered.org/t/what-is-going-on-here-with-parsint/55600/10 "2021-12-23T09:41:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 December 2021 09:41 UTC](https://discourse.nodered.org/t/what-is-going-on-here-with-parsint/55600/11 "2021-12-23T09:41:50Z")

</div>

Do you realise what it is setting it to?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 December 2021 09:46 UTC](https://discourse.nodered.org/t/what-is-going-on-here-with-parsint/55600/12 "2021-12-23T09:46:24Z")

</div>

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  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/3/6314d320d105eb0844304577ff5fa31ab46aeda6.png)

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [23 December 2021 09:46 UTC](https://discourse.nodered.org/t/what-is-going-on-here-with-parsint/55600/13 "2021-12-23T09:46:45Z")

</div>

Probably just text.

Colin,  
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.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [23 December 2021 09:49 UTC](https://discourse.nodered.org/t/what-is-going-on-here-with-parsint/55600/14 "2021-12-23T09:49:12Z")

</div>

![Screenshot from 2021-12-23 20-47-20](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/0/f0060a7ff770c52ed7adfe5371208bca012b40c0.png)

![Screenshot from 2021-12-23 20-47-16](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/b/9b22ff4c1722bb92785db52e6265c2c43ce51534.png)

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_.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 December 2021 09:51 UTC](https://discourse.nodered.org/t/what-is-going-on-here-with-parsint/55600/15 "2021-12-23T09:51:15Z")

</div>

> [@Trying\_to\_learn](#):
>
> So what am I missing here?

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

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [23 December 2021 09:52 UTC](https://discourse.nodered.org/t/what-is-going-on-here-with-parsint/55600/16 "2021-12-23T09:52:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [23 December 2021 09:53 UTC](https://discourse.nodered.org/t/what-is-going-on-here-with-parsint/55600/17 "2021-12-23T09:53:47Z")

</div>

# IRUEIY\*(^$(NJHUI!!!

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

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

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [23 December 2021 10:06 UTC](https://discourse.nodered.org/t/what-is-going-on-here-with-parsint/55600/18 "2021-12-23T10:06:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [6 January 2022 10:06 UTC](https://discourse.nodered.org/t/what-is-going-on-here-with-parsint/55600/19 "2022-01-06T10:06:35Z")

</div>

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