How to select one msg.payload out of 4 recieved

That sounds very hard for now. I just started out with node-red so glad i could get this to work with your guys help! Maybe at some point i could make this the way you say. Thanks a lot!

Good luck @anthony_dob.

Don't worry. It is a lot of small steps.

Hear from you later.

Here is my take on what @Colin was meaning but a bit more broken down into nodes.

What it does:
The switch nodes splits the incoming message into 4 messages depending on their button.
(Following the first one for button 1)
The change node (Labelled as topic) sets the topic of the message to the respective button name.

You can see to the right the two examples where button1 and 2 have been pressed and released.

Note the button numbers "circled". That is the message's topic.

Now, rather than do what you did with the 4 MQTT OUT nodes where you set their topics, this is already done. Granted not to what you have: VIVES/IOT/AD/Button. But a quick edit of the change node and this can be easily done.

I will modify the code before I post it (below) to do just that.

Here is the code:

[{"id":"8dd4a8b0.631a68","type":"switch","z":"b9924a74.4d98f8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"button 1","vt":"str"},{"t":"cont","v":"button 2","vt":"str"},{"t":"cont","v":"button 3","vt":"str"},{"t":"cont","v":"button 4","vt":"str"}],"checkall":"true","repair":false,"outputs":4,"x":430,"y":3830,"wires":[["139ee127.31729f"],["b6698e2f.224438"],[],[]]},{"id":"467f5727.31621","type":"inject","z":"b9924a74.4d98f8","name":"Button 1 pressed","topic":"","payload":"button 1 pressed","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":3760,"wires":[["8dd4a8b0.631a68"]]},{"id":"e6104c46.8d1178","type":"inject","z":"b9924a74.4d98f8","name":"Button 1 released","topic":"","payload":"button 1 released","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":3800,"wires":[["8dd4a8b0.631a68"]]},{"id":"139ee127.31729f","type":"change","z":"b9924a74.4d98f8","name":"Topic","rules":[{"t":"set","p":"topic","pt":"msg","to":"VIVES/IOT/AD/Button1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":590,"y":3770,"wires":[["22c88f1e.d9f058"]]},{"id":"26884217.d92e06","type":"inject","z":"b9924a74.4d98f8","name":"Button 2 pressed","topic":"","payload":"button 2 pressed","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":3840,"wires":[["8dd4a8b0.631a68"]]},{"id":"c592b4a.2525e48","type":"inject","z":"b9924a74.4d98f8","name":"Button 2 released","topic":"","payload":"button 2 released","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":3880,"wires":[["8dd4a8b0.631a68"]]},{"id":"b6698e2f.224438","type":"change","z":"b9924a74.4d98f8","name":"Topic","rules":[{"t":"set","p":"topic","pt":"msg","to":"VIVES/IOT/AD/Button2","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":590,"y":3810,"wires":[["7633491f.bcdaa"]]},{"id":"22c88f1e.d9f058","type":"debug","z":"b9924a74.4d98f8","name":"Button 1 state","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":760,"y":3770,"wires":[]},{"id":"7633491f.bcdaa","type":"debug","z":"b9924a74.4d98f8","name":"Button 2 state","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":760,"y":3810,"wires":[]}]

Now, all this can be done in one node (as @Colin said). But for now I am keeping it simple and basic so you can understand what is going on and why.

Now, as great as this is for you, I shall again digress to my second suggestion where you put the topic in the arduino code.

Which would be something like this:

void SendMQTTsw1()
{
    if(sw1){pc.printf({msg.payload:"button 1 is uit\n",msg.topic:"VIVES/IOT/AD/Button1"});}
    else   {pc.printf({msg.payload:"button 1 is aan\n",msg.topic:"VIVES/IOT/AD/Button1"});} 
}

Others can help you with the exact syntax.

But what you would do is put a debug node directly on the COM4 node, open it and get it to show the complete message body. (It would work if you left it as default. You just need to check that small bit which I showed as the topic part in its output.)

Then - all you would do is connect the COM4 node to a MQTT OUT node.

That's it. Nothing to set, other than the broker IP address.

This is both good and bad.

Good: Less nodes used. 2 in total.
Bad: You don't learn as much about node-red now. But there is always later.

There is no right and wrong way to do it. Which ever way you want.

I showed you a couple of ways. @Colin suggested another.

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