Assign IP and Port of MQTT node dynamically

I need to assign IP and Port values (rectangles in picture) of MQTT node depending on a value from UI.
Is there a way to do so?
Capture

No, not with the standard mqtt nodes.

Thanks for replying . which node can I use?

I am not aware of one.

Have you tried using environment variables? E.g. $(port)

Note, env vars need to be set in the flow and environment before node-red starts.

Ps, can't remember the exact format for env vars. If that doesn't work, try ${port}

1 Like

I'm new to node-red so I just knew about environment variables now, can you tell me where to set it exactly?
I tried searching,and understand how to get env. variables but how to set it ?

In the boxes you highlighted, enter what I showed you.

Note - I am not 100% certain this is possible (It is an educated guess or a distant memory) so you're gonna have to try it out.

Ps,
If you don't know what environment variables are, search the internet.

See this link for how to use it in a node's settings (It is ${ENV_VAR}). https://nodered.org/docs/user-guide/environment-variables

I had assumed that you want to dynamically change the address/port on the fly, but if you just want to preset it to a value that you know before hand but don't want to build into the flow then environment variables is the way to do it.

I need the user to have flexible broker setting. so he can (at anytime) change the IP and port from UI and that will update my data and then start connecting on the new broker.

In that case you can't use environment variables as they are picked up when node-red is deployed, unless you change them from the flow and then restart node-red (which can be done from the flows).

1 Like

A drop down list? So operator/user selects from an approved broker list, then you pass the selection to a test node? Where test node has wire to each defined MQTT publish node? This would work well if you only had a few brokers? At some point I will have a redundant broker... I plan to just use ping or ARP query to determine which broker is online. Another option is to have CNAME record in DNS, such that broker.domain.org resolves to both a-broker.domain.org and b-broker.domain.org. If you can't resolve/ping 'a' then send to 'b' otherwise send to 'a' type of thing. CNAME records in DNS are often used for various things, this is one such case.

Firstly, Thanks alot for your help
I'm working on doing what you said, I learned how to deal with environment variables. But still can't figure out the restart part, but I'm working on it.
I just don't understand what will happen if node red restarted, how the port of the MQTT node will change by itself?

It won't change by itself, you have to change the value in the system environment variable, then when node-red restarts it will use the new value from the environment variable, assuming that you have specified that variable in the node's config.

It worked.
Thanks a lot for your help. I really appreciate it, I learned a lot.
I'm going to adjust the code to be understandable and clean, then put it here.

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