Dynamic IP in dynamic-mqtt out node

Hello,

I’m working with Node-RED and trying to use the MQTT Dynamic Broker out node to connect to an MQTT broker using a dynamically provided flow-level IP address. The goal is to pass the IP address to the broker dynamically via a text input widget in the UI, but I’m encountering an issue where the connection fails. The terminal shows the following error:

**Connection failed to broker: mqtt://$undefined:1883**

Setup:

  1. UI Setup: I have a text input widget that lets the user input an IP address.
  2. Change Node: I use a Change node to update the flow context. Specifically, I set msg.payload (UI input)to flow.ip.
  3. Function Node: I use a Function node to get the IP address from the flow context and assign it to msg.hostname2. This msg.hostname2 is then used in a debug node to confirm the value.

Problem:

  • Initially, when the flow starts, the IP address is empty, so the MQTT Dynamic Broker out node tries to connect with mqtt://$undefined:1883, which results in the connection failure.
  • After the user inputs the IP address (e.g., 10.2.35.117), the debug node correctly shows the updated value of msg.hostname2. However, the MQTT Dynamic Broker out node still fails to connect, and the terminal message shows mqtt://$undefined:1883 instead of mqtt://10.2.35.117:1883.

What I’ve Tried:

  1. Hardcoding the IP Address: I hardcoded the IP address directly in the MQTT Dynamic Broker out node, and the connection works fine. However, I want to avoid hardcoding the IP and have it set dynamically by the user.
  2. Testing with Variables: I tested using ${msg.hostname2} and ${flow.ip} in the MQTT Dynamic Broker out node to pass the IP address. Both attempts still result in the error, and I’m not sure which variable to use.
  3. Debugging: The debug node shows that the flow-level variable msg.hostname2 is correctly updated with the IP address once the user inputs it.

What I Expect:

I would like the MQTT connection to be established using the dynamic IP address that the user inputs via the UI. The MQTT Dynamic Broker out node should then use the updated value from the flow context and successfully connect to mqtt://<IP_ADDRESS>:1883.

  • Why does the MQTT Dynamic Broker out node still try to connect with mqtt://$undefined:1883 even after the IP address has been set in the flow context?
  • Should I be using msg.hostname2 or flow.ip in the MQTT Dynamic Broker out node, and why isn’t it working?

Can anyone help identify what might be causing the connection failure or provide suggestions for how to resolve this issue?


Don't use this node. It is too old and unsupported.

If you want to dynamically connect to a broker just use the built in MQTT nodes.

1 Like

Wow, I have no idea how I missed this topic. It works like a charm. Thank you for the help Steve!

1 Like