# How to Write a function?

**URL:** <https://discourse.nodered.org/t/how-to-write-a-function/62970>\
**Category:** General\
**Created:** [23 May 2022 15:46 UTC](https://discourse.nodered.org/t/how-to-write-a-function/62970 "2022-05-23T15:46:31Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![fredshortlv129bis](https://avatars.discourse-cdn.com/v4/letter/f/5fc32e/32.png) [@fredshortlv129bis](https://discourse.nodered.org/u/fredshortlv129bis)\
**Post date:** [23 May 2022 15:46 UTC](https://discourse.nodered.org/t/how-to-write-a-function/62970/1 "2022-05-23T15:46:31Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/8/f80f6db93ab5e649f737c9160e61843b09eafa46.jpeg)

---

<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:** [23 May 2022 15:57 UTC](https://discourse.nodered.org/t/how-to-write-a-function/62970/2 "2022-05-23T15:57:30Z")

</div>

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

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

```

this would return `4.7 304`

---

<div class="post-metadata">

**Author:** ![fredshortlv129bis](https://avatars.discourse-cdn.com/v4/letter/f/5fc32e/32.png) [@fredshortlv129bis](https://discourse.nodered.org/u/fredshortlv129bis)\
**Post date:** [23 May 2022 17:58 UTC](https://discourse.nodered.org/t/how-to-write-a-function/62970/3 "2022-05-23T17:58:34Z")

</div>

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!  
JDG  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/b/4b787337eb244466f27bfa879785bd2de8171cce.png)

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [23 May 2022 18:06 UTC](https://discourse.nodered.org/t/how-to-write-a-function/62970/4 "2022-05-23T18:06:04Z")

</div>

There’s a great page in the docs ([Working with messages : Node-RED](https://nodered.org/docs/user-guide/messages)) 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.

![BX00Cy7yHi](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bd1f333a9e043b061b39917c328b0094d3e52d30.gif)

I recommend watching this playlist: [Node-RED Essentials](https://www.youtube.com/playlist?list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6). 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.

---

<div class="post-metadata">

**Author:** ![fredshortlv129bis](https://avatars.discourse-cdn.com/v4/letter/f/5fc32e/32.png) [@fredshortlv129bis](https://discourse.nodered.org/u/fredshortlv129bis)\
**Post date:** [26 May 2022 18:21 UTC](https://discourse.nodered.org/t/how-to-write-a-function/62970/5 "2022-05-26T18:21:46Z")

</div>

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?  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/4/14d8641b4d43a17145fccad6db87415995b1a994.png)

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [26 May 2022 18:34 UTC](https://discourse.nodered.org/t/how-to-write-a-function/62970/6 "2022-05-26T18:34:45Z")

</div>

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

> **[JavaScript Tutorial](https://www.w3schools.com/js/default.asp)**
>
> W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

More about concatenation can be found here...

> **[JavaScript Concatenate Strings: A Guide](https://careerkarma.com/blog/javascript-concat-string/)**
>
> The JavaScript concat string method is used by programmers to merge two strings into one value. Learn about how the concat method works and how to use it in your JavaScript code on Career Karma.

---

<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:** [26 May 2022 18:47 UTC](https://discourse.nodered.org/t/how-to-write-a-function/62970/7 "2022-05-26T18:47:31Z")

</div>

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

> **[Basic math in JavaScript — numbers and operators - Learn web development | MDN](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/Math)**
>
> In this article we have covered the fundamental information you need to know about numbers in JavaScript, for now. You'll see numbers used again and again, all the way through your JavaScript learning, so it's a good idea to get this out of the way...

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](https://developer.mozilla.org/en-US/docs/Web/JavaScript)

---

<div class="post-metadata">

**Author:** ![fredshortlv129bis](https://avatars.discourse-cdn.com/v4/letter/f/5fc32e/32.png) [@fredshortlv129bis](https://discourse.nodered.org/u/fredshortlv129bis)\
**Post date:** [26 May 2022 19:20 UTC](https://discourse.nodered.org/t/how-to-write-a-function/62970/8 "2022-05-26T19:20:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [26 May 2022 20:24 UTC](https://discourse.nodered.org/t/how-to-write-a-function/62970/9 "2022-05-26T20:24:16Z")

</div>

> [@fredshortlv129bis](#):
>
> What is still not clear to me : is the syntax of the return payload JS or JSon

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.

> [@Object vs strings - extract a value from a split](https://discourse.nodered.org/t/object-vs-strings-extract-a-value-from-a-split/42112/2):
>
> You need to understand the differnece (and advantage) of using objects and properties in node-red (and JavaScript in general). For example, if you have an object, there is ZERO need to split strings etc. JSON stands for JavaScript Object Notation - meaning it is a string representation of a JS object - which is useful for transmitting between devices and across the internet (they all do strings right!). That is why JSON is so popular - because it is REALLY EASILY convert to/from object/string…

---

<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:** [9 June 2022 20:24 UTC](https://discourse.nodered.org/t/how-to-write-a-function/62970/10 "2022-06-09T20:24:21Z")

</div>

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