# Connect mqtt-in and mqtt-out in one flow to request data and return it

**URL:** <https://discourse.nodered.org/t/connect-mqtt-in-and-mqtt-out-in-one-flow-to-request-data-and-return-it/59960>\
**Category:** General\
**Tags:** mqtt\
**Created:** [17 March 2022 00:36 UTC](https://discourse.nodered.org/t/connect-mqtt-in-and-mqtt-out-in-one-flow-to-request-data-and-return-it/59960 "2022-03-17T00:36:49Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![vaites](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vaites/32/58594_2.png) [@vaites](https://discourse.nodered.org/u/vaites)\
**Post date:** [17 March 2022 00:36 UTC](https://discourse.nodered.org/t/connect-mqtt-in-and-mqtt-out-in-one-flow-to-request-data-and-return-it/59960/1 "2022-03-17T00:36:49Z")

</div>

I'm trying to get data from a Dyson fan that uses mqtt. I can connect to the device and I have two flows:

1. http-in \> mqtt-out that send the request for data
2. mqtt-in \> debug that displays the data

The problem is how to connect both. I want to make an http request, trigger a mqtt-out, get the data from mqtt-in and return a http response with these data. How can I "inject" the mqtt-in data into the request to return it in the http response?

 ![flow](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/4/14436f9df8af505d9b68e914dd6cff753ac4d9fc.jpeg)

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [17 March 2022 00:51 UTC](https://discourse.nodered.org/t/connect-mqtt-in-and-mqtt-out-in-one-flow-to-request-data-and-return-it/59960/2 "2022-03-17T00:51:14Z")

</div>

Hi and welcome to the forum.

What you are asking is not too difficult and can be fully automated. But let's not get too far ahead of ourselves.

I'm not fully understanding what you mean, but I'll try.

First you need to know what _commands_ the fan accepts and what messages it sends back and how to read them.

Looking at your drawing, you send a `MQTT` message and it replies via `MQTT`.  
Then you look at the reply and depending on that send a command to turn the fan on/off.  
(Or something like that - yes?)

Ok, the `function` node (`2`) may not be needed. You may be able to construct the message in the `inject` node. But not a problem.

Then step `3` you receive the `MQTT` reply. You will need a `function` node (or other) there to determine what to do next.

Then you construct a message to send via the `http out` node to tell the fan to turn on/off.

Before we can go much further you are going to have to share what you see coming back in step `3`, and list what determines the next action.  
And lastly you need to know the command to send.

For now you could just establish that you can control the fan by turning it on/off by getting an `inject` node and constructing a message in it and connecting it to a `http out` node, configuring it as needed, and sending a message.

You can then put a second `inject` node so one turns the fan on and the other turns it off.

That may be a better way of doing it then you have established a working platform into which you can then build backwards to be controlled by other things.

I'll leave it there for now.

Best of luck and hope to hear back soon with the things needed to be known that I mentioned.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [17 March 2022 09:24 UTC](https://discourse.nodered.org/t/connect-mqtt-in-and-mqtt-out-in-one-flow-to-request-data-and-return-it/59960/3 "2022-03-17T09:24:45Z")

</div>

> [@vaites](#):
>
> How can I "inject" the mqtt-in data into the request to return it in the http response?

Is it that you don't know how to do the http request, or that you don't know how to trigger that from the mqtt In? If it is that you don't know how to trigger it then you should be able to do that by connecting the MQTT In node to the http request node. Or perhaps I have misinterpreted the question.

---

<div class="post-metadata">

**Author:** ![vaites](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vaites/32/58594_2.png) [@vaites](https://discourse.nodered.org/u/vaites)\
**Post date:** [17 March 2022 10:07 UTC](https://discourse.nodered.org/t/connect-mqtt-in-and-mqtt-out-in-one-flow-to-request-data-and-return-it/59960/4 "2022-03-17T10:07:17Z")

</div>

Sorry, I think my explanation is poor.

First of all, I know how to call all methods for the fan (on, off, request data...). There's no issue here.

I want something like a REST API: make a request to Node-RED (curl [http://host:1880/fan/status](http://host:1880/fan/status)) and get a JSON with the status and air quality values. I can't see how to do this because I need 2 flows: one for the request (http-in \> mqtt-out) and other for the response (mqtt-in AFTER mqtt-out).

I need to "join" these flows; the request must "wait" for the response. I can set a "global" (flow context?) variable on mqtt-in and when the incoming request makes the mqtt-out, wait for these variable to be set, and return to the HTTP client. But this seems too complex, so this is because I'm asking for a simple solution to "join" these flows.

Thanks again for your help. I'm starting with Node-RED and maybe I have some misuderestandings.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [17 March 2022 10:09 UTC](https://discourse.nodered.org/t/connect-mqtt-in-and-mqtt-out-in-one-flow-to-request-data-and-return-it/59960/5 "2022-03-17T10:09:48Z")

</div>

This type of flow might get you going (concept - not tested but I think the logic is OK)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/6/f6d61af0f2dac63b525d17a2185042db0835d3e3.png)

The Http in sends message off to MQTT out and the MQTT in listens for the reply and sets a flow context value

The original HTTP request is delayed to give the MQTT process time to respond and then sets the payload to the flow context value.

It's very coarse idea 🙂

---

<div class="post-metadata">

**Author:** ![vaites](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vaites/32/58594_2.png) [@vaites](https://discourse.nodered.org/u/vaites)\
**Post date:** [17 March 2022 15:25 UTC](https://discourse.nodered.org/t/connect-mqtt-in-and-mqtt-out-in-one-flow-to-request-data-and-return-it/59960/6 "2022-03-17T15:25:14Z")

</div>

Looks exactly what I need. Didn't know the "set" node so will try and report the results.

Thanks!

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [17 March 2022 16:10 UTC](https://discourse.nodered.org/t/connect-mqtt-in-and-mqtt-out-in-one-flow-to-request-data-and-return-it/59960/7 "2022-03-17T16:10:04Z")

</div>

the set node is the change node 🙂 - I just named them set

---

<div class="post-metadata">

**Author:** ![vaites](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vaites/32/58594_2.png) [@vaites](https://discourse.nodered.org/u/vaites)\
**Post date:** [17 March 2022 18:40 UTC](https://discourse.nodered.org/t/connect-mqtt-in-and-mqtt-out-in-one-flow-to-request-data-and-return-it/59960/8 "2022-03-17T18:40:52Z")

</div>

Yeah! It worked like a charm!. Many thanks, now I understand much better the nodes and flows.

 ![dyson](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/e/eeebdf515a16791f7217192432845ddcaa62f7b3.png)

---

<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:** [31 March 2022 23:14 UTC](https://discourse.nodered.org/t/connect-mqtt-in-and-mqtt-out-in-one-flow-to-request-data-and-return-it/59960/10 "2022-03-31T23:14:19Z")

</div>

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