How to pass msg value to switch topic field

Hi

I want to set Topic filed with special expression: {{{msg.prex}}}_test in the switch node, but it not work.
How can I fix it?

Thanks!!!

[{"id":"992e19b1.96722","type":"ui_switch","z":"7c707aa7.c06c4c","name":"loop","label":"loop","tooltip":"","group":"3fe9dd19.60393a","order":17,"width":3,"height":1,"passthru":true,"decouple":"false","topic":"{{{msg.prex}}}_test","topicType":"str","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","animate":false,"x":490,"y":320,"wires":[["d02d8b77.f0b2f"]]},{"id":"8bfd9b7c.e99098","type":"inject","z":"7c707aa7.c06c4c","name":"","props":[{"p":"prex","v":"prex","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":150,"y":320,"wires":[["992e19b1.96722"]]},{"id":"d02d8b77.f0b2f","type":"debug","z":"7c707aa7.c06c4c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"topic","targetType":"msg","statusVal":"","statusType":"auto","x":740,"y":320,"wires":[]},{"id":"3fe9dd19.60393a","type":"ui_group","name":"rule1","tab":"4679b587.ba0d2c","order":2,"disp":true,"width":7,"collapse":true},{"id":"4679b587.ba0d2c","type":"ui_tab","name":"场景一","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

Hi, in order to make code more readable and importable it is important to surround your code with three backticks
```
like this
```

You can edit and correct your post by clicking the pencil icon.

See this post for more details - How to share code or flow json

It doesnt work like that. Use JSONata or a template node or a function node instead

image

in a function...

msg.topic = msg.prex + "_test"
return msg;

as JSONata
image

in a template
image

Demo flow...

[{"id":"92707120.1714","type":"change","z":"336fad55.d74c02","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"prex & \"_test\"","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":598,"y":416,"wires":[["1c5263f1.b1721c"]]},{"id":"c7fdea45.89c018","type":"template","z":"336fad55.d74c02","name":"","field":"topic","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{{prex}}}_test","output":"str","x":588,"y":464,"wires":[["6c7353f4.8f266c"]]},{"id":"5b5aa9d3.ba3608","type":"inject","z":"336fad55.d74c02","name":"msg.prex = \"hello\"","props":[{"p":"prex","v":"hello","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":378,"y":368,"wires":[["c7fdea45.89c018","92707120.1714","ef35e9ea.91a7b8"]]},{"id":"1c5263f1.b1721c","type":"debug","z":"336fad55.d74c02","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"topic","targetType":"msg","statusVal":"topic","statusType":"auto","x":748,"y":416,"wires":[]},{"id":"6c7353f4.8f266c","type":"debug","z":"336fad55.d74c02","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"topic","targetType":"msg","statusVal":"topic","statusType":"auto","x":750,"y":464,"wires":[]},{"id":"ef35e9ea.91a7b8","type":"function","z":"336fad55.d74c02","name":"","func":"msg.topic = msg.prex + \"_test\"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":588,"y":368,"wires":[["30f0f154.562aee"]]},{"id":"30f0f154.562aee","type":"debug","z":"336fad55.d74c02","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"topic","targetType":"msg","statusVal":"topic","statusType":"auto","x":748,"y":368,"wires":[]}]

Thanks for your reply, I will try.

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