Help with storing temporary data in either an array or object

(I am getting worse each passing day at remembering things)

I've tried to look for ways to do this but it is not fun. Confusion reigns supreme.

I have an incoming message.


msg.rain = 0, msg.count = 0
Then next time I get
msg.rain = 0, msg.count = 1

I want to store these in an..... array? or object. It isn't really critical which is the best as it is only for inspection and the data is only needed for maybe 12 iterations then wiped.
(Eventually this won't even be needed. This is just a passing curiosity.)

So I think I want to do something like:

rain[msg.count] = msg.rain;

each time so then I can look at the node's context and see what is happening.

But my attempts to understand how to do it are not really giving me anything I can understand.
Would someone mind helping me with getting my head around this - please.

What is msg.count counting? Is it just a counter 0, 1, 2, ... 11?

What does msg.rain mean - is it a measurement of rain quantity or a boolean 0/1?

msg.count is - as you said/asked - just a series of 1, 2, 3, 4, etc.
And reset if 0.

msg.rain is the amount of rain. So it is a number/measurement.

So I guess you need an array context variable
something like

let rainamount = context.get('rainamount') || []    // get context variable, default empty array
if (msg.count == 0) {           // if msg.count is 0, clear array (no idea if this is what you want)
   rainamount = []
rainamount.push(msg.rain)    // Add a new array element 
context.set('rainamount', rainamount)   // save to node context

eg [0, 0, 0, 2, 1, 4, 0, 0, 0, 0, 0, 0]

1 Like

That really looks like what I want to do.

But on/in my searches I found/saw stuff like that it it didn't work.

(Nothing against you in me saying that)

I am also torn between oversimplifying what I am doing and posting a whole lot of stuff I couldn't expect anyone to understand.

What I have SEEMS TO WORK - I've seen it doing the right thing.
But - me being ADHD/OCD/What ever wanted to drill down on what was/IS going on and became obsessed with seeing things that weren't there.
Sorry, WANTING to see things that weren't there.

And so I am now digging this huge hole and am floundering in what is going on.

I'll give it a go and let you know.

Alas that can't be until tomorrow (or later on today) as it is 08:41 and ...... I need it to go past 12:00 to check / see what happens.

Yes, it doesn't really matter one way or the other if I let things really happen with what data I have now.
But again it loops back to the ADHD / OCD problem and it isn't fun.

Well I have not tested that code, it might not work for the particular flow you are building.

Let's hope it does, if not, I apologise in advance for my bad advice. :grinning:

1 Like

I appreciate the names may not be right.

I am testing it with/on the broken down test flow I made with the needed name changes.

(This isn't saying you are wrong.)

This is my TEST flow.....
As broken down and as bare to the bones as I can get for testing.


[{"id":"143135ba0109bdce","type":"function","z":"68e3655f.b606b4","name":"function 42","func":"let rainamount = context.get('rainamount') || []    // get context variable, default empty array\nif (msg.count == 0) {           // if msg.count is 0, clear array (no idea if this is what you want)\n    node.warn(\"Wiping existing data\");\n    rainamount = [];\n    return;\n}\n\n//let c = parseInt(msg.count);\nlet v = msg.payload;\n\nnode.warn(\"v = \" + v);\n\nrainamount.push(v)    // Add a new array element \ncontext.set('rainamount', rainamount)   // save to node context\n\n\n\nmsg.payload = context.get('rainammount');\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":910,"y":420,"wires":[["6930f7c042cd0dc7"]]},{"id":"a91a087025472415","type":"counter","z":"68e3655f.b606b4","name":"","init":"0","step":"1","lower":null,"upper":null,"mode":"increment","outputs":"1","x":750,"y":420,"wires":[["143135ba0109bdce","648218dcc8b28c55"]]},{"id":"6930f7c042cd0dc7","type":"debug","z":"68e3655f.b606b4","name":"debug 2491","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1090,"y":420,"wires":[]},{"id":"98764bfae77b82d1","type":"random","z":"68e3655f.b606b4","name":"","low":"1","high":"10","inte":"true","property":"payload","x":600,"y":420,"wires":[["a91a087025472415"]]},{"id":"a1b220d69af20028","type":"inject","z":"68e3655f.b606b4","name":"Reset","props":[{"p":"reset","v":"reset","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":610,"y":460,"wires":[["a91a087025472415"]]},{"id":"648218dcc8b28c55","type":"debug","z":"68e3655f.b606b4","name":"count","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"count","targetType":"msg","statusVal":"count","statusType":"auto","x":890,"y":460,"wires":[]},{"id":"16a399fdec8cb4c3","type":"inject","z":"68e3655f.b606b4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":460,"y":420,"wires":[["98764bfae77b82d1"]]}]

Main part of the code in the function node.

let rainamount = context.get('rainamount') || []    // get context variable, default empty array
if (msg.count == 0) {           // if msg.count is 0, clear array (no idea if this is what you want)
    node.warn("Wiping existing data");
    rainamount = [];

//let c = parseInt(msg.count);
let v = msg.payload;

node.warn("v = " + v);

rainamount.push(v)    // Add a new array element 
context.set('rainamount', rainamount)   // save to node context

msg.payload = context.get('rainammount');
return msg;

Which is basically what you posted with some slight name changes to accommodate the names in this flow.

I press the reset node and you can see it wipes the values.
(Well, it kind of does, but as you can next see, the array is not wiped.)
(Not your fault. Just mentioning.)

Then when I send a message in, it is received and you see it with the node.warn() line. v = 8, but it isn't being stored because you get an undefined in the debug node.


I think I know what is wrong with it not being wiped.

I am not wiping the context part of rainammount. My mistake.
(Not now I'm stuck how to wipe it in context.)
context.set(rainammount) = []; doesn't work.
(ok, just saw that mistake. Changed from to '. Still doesn't work.) FIXED that part context.set('rainammount',); works. But am still / now getting[empty]` when I inject from the other node.


Sticky m key.

Yes, you have rainammount (two ms) there.

Yeah, sorry.... My fault.


Not sure what is going on there. But it seems to work - in the test flow at least.

I'll migrate it to the REAL one this afternoon/night I hope.

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