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