# Function Node for Beginner

**URL:** https://discourse.nodered.org/t/function-node-for-beginner/32948
**Category:** General
**Created:** [16 September 2020 08:43 UTC](https://discourse.nodered.org/t/function-node-for-beginner/32948 "2020-09-16T08:43:42Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![Philip](https://avatars.discourse-cdn.com/v4/letter/p/96bed5/32.png) [@Philip](https://discourse.nodered.org/u/Philip)
#### Post date: [16 September 2020 08:43 UTC](https://discourse.nodered.org/t/function-node-for-beginner/32948/1 "2020-09-16T08:43:42Z")

</div>

Hi Guys  
I'm a total beginner to Node-Red with no programming experience.....I am looking to connect a DHT11 Temp Humidity Sensor with an Uno (Done) and represent on Two Graphs Node Red /ui. I am stuck with the Java code required in the function node to take the input "Temperature: \*.\*C or Humidity \*\*% and only output the actual value to the respective graphic node.  
Would any be able top point me in the right direction?  
Thank You  
Philip

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [16 September 2020 09:04 UTC](https://discourse.nodered.org/t/function-node-for-beginner/32948/2 "2020-09-16T09:04:33Z")

</div>

Feed the message containing the values into a debug node and show it what it says.  
By the way it is javascript not Java. Completely different but named so as to cause maximum confusion.

---

<div class="post-metadata">

### Author: ![greengolfer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/greengolfer/32/28276_2.png) [@greengolfer](https://discourse.nodered.org/u/greengolfer)
#### Post date: [16 September 2020 09:37 UTC](https://discourse.nodered.org/t/function-node-for-beginner/32948/3 "2020-09-16T09:37:04Z")

</div>

You need to decide first how the arduino will "talk" to NodeRed. MQTT? http endpoint? Other methods?  
Then on nodered, you need to put the "listen" part in place.  
You may not need to write any function. Just using the available nodes could do the trick. But, 1st things 1st, what is going out and how, from the Uno?

---

<div class="post-metadata">

### Author: ![Philip](https://avatars.discourse-cdn.com/v4/letter/p/96bed5/32.png) [@Philip](https://discourse.nodered.org/u/Philip)
#### Post date: [16 September 2020 10:33 UTC](https://discourse.nodered.org/t/function-node-for-beginner/32948/4 "2020-09-16T10:33:43Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/9/09d7e9ce7ec9da1fb1d88868e0f3c1f1d34acb28.jpeg)

This is what the debug node is showing in the "debug" pallett. I am inputting Uno serial (9600) into Pi using USB ......just passing msg.payload into debug node....what I want to do is split the temp & humidity into separate strands and attach a graph node for each?

---

<div class="post-metadata">

### Author: ![Philip](https://avatars.discourse-cdn.com/v4/letter/p/96bed5/32.png) [@Philip](https://discourse.nodered.org/u/Philip)
#### Post date: [16 September 2020 10:35 UTC](https://discourse.nodered.org/t/function-node-for-beginner/32948/5 "2020-09-16T10:35:25Z")

</div>

That sounds interesting! Is there an example of this I could take a look at?

---

<div class="post-metadata">

### Author: ![greengolfer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/greengolfer/32/28276_2.png) [@greengolfer](https://discourse.nodered.org/u/greengolfer)
#### Post date: [16 September 2020 11:23 UTC](https://discourse.nodered.org/t/function-node-for-beginner/32948/6 "2020-09-16T11:23:40Z")

</div>

First you would need to connect the uno to Ethernet (or wifi). You can search for "arduino ethernet shield".

Then for exemple, using MQTT, this guide could help: [http://www.steves-internet-guide.com/using-arduino-pubsub-mqtt-client/](http://www.steves-internet-guide.com/using-arduino-pubsub-mqtt-client/)

You would also need to run an MQTT broker (eg mosquitto) for NodeRed and the Uno to exchange messages.

Clearly, there are much more "components" (hard and soft) to use than just the Uno board but, it will give you so much more flexibility.

---

<div class="post-metadata">

### Author: ![Philip](https://avatars.discourse-cdn.com/v4/letter/p/96bed5/32.png) [@Philip](https://discourse.nodered.org/u/Philip)
#### Post date: [16 September 2020 11:31 UTC](https://discourse.nodered.org/t/function-node-for-beginner/32948/7 "2020-09-16T11:31:12Z")

</div>

I will need to source the parts for this but Thanks. Can you advise the syntax required in a function node to take input string from UNO and SELECT the "Temperature: 22.00C" message only .....then remove just select the value and output that to debug node? If I could get that done...would be a start? Thanks

---

<div class="post-metadata">

### Author: ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)
#### Post date: [16 September 2020 11:52 UTC](https://discourse.nodered.org/t/function-node-for-beginner/32948/8 "2020-09-16T11:52:39Z")

</div>

Hi Philip,

Although the best approach would be greengolfers recommendations, for now .. as is ..  
you can experiment with the javascript's [split()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split) command and maybe slice()

```auto
msg.payload = msg.payload.split(': ')
return msg;

```

---

<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: [16 September 2020 11:54 UTC](https://discourse.nodered.org/t/function-node-for-beginner/32948/9 "2020-09-16T11:54:57Z")

</div>

I’d suggest you also look at getting a WeMos D1 Mini and flashing it with ESPEasy. The Wemos has wifi and espeasy makes it easy to select the device and send the readings Via MQTT.

Oh and get some BMP/E280 to use instead of the dht11’s which are not very accurate

---

<div class="post-metadata">

### Author: ![greengolfer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/greengolfer/32/28276_2.png) [@greengolfer](https://discourse.nodered.org/u/greengolfer)
#### Post date: [16 September 2020 12:08 UTC](https://discourse.nodered.org/t/function-node-for-beginner/32948/10 "2020-09-16T12:08:23Z")

</div>

Here is an exemple without using the function node:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/2/22db6c00bb20237fb0634e6d4408b7773ff062d9.png)

With the code:

```auto
[{"id":"faf583b.7c0878","type":"inject","z":"33b1225f.3b50ee","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Temperature: 22.00°C","payloadType":"str","x":270,"y":2280,"wires":[["e7c6bd7f.044be"]]},{"id":"e7c6bd7f.044be","type":"change","z":"33b1225f.3b50ee","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.$substringBefore(\"°C\").$substringAfter(\"Temperature: \")\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":2280,"wires":[["f029984a.1f3e08"]]},{"id":"f029984a.1f3e08","type":"debug","z":"33b1225f.3b50ee","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":730,"y":2280,"wires":[]}]

```

---

<div class="post-metadata">

### Author: ![Philip](https://avatars.discourse-cdn.com/v4/letter/p/96bed5/32.png) [@Philip](https://discourse.nodered.org/u/Philip)
#### Post date: [16 September 2020 12:17 UTC](https://discourse.nodered.org/t/function-node-for-beginner/32948/11 "2020-09-16T12:17:44Z")

</div>

I understand that....and thank you!.....but I'm taking realtime data from a dht11 sensor as my flow input.....all I want to do for now is separate the Temperature value and the Humidity Value in payload so I can graph each on a Ui?

---

<div class="post-metadata">

### Author: ![greengolfer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/greengolfer/32/28276_2.png) [@greengolfer](https://discourse.nodered.org/u/greengolfer)
#### Post date: [16 September 2020 12:28 UTC](https://discourse.nodered.org/t/function-node-for-beginner/32948/12 "2020-09-16T12:28:29Z")

</div>

What about:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/a/3aa095b8996d941a19feee9be556f56fdf09ff21.png)

Code:

```auto
[{"id":"faf583b.7c0878","type":"inject","z":"33b1225f.3b50ee","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Temperature: 22.00°C","payloadType":"str","x":180,"y":2220,"wires":[["5dee92ea.fdae9c"]]},{"id":"e7c6bd7f.044be","type":"change","z":"33b1225f.3b50ee","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.$substringBefore(\"°C\").$substringAfter(\"Temperature: \")\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":540,"y":2220,"wires":[["f029984a.1f3e08"]]},{"id":"f029984a.1f3e08","type":"debug","z":"33b1225f.3b50ee","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":730,"y":2260,"wires":[]},{"id":"1c812656.a7cb7a","type":"inject","z":"33b1225f.3b50ee","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Humidity: 75%","payloadType":"str","x":180,"y":2300,"wires":[["5dee92ea.fdae9c"]]},{"id":"5dee92ea.fdae9c","type":"switch","z":"33b1225f.3b50ee","name":"","property":"payload","propertyType":"msg","rules":[{"t":"regex","v":"Temperature","vt":"str","case":false},{"t":"regex","v":"Humidity","vt":"str","case":false}],"checkall":"true","repair":false,"outputs":2,"x":370,"y":2260,"wires":[["e7c6bd7f.044be"],["22418e6e.e66db2"]]},{"id":"22418e6e.e66db2","type":"change","z":"33b1225f.3b50ee","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.$substringBefore(\"%\").$substringAfter(\"Humidity: \")\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":540,"y":2300,"wires":[["f029984a.1f3e08"]]}]

```

For the UI part, I don't know. Not using it. Sorry.

---

<div class="post-metadata">

### Author: ![Philip](https://avatars.discourse-cdn.com/v4/letter/p/96bed5/32.png) [@Philip](https://discourse.nodered.org/u/Philip)
#### Post date: [16 September 2020 12:29 UTC](https://discourse.nodered.org/t/function-node-for-beginner/32948/13 "2020-09-16T12:29:27Z")

</div>

let me try that. Thanks

---

<div class="post-metadata">

### Author: ![Philip](https://avatars.discourse-cdn.com/v4/letter/p/96bed5/32.png) [@Philip](https://discourse.nodered.org/u/Philip)
#### Post date: [16 September 2020 12:41 UTC](https://discourse.nodered.org/t/function-node-for-beginner/32948/14 "2020-09-16T12:41:43Z")

</div>

Using msg.payload.split.....my payload has been separated......how now do I select only Temp Value and connect that into graph node?

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

---

<div class="post-metadata">

### Author: ![greengolfer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/greengolfer/32/28276_2.png) [@greengolfer](https://discourse.nodered.org/u/greengolfer)
#### Post date: [16 September 2020 14:04 UTC](https://discourse.nodered.org/t/function-node-for-beginner/32948/15 "2020-09-16T14:04:52Z")

</div>

You may want to learn as well how to share flows and debug output... Have a look at [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

---

<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: [16 September 2020 14:15 UTC](https://discourse.nodered.org/t/function-node-for-beginner/32948/16 "2020-09-16T14:15:52Z")

</div>

If you have control of the output, I would strongly recommend you output JSON. That way, the properties and values are very very easy to access (no string splitting etc)..

Side note...  
Philip. Can I ask. Why do you keep taking photographs of your flows/debug output & not simple screenshots? Is the node-red installation not accessible on your PC browser where you make these posts? A sniping tool or greenshot or shared or similar is far better in both file size and clarity.

---

<div class="post-metadata">

### Author: ![Philip](https://avatars.discourse-cdn.com/v4/letter/p/96bed5/32.png) [@Philip](https://discourse.nodered.org/u/Philip)
#### Post date: [16 September 2020 14:58 UTC](https://discourse.nodered.org/t/function-node-for-beginner/32948/17 "2020-09-16T14:58:34Z")

</div>

Thanks Steve.......side note first......I'm running all from a Pi......and communicating here through Laptop.....not sure how to snip on Linux?

If you have any examples of using JSON for this task....I would very much appreciate?  
regards  
Philip

---

<div class="post-metadata">

### Author: ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)
#### Post date: [16 September 2020 15:59 UTC](https://discourse.nodered.org/t/function-node-for-beginner/32948/18 "2020-09-16T15:59:17Z")

</div>

There is a good introductory video on working with JSON  
[Working With JSON Data in Node Red](https://www.youtube.com/watch?v=24ZY3CEsiow)

If you can modify your Arduino code so it outputs JSON format. For example :

`"{"Temperature": 22.40, "Humidity": 60.50}"`

a JSON or function node can easily parse that JSON string which makes life much easier accessing individual values.

---

<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: [15 November 2020 15:59 UTC](https://discourse.nodered.org/t/function-node-for-beginner/32948/19 "2020-11-15T15:59:19Z")

</div>

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