# How to select one msg.payload out of 4 recieved

**URL:** <https://discourse.nodered.org/t/how-to-select-one-msg-payload-out-of-4-recieved/28355>\
**Category:** General\
**Created:** [15 June 2020 11:18 UTC](https://discourse.nodered.org/t/how-to-select-one-msg-payload-out-of-4-recieved/28355 "2020-06-15T11:18:55Z")\
**Posts on this page:** 4\
**Page:** 2

<div class="post-metadata">

**Author:** ![anthony\_dob](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/anthony_dob/32/23958_2.png) [@anthony\_dob](https://discourse.nodered.org/u/anthony_dob)\
**Post date:** [15 June 2020 13:14 UTC](https://discourse.nodered.org/t/how-to-select-one-msg-payload-out-of-4-recieved/28355/21 "2020-06-15T13:14:10Z")

</div>

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!

---

<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:** [15 June 2020 13:16 UTC](https://discourse.nodered.org/t/how-to-select-one-msg-payload-out-of-4-recieved/28355/22 "2020-06-15T13:16:43Z")

</div>

Good luck @anthony_dob.

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

Hear from you later.

---

<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:** [15 June 2020 22:03 UTC](https://discourse.nodered.org/t/how-to-select-one-msg-payload-out-of-4-recieved/28355/23 "2020-06-15T22:03:33Z")

</div>

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

 ![Screenshot from 2020-06-16 07-42-37](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/a/1a8bf344ab6839788bf7793fafb5d24f9776538a.png)

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:

```auto
[{"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:

```auto
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.

---

<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:** [14 August 2020 22:03 UTC](https://discourse.nodered.org/t/how-to-select-one-msg-payload-out-of-4-recieved/28355/24 "2020-08-14T22:03:45Z")

</div>

Closed on 14 August 2020 22:03 UTC

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

[Previous page](https://discourse.nodered.org/t/how-to-select-one-msg-payload-out-of-4-recieved/28355.md?page=1)
