What am I doing wrong here?

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

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 { }.
image

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

Then I run the original flow, the original function node...
image

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

I can't spot the issue. What does it show in the debug window if you add the below line?

node.warn(flow.get('foo'));

I added to the question, with more testing, so please review the updated post. I believe it answers your query. I forced the flow variable to a know value, and even that gets dumped on... overwritten.

This might actually be some odd bug? Even stopping and restarting NR, does not seem to resolve the issue either. I tried that as well.

Just based on your code it seems like flow.get('something') would always return {}. Is this inside a subflow by any chance?

Yeah, i took it back to an even more basic test, and that works. So there is something in the original flow that is breaking something. This now is starting to make more sense. Coding and too much pizza I guess!

[{"id":"fb0757a6.c7dff","type":"inject","z":"f063fde6.2f474","name":"Test","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0x40,0x44,0x60,0x76,0x77","payloadType":"str","x":130,"y":240,"wires":[["e4c29ecf.4235"]]},{"id":"e4c29ecf.4235","type":"split","z":"f063fde6.2f474","name":"Split","splt":",","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":270,"y":240,"wires":[["1be58bca.141bd4"]]},{"id":"1be58bca.141bd4","type":"function","z":"f063fde6.2f474","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);\nnode.warn(flow.get('sensors'));\n\n//node.warn(theSensors);\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":420,"y":240,"wires":[[]]},{"id":"a7c5bbf1.036968","type":"inject","z":"f063fde6.2f474","name":"Invoke","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":130,"y":340,"wires":[["dfeecaa2.2d64f"]]},{"id":"dfeecaa2.2d64f","type":"function","z":"f063fde6.2f474","name":"Set","func":"var theArray = [ msg.payload ];\nflow.set('sensors', theArray);\nnode.warn(flow.get('sensors'));\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":270,"y":340,"wires":[[]]}]

Found it... the original flow sets the array to empty before the i2c scan... and typo... braces, not brackets! Nuts. Definitely... too much pizza, too late at night coding. Must have looked at it 10 times and just not seen the typo.

Yep this sort of problems are usually best left for the next day and fresh pair of eyes.

I should have, walked away... but I just could not let it go! LOL

I think the problem was the 'push' error/ When I first copied your flow, I was seeing that too but after making some changes it went away. Coping you flow in agai to use the current version without the {} on the get and I can't recreate it again.

Sometimes a node gets messed up some how and won't work but if you replace it with a new copy it starts working. I've never been able to determine the circumstances but have experienced it a couple times. So maybe that was the issue???

That makes sense... When I first started to debug the issue, it really did feel like something was not updating or refreshing. Now that I have it, right, it is working consistently. I use the flow to watch my primary PC, which has 4 monitors (3 that I use often). I got tried of turning things on and off, even with my phone... yes I can turn on the monitors with my phone if I want. So I just push the PC power, and my NR instance senses this via low level/slow ping loop, then sends CEC commands to monitors that support CEC, and on signals to the smart switches for those that don't, using the same MQTT logic my phone does. This is all because my two video cards don't do CEC and I don't leave anything in sleep mode.

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