# Msg.topic = "X" in a if/else function

**URL:** https://discourse.nodered.org/t/msg-topic-x-in-a-if-else-function/49344
**Category:** General
**Created:** [4 August 2021 19:36 UTC](https://discourse.nodered.org/t/msg-topic-x-in-a-if-else-function/49344 "2021-08-04T19:36:56Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![finch6](https://avatars.discourse-cdn.com/v4/letter/f/9d8465/32.png) [@finch6](https://discourse.nodered.org/u/finch6)
#### Post date: [4 August 2021 19:36 UTC](https://discourse.nodered.org/t/msg-topic-x-in-a-if-else-function/49344/1 "2021-08-04T19:36:56Z")

</div>

Hi all,

I'm having trouble with a flow I'm trying to create - I'm sure there is an easier way of doing this whole flow, if so, please let me know...

I have a "collection" of a few pictures that I want to send to a telegram bot in a random order triggered by an MQTT message.  
Attached to these pictures, as captions, I want to have text - also attached randomly.  
The format of the last send function node is correct for the Telegram node.

 ![Screenshot 2021-08-04 at 21.35.05](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/3/131de26bce008f98c420de13a17cd202483fd1c3.png)

The issue I'm having is with the "Caption Picker" function node. I tried using `msg.topic = "Caption"` so the join node could get the info, but I can't seem to get it right... I'm assuming the if/else function node isn't formatted correctly or something is not allowing the `msg.topic = "Caption"` to be sent on...

I want different combinations of picture/text for the messages - as not to have the same old picture/text combo all the time. What would be great would be a random node that output predefined text 😁  
I did search the Node Library, but couldn't find anything...

Like I say, if I can do this in a simpler way please let me know.  
Thanks in advance everyone 👍🏼

```auto
[{"id":"6ec407e95e9dbbb3","type":"group","z":"090649e01c38995f","name":"Example Flow","style":{"stroke":"#92d04f","fill":"#e3f3d3","fill-opacity":"0.3","label":true,"label-position":"ne","color":"#000000"},"nodes":["7c766c69e31fb170","3ff9f1d47969adc9","369030915ece8ff4","973be7a50f93fb9b","35c3ee8b9adcf61a","8e625554fa800735","81931ab6a02d4695","ae4540e4f1533d28","cbd338ce86a850c1"],"x":44,"y":319,"w":912,"h":192},{"id":"7c766c69e31fb170","type":"random","z":"090649e01c38995f","g":"6ec407e95e9dbbb3","name":"","low":1,"high":"5","inte":"true","property":"payload","x":300,"y":390,"wires":[["8e625554fa800735"]]},{"id":"3ff9f1d47969adc9","type":"random","z":"090649e01c38995f","g":"6ec407e95e9dbbb3","name":"","low":1,"high":"5","inte":"true","property":"payload","x":300,"y":430,"wires":[["cbd338ce86a850c1"]]},{"id":"369030915ece8ff4","type":"inject","z":"090649e01c38995f","g":"6ec407e95e9dbbb3","name":"","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":140,"y":410,"wires":[["3ff9f1d47969adc9","7c766c69e31fb170"]]},{"id":"973be7a50f93fb9b","type":"debug","z":"090649e01c38995f","g":"6ec407e95e9dbbb3","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":820,"y":470,"wires":[]},{"id":"35c3ee8b9adcf61a","type":"join","z":"090649e01c38995f","g":"6ec407e95e9dbbb3","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"2","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"num","reduceFixup":"","x":500,"y":410,"wires":[["81931ab6a02d4695"]]},{"id":"8e625554fa800735","type":"function","z":"090649e01c38995f","g":"6ec407e95e9dbbb3","name":"","func":"msg.topic = \"Photo\"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":440,"y":360,"wires":[["35c3ee8b9adcf61a"]]},{"id":"81931ab6a02d4695","type":"function","z":"090649e01c38995f","g":"6ec407e95e9dbbb3","name":"Send","func":"msg.method = \"sendPhoto\";\nmsg.payload = {\n photo: \"/home/pi/Picture\" + msg.payload.Photo + \".png\",\n caption: msg.payload.Caption\n};\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":630,"y":410,"wires":[["973be7a50f93fb9b","ae4540e4f1533d28"]]},{"id":"ae4540e4f1533d28","type":"telegrambot-payload","z":"090649e01c38995f","g":"6ec407e95e9dbbb3","name":"","bot":"","chatId":"","sendMethod":"","payload":"","x":840,"y":410,"wires":[[]]},{"id":"cbd338ce86a850c1","type":"function","z":"090649e01c38995f","g":"6ec407e95e9dbbb3","name":"Caption Picker","func":"msg.topic = \"Caption\"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":460,"y":460,"wires":[["35c3ee8b9adcf61a"]]},{"id":"f763a52cb615f42a","type":"group","z":"090649e01c38995f","name":"Problem Flow","style":{"stroke":"#ff0000","fill":"#ffbfbf","fill-opacity":"0.3","label":true,"label-position":"ne","color":"#000000"},"nodes":["bdd3c2f42db2cd92","af7dcb1b24182bb8","8b4b33c4fd93ee02","43f40d863406bdc2","bfe136ab82242bcf","edfac785d9ae747f","9ba20c469316b028","2fb5d1d37eae0de2","41fdbacc8728e3a5"],"x":44,"y":129,"w":912,"h":192},{"id":"bdd3c2f42db2cd92","type":"random","z":"090649e01c38995f","g":"f763a52cb615f42a","name":"","low":1,"high":"5","inte":"true","property":"payload","x":300,"y":200,"wires":[["edfac785d9ae747f"]]},{"id":"af7dcb1b24182bb8","type":"random","z":"090649e01c38995f","g":"f763a52cb615f42a","name":"","low":1,"high":"5","inte":"true","property":"payload","x":300,"y":240,"wires":[["41fdbacc8728e3a5"]]},{"id":"8b4b33c4fd93ee02","type":"inject","z":"090649e01c38995f","g":"f763a52cb615f42a","name":"","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":140,"y":220,"wires":[["af7dcb1b24182bb8","bdd3c2f42db2cd92"]]},{"id":"43f40d863406bdc2","type":"debug","z":"090649e01c38995f","g":"f763a52cb615f42a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":820,"y":280,"wires":[]},{"id":"bfe136ab82242bcf","type":"join","z":"090649e01c38995f","g":"f763a52cb615f42a","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"2","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"num","reduceFixup":"","x":500,"y":220,"wires":[["9ba20c469316b028"]]},{"id":"edfac785d9ae747f","type":"function","z":"090649e01c38995f","g":"f763a52cb615f42a","name":"","func":"msg.topic = \"Photo\"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":440,"y":170,"wires":[["bfe136ab82242bcf"]]},{"id":"9ba20c469316b028","type":"function","z":"090649e01c38995f","g":"f763a52cb615f42a","name":"Send","func":"msg.method = \"sendPhoto\";\nmsg.payload = {\n photo: \"/home/pi/Picture\" + msg.payload.Photo + \".png\",\n caption: msg.payload.Caption\n};\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":630,"y":220,"wires":[["43f40d863406bdc2","2fb5d1d37eae0de2"]]},{"id":"2fb5d1d37eae0de2","type":"telegrambot-payload","z":"090649e01c38995f","g":"f763a52cb615f42a","name":"","bot":"","chatId":"","sendMethod":"","payload":"","x":840,"y":220,"wires":[[]]},{"id":"41fdbacc8728e3a5","type":"function","z":"090649e01c38995f","g":"f763a52cb615f42a","name":"Caption Picker","func":"msg.topic = \"Caption\"\n\nvar msg1 = \"Text 1\"\nvar msg2 = \"Text 2\"\nvar msg3 = \"Text 3\"\nvar msg4 = \"Text 4\"\n\nif (msg.payload == \"1\") {\n return {payload: msg1}\n} else if (msg.payload == \"2\") {\n return {payload: msg2}\n} else if (msg.payload == \"3\") {\n return {payload: msg3}\n} else {\n return {payload:msg4}\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":460,"y":270,"wires":[["bfe136ab82242bcf"]]}]

```

---

<div class="post-metadata">

### Author: ![rko](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rko/32/45807_2.png) [@rko](https://discourse.nodered.org/u/rko)
#### Post date: [4 August 2021 19:58 UTC](https://discourse.nodered.org/t/msg-topic-x-in-a-if-else-function/49344/2 "2021-08-04T19:58:51Z")

</div>

I think you forgot to add the topic in your `Caption Picker` function. Try something like

```auto
return {payload: msg1, topic: msg.topic }

```

However, you could just use the `msg` and add the needed payload to it instead of creating new `msg` objects:

```auto
// ...
if (msg.payload == "1") {
   msg.payload = msg1;
}
// ...
return msg;

```

Maybe the whole thing can be done easier and more flexible .. but if it works for you ... 😉

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [4 August 2021 22:01 UTC](https://discourse.nodered.org/t/msg-topic-x-in-a-if-else-function/49344/3 "2021-08-04T22:01:22Z")

</div>

> [@finch6](#):
>
> What would be great would be a random node that output predefined text 😁

You could define arrays of pics and captions, even load them from a text file if you wanted.

Here is a simple example using a change node. i define the captions and photos there, but you could also feed the predefined payloads in.

```auto
[{"id":"e79fda72.686a5","type":"change","z":"9af9141769558c7a","name":"","rules":[{"t":"set","p":"method","pt":"msg","to":"sendPhoto","tot":"str"},{"t":"set","p":"payload.photos","pt":"msg","to":"[\"1\",\"2\",\"3\",\"4\",\"5\"]","tot":"json"},{"t":"set","p":"payload.captions","pt":"msg","to":"[\"cap1\",\"cap2\",\"cap3\",\"cap4\",\"cap5\"]","tot":"json"},{"t":"set","p":"payload","pt":"msg","to":"($caption := $floor($random()*$count($$.payload.captions));\t$photo := $floor($random()*$count($$.payload.photos));\t{\t \"photo\": \"/home/pi/picture\" & $$.payload.photos[$photo] & \".png\",\t \"caption\": $$.payload.captions[$caption]\t})","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":360,"wires":[["2a2c5e30.85120a"]]},{"id":"d5554526.e01d88","type":"inject","z":"9af9141769558c7a","name":"","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":210,"y":360,"wires":[["e79fda72.686a5"]]},{"id":"2a2c5e30.85120a","type":"debug","z":"9af9141769558c7a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":550,"y":360,"wires":[]}]

```

The final JSONata expression will output the formatted payload. The input arrays can be any length.

---

<div class="post-metadata">

### Author: ![finch6](https://avatars.discourse-cdn.com/v4/letter/f/9d8465/32.png) [@finch6](https://discourse.nodered.org/u/finch6)
#### Post date: [4 August 2021 22:12 UTC](https://discourse.nodered.org/t/msg-topic-x-in-a-if-else-function/49344/4 "2021-08-04T22:12:31Z")

</div>

@rko, thank you so much!!!

> [@rko](#):
>
> However, you could just use the `msg` and add the needed payload to it instead of creating new `msg` objects:
> 
> ```auto
> // ...
> if (msg.payload == "1") {
> msg.payload = msg1;
> }
> // ...
> return msg;
> 
> ```

This worked like a charm!!!

I think something in my _IF/ELSE_ was "kicking" the `msg.topic = "Caption"` out... At least I think (with my VERY limited knowledge!) But you just showed that a _IF/ELSE_ wasn't necessary, just a _IF_!!!

I understand there are MANY ways to skin a cat...! And I like learning new ways... 😁  
So thank you!!

---

<div class="post-metadata">

### Author: ![finch6](https://avatars.discourse-cdn.com/v4/letter/f/9d8465/32.png) [@finch6](https://discourse.nodered.org/u/finch6)
#### Post date: [4 August 2021 22:14 UTC](https://discourse.nodered.org/t/msg-topic-x-in-a-if-else-function/49344/5 "2021-08-04T22:14:34Z")

</div>

@E1cid, I like the look of this too 😀  
Looks complicated but when you break it down slowly it isn't... Definitely going to explore this avenue too. Thanks so much!!! 😀

---

<div class="post-metadata">

### Author: ![rko](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rko/32/45807_2.png) [@rko](https://discourse.nodered.org/u/rko)
#### Post date: [4 August 2021 22:32 UTC](https://discourse.nodered.org/t/msg-topic-x-in-a-if-else-function/49344/6 "2021-08-04T22:32:13Z")

</div>

You're very welcome.

The `msg.topic` is actually being kicked out because by using `return {payload: msg1}` you are sending a new message object which only contains a payload variable which is set to msg1.

Have a look at this slightly improved version (similar to what @E1cid suggested):

```auto
[{"id":"095d57786dc5028b","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"f763a52cb615f42a","type":"group","z":"095d57786dc5028b","name":"Problem Flow","style":{"stroke":"#ff0000","fill":"#ffbfbf","fill-opacity":"0.3","label":true,"label-position":"ne","color":"#000000"},"nodes":["bdd3c2f42db2cd92","af7dcb1b24182bb8","8b4b33c4fd93ee02","43f40d863406bdc2","bfe136ab82242bcf","edfac785d9ae747f","9ba20c469316b028","41fdbacc8728e3a5"],"x":284,"y":209,"w":892,"h":192},{"id":"bdd3c2f42db2cd92","type":"random","z":"095d57786dc5028b","g":"f763a52cb615f42a","name":"","low":1,"high":"5","inte":"true","property":"payload","x":540,"y":280,"wires":[["edfac785d9ae747f"]]},{"id":"af7dcb1b24182bb8","type":"random","z":"095d57786dc5028b","g":"f763a52cb615f42a","name":"","low":"0","high":"3","inte":"true","property":"payload","x":540,"y":320,"wires":[["41fdbacc8728e3a5"]]},{"id":"8b4b33c4fd93ee02","type":"inject","z":"095d57786dc5028b","g":"f763a52cb615f42a","name":"","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":380,"y":300,"wires":[["af7dcb1b24182bb8","bdd3c2f42db2cd92"]]},{"id":"43f40d863406bdc2","type":"debug","z":"095d57786dc5028b","g":"f763a52cb615f42a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1060,"y":360,"wires":[]},{"id":"bfe136ab82242bcf","type":"join","z":"095d57786dc5028b","g":"f763a52cb615f42a","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"2","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"num","reduceFixup":"","x":740,"y":300,"wires":[["9ba20c469316b028"]]},{"id":"edfac785d9ae747f","type":"function","z":"095d57786dc5028b","g":"f763a52cb615f42a","name":"","func":"msg.topic = \"Photo\"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":680,"y":250,"wires":[["bfe136ab82242bcf"]]},{"id":"9ba20c469316b028","type":"function","z":"095d57786dc5028b","g":"f763a52cb615f42a","name":"Send","func":"msg.method = \"sendPhoto\";\nmsg.payload = {\n photo: \"/home/pi/Picture\" + msg.payload.Photo + \".png\",\n caption: msg.payload.Caption\n};\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":870,"y":300,"wires":[["43f40d863406bdc2"]]},{"id":"41fdbacc8728e3a5","type":"function","z":"095d57786dc5028b","g":"f763a52cb615f42a","name":"Caption Picker","func":"msg.topic = \"Caption\"\nvar texts = [\"Text 1\", \"Text 2\", \"Text 3\", \"Text 4\"];\nmsg.payload = texts[msg.payload];\nreturn msg;\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":700,"y":350,"wires":[["bfe136ab82242bcf"]]}]

```

The random number is treated as index of an array which contains your texts.

The cat is not ~~skinny~~ skinned yet but we're getting there 😃  
(no animals being harmed by running this flow)

---

<div class="post-metadata">

### Author: ![finch6](https://avatars.discourse-cdn.com/v4/letter/f/9d8465/32.png) [@finch6](https://discourse.nodered.org/u/finch6)
#### Post date: [4 August 2021 23:07 UTC](https://discourse.nodered.org/t/msg-topic-x-in-a-if-else-function/49344/7 "2021-08-04T23:07:04Z")

</div>

@rko, I will take a look in the morning..

I now understand why the `msg.topic` was being omitted, makes perfect sense when it's explained.

Looking forward to seeing your flow, will check it out tomorrow.

> [@rko](#):
>
> The cat is not ~~skinny~~ skinned yet but we're getting there 😃

That we are!!! 😁

---

<div class="post-metadata">

### Author: ![finch6](https://avatars.discourse-cdn.com/v4/letter/f/9d8465/32.png) [@finch6](https://discourse.nodered.org/u/finch6)
#### Post date: [5 August 2021 12:00 UTC](https://discourse.nodered.org/t/msg-topic-x-in-a-if-else-function/49344/8 "2021-08-05T12:00:26Z")

</div>

@rko, thanks so much... I just had a look at your flow above!  
Everything is so much clearer wjhen you have direction 😂😂😂

I also found this node - [random-generator\_node-red-contrib](https://flows.nodered.org/node/random-generator_node-red-contrib) - that I could have used... But now I have 3 different ways of doing the same thing..! And I bet I'll use each one in a different project in the future, so thank you @rko & @E1cid, I appreciate it greatly..!

Stay safe everyone!!

---

<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: [4 October 2021 12:00 UTC](https://discourse.nodered.org/t/msg-topic-x-in-a-if-else-function/49344/9 "2021-10-04T12:00:33Z")

</div>

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