Subtract two separate message values

I have 2 Sources of MQTT data coming in, i need a function that deducts them from each other. The issue is there is a slight delay between messages coming in, so i dont get the output.

Messages will never arrive at a node input at the same time no matter how instantaneous you think they are.

You need a join node or store the values in context.

See this article in the cookbook for an example of how to join messages into one object.

1 Like

So do you have two mqtt-in nodes or one getting two messages with different topics?

In either case, you need to join the two messages so you can access the data to do the subtraction OR put the data in a flow variables.

But what happens if you get msg1 then msg 1 again then msg2?

@bceyresy

As others have said above.

Messages never arrive at the same time, or know about each other - despite it looking like so sometimes - each message is an entirely different message

as suggested by @Steve-Mcl

Use a join Node to combine the 2 messages (and set it to flush the message after 2)

Screenshot 2023-12-21 at 15.54.09

This will then deliver an array of 2 message, which you can process at the same time.

Cheap example

[{"id":"6ddc60dd2457aaff","type":"inject","z":"a35bca0e4d8d389a","name":"10","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"10","payloadType":"num","x":685,"y":200,"wires":[["0a59d65f6139832a"]]},{"id":"965f5dd93f8417d8","type":"inject","z":"a35bca0e4d8d389a","name":"10","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"10","payloadType":"num","x":675,"y":275,"wires":[["0a59d65f6139832a"]]},{"id":"0a59d65f6139832a","type":"join","z":"a35bca0e4d8d389a","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":860,"y":230,"wires":[["b49c40c6d821fcc2"]]},{"id":"b49c40c6d821fcc2","type":"function","z":"a35bca0e4d8d389a","name":"Add","func":"msg.payload = msg.payload[0] + msg.payload[1]\nreturn msg","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1020,"y":230,"wires":[["899227b661a1fe87"]]},{"id":"899227b661a1fe87","type":"debug","z":"a35bca0e4d8d389a","name":"debug 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1180,"y":230,"wires":[]}]

1 Like

I have moved this to its own topic as this is really a new question with no relevance to the OG question. Please bear this in mind in future @bceyresy .

2 Likes

Yup, I posted right after you did, so deleted my post and wrote it here instead

2 Likes

Thanks Everyone, I will bear in mind for other topics.

All examples work great.

1 Like

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