# How use the function node

**URL:** <https://discourse.nodered.org/t/how-use-the-function-node/72499>\
**Category:** Dashboard\
**Created:** [20 December 2022 23:34 UTC](https://discourse.nodered.org/t/how-use-the-function-node/72499 "2022-12-20T23:34:11Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Grecia01](https://avatars.discourse-cdn.com/v4/letter/g/a183cd/32.png) [@Grecia01](https://discourse.nodered.org/u/Grecia01)\
**Post date:** [20 December 2022 23:34 UTC](https://discourse.nodered.org/t/how-use-the-function-node/72499/1 "2022-12-20T23:34:12Z")

</div>

Hello, I need help to display a text in a temple node, I attach my flowchart and how I have organized the nodes in the flow.  
In the function node c I get values ​​of sensor signals and alarm and pre-alarm constants, what happens in that node is a simple subtraction, just like in the function node h. And this is where I have the problem when comparing them in my 'CONCATENATE' function, it doesn't show me when I have two alerts. Just show me one. I am a beginner in node red.

 ![flow](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/f/7f94e48ea9663fee72a22662bfcd3bb09e6735ad.png)  
 ![node](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/8/887289e2695137717b322b647157e07a75d02f5f.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/b/6b6907f8e00f20006e5976401f75a26ee7a65a8e.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/3/834ba6335d8d31d5e75b2965e6d1b2899ba4e4a6.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/e/ce4d65978d1fee881b14d87f41cd4780238d6e66.png)

---

<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:** [21 December 2022 00:19 UTC](https://discourse.nodered.org/t/how-use-the-function-node/72499/2 "2022-12-21T00:19:25Z")

</div>

Hi

One important point to know is that messages never travel down the wires at the same time. Your function has two wires & therefore 2 messages will arrive at different times.

You can use a join node to join two or more message (canned text below) however in your case here, the better solution is to write everything in series & use different msg properties. E.g func1 outputs result in `msg.payload1`, passes that msg to func2 which puts it's value into `msg.payload2` which then gets passed to func3 where it has access to `msg.payload2` & `msg.payload1` at the same time (in the same msg)

* * *

Canned text...

See [this article in the cookbook](https://cookbook.nodered.org/basic/join-streams) for an example of how to join messages into one object.

---

<div class="post-metadata">

**Author:** ![Grecia01](https://avatars.discourse-cdn.com/v4/letter/g/a183cd/32.png) [@Grecia01](https://discourse.nodered.org/u/Grecia01)\
**Post date:** [21 December 2022 00:29 UTC](https://discourse.nodered.org/t/how-use-the-function-node/72499/3 "2022-12-21T00:29:26Z")

</div>

![Annotation 2022-12-20 182837](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/c/5c4ef78d1b8ecfd3e78b530796e576432d710283.png)  
I have a question, the declaration of 'fh' and 'fc' are well matched with msg.payload?  
because so with my test values ​​it is supposed to send the alert where 1 and 2 are, however it does not send that.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [21 December 2022 01:25 UTC](https://discourse.nodered.org/t/how-use-the-function-node/72499/4 "2022-12-21T01:25:06Z")

</div>

The important thing to realise is that your function "CONCATENAR" will operate on the messages from "fuction c" and "fuction h" _totally independently_.  
It does not have access to both payloads simultaneously.

> [@Grecia01](#):
>
> I have a question, the declaration of 'fh' and 'fc' are well matched with msg.payload?

fh and fc are _identical_. They _both_ equal the payload from fuction h at one moment, and they _both_ equal the payload from fuction c at another moment.

@Steve-Mcl has described how you can make the two payloads available in a single message so you can do your comparisons.

Edit - In one of your functions, I presume it is "fuction h", you set **msg.payload.hous** to either 2 or 0, but in CONCATENAR you set **fh = msg.payload** and then test if it == 1 or 2.  
{hous: 2} is an object, 2 is a number. They can never be equal.

---

<div class="post-metadata">

**Author:** ![Grecia01](https://avatars.discourse-cdn.com/v4/letter/g/a183cd/32.png) [@Grecia01](https://discourse.nodered.org/u/Grecia01)\
**Post date:** [21 December 2022 01:58 UTC](https://discourse.nodered.org/t/how-use-the-function-node/72499/5 "2022-12-21T01:58:30Z")

</div>

But how would I do the condition using the join node? if they would be separated from my 'Concatenate' node

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

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [21 December 2022 02:16 UTC](https://discourse.nodered.org/t/how-use-the-function-node/72499/6 "2022-12-21T02:16:32Z")

</div>

Take a look at this flow.  
After the join there is a single message in which **msg.payload.fh == 2** and **msg.payload.fc == 0**

 ![Untitled 2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/5/b5f0712377f3ebbe9d5f7a4b08724dffc651d8da.jpeg)

```auto
[{"id":"cb223579aad92683","type":"join","z":"947cea7bd1d96252","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":770,"y":160,"wires":[["f891feb32cadaf79"]]},{"id":"c0be4639e49854a2","type":"inject","z":"947cea7bd1d96252","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":280,"y":160,"wires":[["9c0198ea99de86d1","c0534cf69da384ae"]]},{"id":"9c0198ea99de86d1","type":"function","z":"947cea7bd1d96252","name":"Set msg.payload = 2, msg.topic = fh","func":"msg.payload = 2\nmsg.topic = \"fh\"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":520,"y":140,"wires":[["cb223579aad92683"]]},{"id":"c0534cf69da384ae","type":"function","z":"947cea7bd1d96252","name":"Set msg.payload = 2, msg.topic = fh","func":"msg.payload = 0\nmsg.topic = \"fc\"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":520,"y":180,"wires":[["cb223579aad92683"]]},{"id":"f891feb32cadaf79","type":"debug","z":"947cea7bd1d96252","name":"debug 79","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":920,"y":160,"wires":[]}]

```

There is a [series of short videos](https://www.youtube.com/watch?v=ksGeUD26Mw0&list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6) that might be worth watching, though of course the narration is all in English.

---

<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:** [21 December 2022 11:41 UTC](https://discourse.nodered.org/t/how-use-the-function-node/72499/7 "2022-12-21T11:41:32Z")

</div>

Better to do them in series, as suggested by @Steve-Mcl, or possibly even better do it all in one function node. There is no point splitting the flow and then going to the effort of joining it back together again.

---

<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:** [20 January 2023 11:42 UTC](https://discourse.nodered.org/t/how-use-the-function-node/72499/8 "2023-01-20T11:42:22Z")

</div>

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