Read msg.topic?

Hello ! ! !

I will read the Tobic of an Message. This is in my Function Node

var topic = msg.topic;
return topic;

Alway comes in Debug Node

Function tried to send a message of type string

Where ist the Problem ?

Greets
Malcom

Hi @malcomxcia

A Function node must return a message object. It cannot return a simple String or Number type.

Your function is returning the value of msg.topic - which is not a message object, so this isn't a valid thing to do.

What do you want to do with msg.topic?

Thank you for Answer :wink:

I will testing to choose on an Dropdown where in the Value the topic of Lamps. I will write this Value to the Topic and in ..... next Step then use it to change with a Color Picker the color of the selected lamp.

[{"id":"3846eb1f.abe4c4","type":"ui_dropdown","z":"7b7476ef.c490a8","name":"","label":"","tooltip":"","place":"Select option","group":"410b79ee.be3948","order":10,"width":0,"height":0,"passthru":true,"options":[{"label":"Lampe Neu","value":"cmnd/lampe_neu_1/color","type":"str"},{"label":"LED PC","value":"cmnd/led_stripe_pc/color","type":"str"}],"payload":"","topic":"test","x":120,"y":140,"wires":[["de830a35.ab6ac8","4c99cab5.9948d4"]]},{"id":"4c99cab5.9948d4","type":"function","z":"7b7476ef.c490a8","name":"","func":"var topic=msg.topic;\nreturn topic;","outputs":1,"noerr":0,"x":630,"y":400,"wires":[["9e3564eb.f6e0f8"]]},{"id":"410b79ee.be3948","type":"ui_group","z":"","name":"TESTPANEL","tab":"5486a51c.17010c","order":1,"disp":true,"width":"6","collapse":false},{"id":"5486a51c.17010c","type":"ui_tab","z":"","name":"TESTPANEL","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

The Tobic on the Dropdown for Testing is TEST and then it should give me the topic ?

Sorry im Newcomer an allways sorry for my English :smiley:

Malcom

Step 1: Add some debug nodes to your flow so you can see the data. (set the to Output: Complete Message Object )

It will give you the topic variable within the function. But you cannot return a variable you have to return an object.

Step 2 : Read the documentation about working with messages.

[{"id":"310be48a.063bdc","type":"ui_dropdown","z":"e4ccc526.618838","name":"","label":"","place":"Select option","group":"673daf99.c8ffb8","order":10,"width":0,"height":0,"passthru":true,"options":[{"label":"Lampe Neu","value":"cmnd/lampe_neu_1/color","type":"str"},{"label":"LED PC","value":"cmnd/led_stripe_pc/color","type":"str"}],"payload":"","topic":"test","x":134,"y":114,"wires":[["8a8387d.d620af8","b3466b68.2e8d18"]]},{"id":"8a8387d.d620af8","type":"function","z":"e4ccc526.618838","name":"","func":"msg.payload = \"Output selected:\" + msg.topic;\nreturn msg;","outputs":1,"noerr":0,"x":418,"y":199,"wires":[["cdfa3a1.b9eaac8"]]},{"id":"cdfa3a1.b9eaac8","type":"debug","z":"e4ccc526.618838","name":"Function Output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":640.5,"y":218,"wires":[]},{"id":"b3466b68.2e8d18","type":"debug","z":"e4ccc526.618838","name":"dropdown output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":408.5,"y":73,"wires":[]},{"id":"673daf99.c8ffb8","type":"ui_group","z":"","name":"TESTPANEL","tab":"f49a16d9.2d5f8","order":1,"disp":true,"width":"6","collapse":false},{"id":"f49a16d9.2d5f8","type":"ui_tab","z":"","name":"TESTPANEL","icon":"dashboard","order":1}]

Thank you for your explanation.

Okay, now I get it. You can't assign an msg object to a normal variable. Should be logical and sorry :wink:

Is it possible that I use the Topic from the Function Node in the Color Picker to further use it ?

For example, with a flow variable or what ever ?

Have a nice Weekend and Thank you again
Malcom