# Dynamic IP in dynamic-mqtt out node

**URL:** <https://discourse.nodered.org/t/dynamic-ip-in-dynamic-mqtt-out-node/96448>\
**Category:** General\
**Tags:** mqtt\
**Created:** [7 April 2025 11:33 UTC](https://discourse.nodered.org/t/dynamic-ip-in-dynamic-mqtt-out-node/96448 "2025-04-07T11:33:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SofTMeha](https://avatars.discourse-cdn.com/v4/letter/s/54ee81/32.png) [@SofTMeha](https://discourse.nodered.org/u/SofTMeha)\
**Post date:** [7 April 2025 11:33 UTC](https://discourse.nodered.org/t/dynamic-ip-in-dynamic-mqtt-out-node/96448/1 "2025-04-07T11:33:29Z")

</div>

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?

 ![nodered4](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/3/438abbd7eb9e7bf4bf05165c12fece2f78f3fa32.png)  
 ![nodered5](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/a/2a129fdd72aa02cd27a7a8c4f281b8f63d018351.png)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [7 April 2025 12:42 UTC](https://discourse.nodered.org/t/dynamic-ip-in-dynamic-mqtt-out-node/96448/2 "2025-04-07T12:42:26Z")

</div>

> [@SofTMeha](#):
>
> MQTT Dynamic Broker out node

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.

> [@Dynamic MQTT node's "actions-\>connect" guidance needed](https://discourse.nodered.org/t/dynamic-mqtt-nodes-actions-connect-guidance-needed/58878/3):
>
> Thanks. That helped a lot

---

<div class="post-metadata">

**Author:** ![SofTMeha](https://avatars.discourse-cdn.com/v4/letter/s/54ee81/32.png) [@SofTMeha](https://discourse.nodered.org/u/SofTMeha)\
**Post date:** [8 April 2025 10:47 UTC](https://discourse.nodered.org/t/dynamic-ip-in-dynamic-mqtt-out-node/96448/3 "2025-04-08T10:47:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [22 April 2025 10:47 UTC](https://discourse.nodered.org/t/dynamic-ip-in-dynamic-mqtt-out-node/96448/4 "2025-04-22T10:47:26Z")

</div>

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