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

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?

Thanks in advance!

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.

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.

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) 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.

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

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 :slight_smile:

1 Like

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


the set node is the change node :slight_smile: - I just named them set

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

1 Like

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