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

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.

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 :grin:
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 :+1:t3:

[{"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"]]}]

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

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:

// ...
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 ... :wink:

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.

[{"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.

@rko, thank you so much!!!

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... :grin:
So thank you!!

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

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):

[{"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 :smiley:
(no animals being harmed by running this flow)

@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.

That we are!!! :grin:

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

I also found this 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!!

1 Like

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