Merge Data from two asynchronous MQTT inputs

Hi all,

i am still very new to NodeRed and I think I didn't understand the principle yet. What I would like to do is the following:

  • I have two MQTT inputs. "Input 1" sends data from my smart meter every 10 seconds into influxdb. "Input 2" sends data from my solar inverter every 1 second.

  • Now I would like to use one parameter from "Input 1" (fed-in energy) and one parameter from "Input 2" (generated energy, do some math with them and create a new message.payload that contains the manipulated variable.

My attempts so far:

  1. I used two function blocks for each MQTT input to create defined topic+payload objects.
  2. I routed both outputs to a "Join" node and configured it to "join each msg.payload" --> no output is shown in debug

Next attempt:

  1. I used two function blocks for each MQTT input to create defined topic+payload objects.
  2. I routed both outputs to a third function block and tried to create two individual variables.
var total_energy = msg.payload.Total_energy
var solar_energy = msg.payload.Solar
  1. I do my math on this and created a new message with:
Messsage = {topic: "Some topic", payload: new_variable}

However, when I check this in the debug console I just get "NaN" in the message.payload tab. If I just return one of the two variables I initializes at the beginning I get "undefined" in the message.payload tab.

Does anyone know what I am doing wrong? Is this because both inputs are not in sync (1s vs. 10s)? If not, how does node red handle this?

Thank you for enlighten me :smiley:

You would have to store each value as the values are in separate messages, you can use context storage for this.

But it would be better to avoid context and use a join node, example in cookbook

You did it the right way: A join node in Manual mode set to combine each message.payload using the value of msg.topic (Since these messages come from two different mqtt inputs, it's safe to assume that there are two different msg.topics.

Perhaps you failed to specify "After 2 message parts"?

Is this because both inputs are not in sync (1s vs. 10s)? If not, how does node red handle this?

The setting "and every subsequent message" handles this.

1 Like

Yes that was my mistake! Thank you is working now :slight_smile:

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