I have done this before, but now I can't seem to get it to work. The flow variable keeps initializing as an 'object' not an 'array' when it initializes. I am clearly using brackets not { } braces.
No flow variable exists...
Here is the function (scripting)...
You can clearly see that if flow variable sensors does not exist, I am initializing it as an array, i.e. using brackets, not braces.
I run the flow, i.e. function node, i.e. script, which should just add a new element to the array as each message received by the function node. But the debug output reports that push is not a function of an array variable. This makes no sense, I created an array and push is applicable to an array. Note the warn reporting that declares the flow variable is an object! That is just not right.
And of course when I check the context flow, sure enough the flow variable now is an empty object, not the populated array I expected, and I actually coded for.. I used brackets not braces { }.
So I created another flow, function node, to test this...
var theArray = [];
flow.set('sensors', theArray);
return msg;
Forcing the flow variable to be an array, this worked...
Then I run the original flow, the original function node...
And nuts, the flow variable is an object again, not an array! The following line of code in the original function should find the flow variable as is, or initialize it, right?
var theSensors = flow.get('sensors') || [];
The test flow created it, it was an array, so the re-run of the original flow function node should see that, no?
What the heck am I missing here? I must be blind, I am not seeing or missing something?
So I change the initialization flow to set an array with a single value, such that...
var theArray = [ msg.payload ];
flow.set('sensors', theArray);
return msg;
This sets the flow sensors variable to...
I then change the original flow function node to ONLY grab the flow variable, not do ANY initialization if not found...
Note, I did not clear the flow context variable, left it initialized as an array with 1 item in said array. I then run the flow that just grabs the flow variable, as is, as show above.
And... kaboom! I get an object flow variable again, empty!
I don't do drugs, I am not on meds.... but man, I can't see why this is happening. Someone please point out what I am doing wrong here? if anything?
Here is the flow export...
[{"id":"a364f8a5.627418","type":"inject","z":"525dd939.deda38","name":"Invoke","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":90,"y":160,"wires":[["1e6a91f6.27e6d6","69f90dd6.8ecaf4"]]},{"id":"1e6a91f6.27e6d6","type":"i2c scan","z":"525dd939.deda38","name":"","busno":"1","x":240,"y":160,"wires":[[],["398420b4.2bdd38"]]},{"id":"78ab1996.c4fd9","type":"switch","z":"525dd939.deda38","name":"Sensor?","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"0x29","vt":"str"},{"t":"eq","v":"0x40","vt":"str"},{"t":"eq","v":"0x44","vt":"str"},{"t":"eq","v":"0x76","vt":"str"},{"t":"eq","v":"0x77","vt":"str"},{"t":"else"}],"checkall":"false","repair":false,"outputs":6,"x":560,"y":180,"wires":[["a2c8333.7be01d"],["51a44568.e72704"],["f96c3052.859d7"],["ecd3219e.03d368"],["6ab2cdc0.e8e53c"],["1a391421.b846cc"]]},{"id":"1a391421.b846cc","type":"change","z":"525dd939.deda38","name":"Ohterwise?","rules":[{"t":"set","p":"descriptor","pt":"msg","to":"Not Recognize Sensor?","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":730,"y":280,"wires":[["4508a0b5.6c978"]]},{"id":"398420b4.2bdd38","type":"function","z":"525dd939.deda38","name":"Convert","func":"const BASE = 16;\n\nconst theHex = (msg.payload).toString(BASE);\n\nmsg.address = `0x${theHex}`;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":400,"y":180,"wires":[["78ab1996.c4fd9"]]},{"id":"69f90dd6.8ecaf4","type":"change","z":"525dd939.deda38","name":"!Sensors","rules":[{"t":"set","p":"sensors","pt":"flow","to":"{}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":240,"y":200,"wires":[[]]},{"id":"eec0bd8f.7f061","type":"inject","z":"525dd939.deda38","name":"Test","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0x40,0x44,0x60,0x76,0x77","payloadType":"str","x":90,"y":240,"wires":[["69f90dd6.8ecaf4","d6e7fac4.9da128"]]},{"id":"d6e7fac4.9da128","type":"split","z":"525dd939.deda38","name":"Split","splt":",","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":230,"y":240,"wires":[["a02de9f0.d7c2e8"]]},{"id":"a02de9f0.d7c2e8","type":"link out","z":"525dd939.deda38","name":"","links":["9901cef.25e093"],"x":335,"y":240,"wires":[]},{"id":"9901cef.25e093","type":"link in","z":"525dd939.deda38","name":"","links":["a02de9f0.d7c2e8"],"x":435,"y":220,"wires":[["78ab1996.c4fd9"]]},{"id":"a840eb3f.d2373","type":"function","z":"525dd939.deda38","name":"Update","func":"const theAddress = msg.payload;\n\n//\nvar theSensors = flow.get('sensors');\nnode.warn(theSensors);\n\n//\nlet theObject = {\n descriptor: msg.descriptor,\n address: msg.payload\n}\n \ntheSensors.push(theObject);\n\nflow.set('sensors', theSensors);\n \nnode.warn(theSensors);\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":480,"y":340,"wires":[[]]},{"id":"f96c3052.859d7","type":"change","z":"525dd939.deda38","name":"0x44","rules":[{"t":"set","p":"descriptor","pt":"msg","to":"SHT31 or HDC1008","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":710,"y":160,"wires":[["4508a0b5.6c978"]]},{"id":"51a44568.e72704","type":"change","z":"525dd939.deda38","name":"0x40","rules":[{"t":"set","p":"descriptor","pt":"msg","to":"HTU21D or SI7021 or HDC1008 ","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":710,"y":120,"wires":[["4508a0b5.6c978"]]},{"id":"a2c8333.7be01d","type":"change","z":"525dd939.deda38","name":"0x29","rules":[{"t":"set","p":"descriptor","pt":"msg","to":"TSL2591","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":710,"y":40,"wires":[["4508a0b5.6c978"]]},{"id":"6ab2cdc0.e8e53c","type":"change","z":"525dd939.deda38","name":"0x77","rules":[{"t":"set","p":"descriptor","pt":"msg","to":"BME280","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":710,"y":240,"wires":[["4508a0b5.6c978"]]},{"id":"ecd3219e.03d368","type":"change","z":"525dd939.deda38","name":"0x76","rules":[{"t":"set","p":"descriptor","pt":"msg","to":"BMP280","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":710,"y":200,"wires":[["4508a0b5.6c978"]]},{"id":"84a7609b.c43ae8","type":"change","z":"525dd939.deda38","name":"0x39","rules":[{"t":"set","p":"descriptor","pt":"msg","to":"TSL2561","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":710,"y":80,"wires":[["4508a0b5.6c978"]]},{"id":"4508a0b5.6c978","type":"link out","z":"525dd939.deda38","name":"","links":["5b34c5ea.0072d4","eab27e53.081c6"],"x":875,"y":160,"wires":[]},{"id":"5b34c5ea.0072d4","type":"link in","z":"525dd939.deda38","name":"","links":["4508a0b5.6c978"],"x":355,"y":340,"wires":[["a840eb3f.d2373"]]},{"id":"6362212f.07d648","type":"inject","z":"525dd939.deda38","name":"Invoke","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":90,"y":340,"wires":[["4930bee3.24816"]]},{"id":"4930bee3.24816","type":"function","z":"525dd939.deda38","name":"Set","func":"var theArray = [ msg.payload ];\nflow.set('sensors', theArray);\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":230,"y":340,"wires":[[]]}]
I am using the 'Test' injection, in the above testing, not the discovered i2c device list.