Variable between "On Start" and "On Message"

Hello

I have noticed a behavior difference between the Function Node between v2.x and v3.x

With v2.x, I used to have some const arrays setup in "On Start" and using them in "On Message"
Something like:
On Start:

misensors = { 
  "ATC354cc1": "salon_sam",
  "ATCa8ac99": "chbre_jmv",
  "ATCad1f66": "salon_tour",
  "ATC2bfbdf": "chbre_rose"
};

Then in the On Message, I was using that variable

...
if ( device  in misensors)   device = misensors[device];
...

When I upgraded to v3.x, everything was still working fine until I edited the function, then errors were pointed as I was using undeclared variabled.
Adding var or const keywords didn't helped so I had to move all those arrays to the On Message tab (adding the const keyword).

So my question 1: What did I missed ? What would have been the best way to code that ?

And my question 2: Now I need some non-constant variables that have to be shared across messages
Should I use context.set()/context.get() ?
Isn't there a way to use simple variables when I don't need the value to be really stored ?

thanks

your variables were always undefined but in V3.x, a better editor is used by default to help users minimise coding bugs. (you can turn this off but it is far better to improve your coding)

Yes, always

Dont store anything if you dont need it! I am probably missing your point, so post an exported flow and I will evaluate / help you improve.

Hi @Steve-Mcl

Thanks
By "stored" I meant "stored to filesystem" as I have configured the context to be stored by default to filesystem. Actually I am storing in (flow.)context values that are critical and that should be preserved in case node-red restarts.

But I have 2 other use-cases:

  • Case 1 : constants such as mapping tables. Those need to be set once for all at deploy/start of NR and doesn't need to be modified by code. I know about the improvement of the editor, great addition. But beside that, it was working in v2 to declare those kind of variables in "On Start" even not specifying var or const.
  • Case 2 : non critical values that I'm using more for debug and I don't care to loose in case of reboot

For those case, I feel that using context.xxx() API is a bit "too much" and remove the flexibility of using the values directly as variables/members without having the seed to call for get() and set()

Here is a simple message counter exempl where I solved the situation using this.
This is what I meant by "simple variables". Something that I can use directly and not through API calls
Still I don't understand why it is enough to use this. only once in "On Start" and once in "On Message" and not every time.

[{"id":"3ae3f90e6c815dd6","type":"function","z":"bbbd97e84cc4a26b","name":"counter","func":"if (msg.reset) {\n    this.counter = 0;\n    node.status({ fill: \"blue\", shape: \"dot\", text: `${counter}` })\n    return null;\n}\ncounter = counter + 1\nnode.status({ fill: \"blue\", shape: \"dot\", text: `${counter}` })\n\nreturn msg;","outputs":1,"timeout":"","noerr":0,"initialize":"// Code added here will be run once\n// whenever the node is started.\nthis.counter = 0\nnode.status({ fill: \"blue\", shape: \"dot\", text: `${counter}` })\n","finalize":"","libs":[],"x":440,"y":7220,"wires":[["a299c77d20d6cf41"]]},{"id":"279a9df6434c4ce7","type":"inject","z":"bbbd97e84cc4a26b","name":"count","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":210,"y":7200,"wires":[["3ae3f90e6c815dd6"]]},{"id":"be8c87b4ab52342a","type":"inject","z":"bbbd97e84cc4a26b","name":"reset","props":[{"p":"reset","v":"true","vt":"bool"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":210,"y":7240,"wires":[["3ae3f90e6c815dd6"]]},{"id":"a299c77d20d6cf41","type":"debug","z":"bbbd97e84cc4a26b","name":"debug 211","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":630,"y":7220,"wires":[]}]

I would be pleased to get your review and comments

Thanks

Well. It looks like you don't need to use this. at all. Try it & ignore the complaints when deploying as well as the red triangle. Your flow runs as you intended.

Why does it run? Node-RED re-uses the vm (to be precise the vm context) to run the scripts of the function node. Looks like the definition in "OnStart" thus gets forwarded to "OnMessage".

This might be convenient - I'm yet not convinced that this was the idea of operation. Why? The syntax checker treats the scripts as separate, not-linked scopes.

Those two aspects don't match...

:point_right: @Steve-Mcl

1 Like

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