Env variable at runtime?

Maybe it is only a silly question due to my dumbness in programming.
Is it possible to define an env variable inside a flow so that other flows can access it?

Thank you in advance for helping out

This is perhaps not quite what you mean but you can define global context variables, for example a change node can set global.ipaddress.
Once set, this variable is accessible in all flows, eg a change node can set msg.ipaddress to the value of global.ipaddress.

The function code to do this is

// set the value 
global.set("ipaddress", msg.payload)  

// use the value (in another flow)
msg.payload = global.get("ipaddress) || ""  // default

There can be context variables at flow (editor tab) level too. If you wanted to confine a variable to just a couple of tabs you could pass the value using link-out and link-in nodes

Thanks Jbudd, but this won't work in my case.
I need to set at runtime the serial port to which a serial node should connect to and I learnt that the only way to achieve it, is to use an env variable; the global variable cannot be used in this case.
I can set statically the env variable on the specific flow, but I would like to set it the first time the flow is run instead.

Even if you could set an env var at run time it still wouldn't do what you want. The point is that the serial port is configured, and connects, when node red is started (or deployed) so even if you set an env var later it still wouldn't affect the serial port.

I didn't realise this was about your serial port question, I thought that was solved.

If you can't do it in Node-red then presumably you could have an external program to communicate over the serial ports, exchanging data with Node-red over MQTT?

It makes sense now, thank you for clarifying this aspect I had not taken into account.

The serial port configuration is actually solved and it is working fine.
It was just a fixation of mine on doing it runtime, Colin clarified why it cannot work.

Thank you both for giving help to this dumb man, much appreciated

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