How to Write a function?

I am trying Node-Red for the fourth time.
How do I write the function so that the msg return the wind direction and windspeed.

You already have both
the speed is msg.payload.windspeed and direction is msg.payload.winddirection

If you want them returned as a string (guessing as you give no idea how you wish them returned) you would do something like

msg.payload = msg.payload.windspeed + " " + msg.payload.winddirection;
return msg;

this would return 4.7 304

What a quick service! Thank you, thank you, thank you.
I have been looking for it for a whole week and have looked up many guides, but there are no such simple examples.
I didn't dare ask such a simple question at first.
Thank you for your understanding.
And it works, eureka!

There’s a great page in the docs (Working with messages : Node-RED) that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.


I recommend watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.


Indeed, and thank you for your many star commendation.
In a previous life I read a whole series of the Essentials but I reread them; their existence had escaped me in the meantime.
In E1cid's reply, the mention of "+" was crucial for me. I need to know how the payload is built up; for example, how is the (windspeed * 60 / 1000 = km/h) rounded to the unit? Where can I find these teachings?

What @E1cid has given you is a simple concatenation of two values separated with a "space" (as per your original question) using JavaScript.

If you want to study JavaScript further, this is a good resource...

More about concatenation can be found here...

This resource should help, worth a read for you.
Plenty of simple examples including the second using toFixed()

something like
msg.payload = (msg.payload.windspeed * 60 / 1000).toFixed(2) + " = kmp/h";

Once you crack the syntax things will become easier, try a few online beginners tutorials.

Javascript reference resource

Thank you.
I have read and practiced this several times, but I have not come across what E1cid has shown me in more than a week lecture. Indeed the site is exemplary.
What is still not clear to me : is the syntax of the return payload JS or JSon?
The good Lord will have to give me a few more years.

JSON stands for JavaScript Object Notation

Read this post I wrote some time back - I think it's fairly clear for folk new to all this.

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