Search code examples
arraysvariablesglobalnode-red

node red global array variable - Get / Set not seeming to work


Working in Node-Red. I can use regular global variables across nodes and flows no problem. However, would like to use a global array variable.

Method A - desired functionality I read in 16 data points at a time (type = double) and want to have them be index 0-15, then the following node would update indexes 16-31; then 32-45 and 46-64 in the last two nodes.

Node Red however, won't let update the array from the second node starting from index #16. I get "TypeError: Cannot read property 'indexOf' of undefined" error.

In lieu of Method A, I could have four different 16-index global arrays. However, accessing them gives erratic results. Trying to access index[n] returns the value from some other index - i.e. global.get("variable"[0]) returns variable[10] and global.get("variable"[1]) returns the value from variable[27].

This describes the problem - https://www.youtube.com/watch?v=cF1bz8bEozI

Here is my sample flow:

[{"id":"ee1694d.7df4768","type":"i2c in","z":"d556390c.391838","name":"Read Camera","address":"105","command":"128","count":"32","x":240,"y":1480,"wires":[["9e27949c.512c28"]]},{"id":"d9eaa7a4.7f0ed8","type":"inject","z":"d556390c.391838","name":"ON","topic":"1","payload":"1","payloadType":"str","repeat":"","crontab":"","once":false,"x":70,"y":1480,"wires":[["ee1694d.7df4768"]]},{"id":"6dc0727a.4cf53c","type":"i2c in","z":"d556390c.391838","name":"Read Camera","address":"105","command":"160","count":"32","x":240,"y":1520,"wires":[["a7ac4b94.44ce58"]]},{"id":"d6d80973.784148","type":"i2c in","z":"d556390c.391838","name":"Read Camera","address":"105","command":"192","count":"32","x":240,"y":1560,"wires":[["b90d910d.8e743","ebeeb439.54cf18"]]},{"id":"b90d910d.8e743","type":"i2c in","z":"d556390c.391838","name":"Read Camera","address":"105","command":"224","count":"32","x":240,"y":1600,"wires":[["2f7b8dde.7a9902"]]},{"id":"6b1509e2.8bd4d8","type":"debug","z":"d556390c.391838","name":"Row 3,4","active":true,"console":"false","complete":"payload","x":1020,"y":1520,"wires":[]},{"id":"a828b6d2.40da08","type":"delay","z":"d556390c.391838","name":"","pauseType":"delay","timeout":"50","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":98,"y":1656,"wires":[["d6d80973.784148"]]},{"id":"ad0a1424.eaae08","type":"function","z":"d556390c.391838","name":"Save Global variables for Temperature","func":"global.set(\"RangeTemperaturesA\", 0);\n\nfor(i=0; i<16; i++){\n global.set(\"RangeTemperaturesA\"[i], msg.payload[i]);   \n}\n\nreturn msg;","outputs":1,"noerr":0,"x":750,"y":1480,"wires":[["6b810d97.0beee4","6dc0727a.4cf53c"]]},{"id":"d386a34f.525d2","type":"function","z":"d556390c.391838","name":"Save Global variables for Temperature","func":"for(i=0; i<16; i++){\n    global.set(\"RangeTemperatureB\"[i], msg.payload[i]);   \n}\n\nreturn msg;","outputs":1,"noerr":0,"x":750,"y":1520,"wires":[["6b1509e2.8bd4d8"]]},{"id":"11c935c.be330ca","type":"function","z":"d556390c.391838","name":"Save Global variables for Temperature","func":"for(i=0; i<16; i++){\n global.set(\"RangeTemperatureC\"[i], msg.payload[i]);   \n}\n\nreturn msg;","outputs":1,"noerr":0,"x":750,"y":1560,"wires":[[]]},{"id":"294185a.d5fe67a","type":"function","z":"d556390c.391838","name":"Save Global variables for Temperature","func":"for(i=0; i<16; i++){\n global.set(\"RangeTemperatureD\"[i], msg.payload[i]);   \n}\n\nreturn msg;","outputs":1,"noerr":0,"x":750,"y":1600,"wires":[[]]},{"id":"3ffa9e84.cba002","type":"function","z":"d556390c.391838","name":"Find Max Temperature","func":"//n = Math.max(... global.get(\"RangeTemperature\"));\n\nreturn {payload: global.get(\"RangeTemperature\")};","outputs":1,"noerr":0,"x":900,"y":1660,"wires":[[]]},{"id":"9e27949c.512c28","type":"function","z":"d556390c.391838","name":"Get Temps full row","func":"var gridEye = [];\nvar loop=0;\n\nfor(n=0; n<32; n+=2){\n    gridEye[loop] = ((msg.payload[n+1]<<8) | msg.payload[n]) * 0.25;\n    //convert to F\n    gridEye[loop] = ((5.0/3.0) * gridEye[loop] + 32.0).toFixed(2);\n    //add right bitshit to reduce noise\n    loop++;\n}\nmsg.payload=gridEye;\n\nreturn msg;","outputs":1,"noerr":0,"x":490,"y":1480,"wires":[["ad0a1424.eaae08"]]},{"id":"a7ac4b94.44ce58","type":"function","z":"d556390c.391838","name":"Get Temps full row","func":"var gridEye = []; //16-byte array with temperature readings\nvar loop=0;\n\nfor(n=0; n<32; n+=2){\n    // Get raw values - bitshift left 8 bits then bitwise OR.\n    // then take new value and multiply by 0.25 since it reads in 1/4 degree C\n    gridEye[loop] = ((msg.payload[n+1]<<8) | msg.payload[n]) * 0.25;\n    //convert to F\n    gridEye[loop] = ((5.0/3.0) * gridEye[loop] + 32.0).toFixed(2);\n    loop++;\n}\n\nmsg.payload=gridEye;\n\nreturn msg;","outputs":1,"noerr":0,"x":490,"y":1520,"wires":[["d386a34f.525d2"]]},{"id":"ebeeb439.54cf18","type":"function","z":"d556390c.391838","name":"Get Temps full row","func":"var gridEye = [];\nvar loop=0;\n/*\nvar pixel = 4;\nvar tmp = ((msg.payload[pixel*2 + 1]<<8) | msg.payload[pixel*2])*0.25; \n//gridEye reads in .25 degree C\ntmp = ((5/3 * tmp) + 32.0); //convert to F\n*/\n\nfor(n=0; n<32; n+=2){\n    gridEye[loop] = ((msg.payload[n+1]<<8) | msg.payload[n]) * 0.25;\n    //convert to F\n    gridEye[loop] = ((5.0/3.0) * gridEye[loop] + 32.0).toFixed(2);\n    //add right bitshit to reduce noise\n    loop++;\n}\n/*\nfor(n=0; n<8; n++){\n    gridEye[n]= ((n/35536 * 60 ) + 20);\n    //convert to F\n    //gridEye[n] = (((5.0/3.0) * gridEye[n]) + 32).toFixed(2);\n}\n*/\nmsg.payload=gridEye;\n\nreturn msg;","outputs":1,"noerr":0,"x":490,"y":1560,"wires":[["11c935c.be330ca"]]},{"id":"2f7b8dde.7a9902","type":"function","z":"d556390c.391838","name":"Get Temps full row","func":"var gridEye = [];\nvar loop=0;\n/*\nvar pixel = 4;\nvar tmp = ((msg.payload[pixel*2 + 1]<<8) | msg.payload[pixel*2])*0.25; \n//gridEye reads in .25 degree C\ntmp = ((5/3 * tmp) + 32.0); //convert to F\n*/\n\nfor(n=0; n<32; n+=2){\n    gridEye[loop] = ((msg.payload[n+1]<<8) | msg.payload[n]) * 0.25;\n    //convert to F\n    gridEye[loop] = ((5.0/3.0) * gridEye[loop] + 32.0).toFixed(2);\n    //add right bitshit to reduce noise\n    loop++;\n}\n/*\nfor(n=0; n<8; n++){\n    gridEye[n]= ((n/35536 * 60 ) + 20);\n    //convert to F\n    //gridEye[n] = (((5.0/3.0) * gridEye[n]) + 32).toFixed(2);\n}\n*/\nmsg.payload=gridEye;\n\nreturn msg;","outputs":1,"noerr":0,"x":490,"y":1600,"wires":[["294185a.d5fe67a"]]},{"id":"a0de3101.0c307","type":"function","z":"d556390c.391838","name":"Read from A","func":"var p = global.get(\"RangeTemperaturesA\"[1]);\nmsg.payload = p;\nreturn msg;","outputs":1,"noerr":0,"x":510,"y":1780,"wires":[["dbb0935d.742f7"]]},{"id":"dbb0935d.742f7","type":"debug","z":"d556390c.391838","name":"test A","active":true,"console":"false","complete":"payload","x":670,"y":1860,"wires":[]},{"id":"f6c50374.59f","type":"function","z":"d556390c.391838","name":"Read from B","func":"var n = global.get(\"RangeTemperatureB\"[0]);\nreturn {payload: n};","outputs":1,"noerr":0,"x":770,"y":1780,"wires":[["3e8947bc.be49b8"]]},{"id":"6b810d97.0beee4","type":"debug","z":"d556390c.391838","name":"Row 1,2","active":true,"console":"false","complete":"payload","x":1020,"y":1480,"wires":[]},{"id":"3e8947bc.be49b8","type":"debug","z":"d556390c.391838","name":"test b","active":true,"console":"false","complete":"payload","x":910,"y":1860,"wires":[]},{"id":"23da3f7f.1c4f8","type":"inject","z":"d556390c.391838","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":340,"y":1780,"wires":[["a0de3101.0c307"]]},{"id":"2a146fa7.577fc","type":"inject","z":"d556390c.391838","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":580,"y":1720,"wires":[["f6c50374.59f"]]}]

Edit - I did a quick test - For global.set -

global.set("RangeTemperaturesA", i)[i]; gives "TypeError: Cannot read property '0' of undefined"

global.set("RangeTemperaturesA[i]", i);  gives "Error: Invalid property expression: unexpected i at position 19"

global.set("RangeTemperaturesA"[i], i); appears to probably work.

Sample code:

for(i=0; i<16; i++){
 global.set("RangeTemperaturesA"[i], i); 
 node.warn("Value: " + i);
}
return msg;

Global.get -

global.get("RangeTemperaturesA"[n]) gives erratic results.
global.get("RangeTemperaturesA[n]") gives "Error: Invalid property expression: unexpected n at position 19"
global.get("RangeTemperaturesA")[n] gives "Value: undefined; Count: 0" gives "Value: undefined; Count: 0" which is perhaps the most promising if the array was never populated correctly.

Sample code:

for(n=0; n<16; n++){
node.warn("Value: " + global.get("RangeTemperaturesA")[n] + "; Count: " + n);
}
return msg;

Solution

  • The problem is due to the way you're trying to address the individual array entries.

    With the code: global.get("variable"[0]), you are asking it to use the 0th element of the string "variable" as the argument passed to the get function. In otherwords, it is equivalent to: global.get("v") Similarly, global.get("variable"[2]) will be equivalent to global.get("r").

    You should either move array index inside the quotes:

    global.get("variable[0]");
    

    or access the 0th element of the result of the get function:

    global.get("variable")[0];
    

    The same holds true for how you are trying to use the set function.

    Updates to reflect edits to the quesiton

    None of your attempts to use global.set() are correct:

    • global.set("RangeTemperaturesA", i)[i] - here you are setting the global property RangeTemperaturesA to the value of i. The function set doesn't return anything, so attempting to treat it as an array is just wrong.

    • global.set("RangeTemperaturesA[i]", i); - this is the closest of the three, however, you are setting the string literal RangeTemperaturesA[i] - JavaScript doesn't know you want the i in the middle of that string to be the value of your local variable i.

    • global.set("RangeTemperaturesA"[i], i); - no. This is the same error as you had in the original question. "RangeTemperaturesA"[i] will evalute to the ith character of the string RangeTemperaturesA.

    To do it properly, you want to use "RangeTemperaturesA["+i+"]" as the key:

    global.set("RangeTemperaturesA["+i+"]", i);
    

    When i is 0, that will generate the key RangeTemperaturesA[0].

    The same applies for global.get:

    var myValue = global.get("RangeTemperaturesA["+i+"]");
    

    All of these examples assume you have already set RangeTemperaturesA to be an array:

    global.set("RangeTemperaturesA",[]);