# Function node. How to refer to several messages?

**URL:** https://discourse.nodered.org/t/function-node-how-to-refer-to-several-messages/58759
**Category:** General
**Tags:** function-node
**Created:** [21 February 2022 16:48 UTC](https://discourse.nodered.org/t/function-node-how-to-refer-to-several-messages/58759 "2022-02-21T16:48:52Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![WBr](https://avatars.discourse-cdn.com/v4/letter/w/b3f665/32.png) [@WBr](https://discourse.nodered.org/u/WBr)
#### Post date: [21 February 2022 16:48 UTC](https://discourse.nodered.org/t/function-node-how-to-refer-to-several-messages/58759/1 "2022-02-21T16:48:52Z")

</div>

I am looking for a method to use the function node to do some math with. There are multiple sensors in my flow providing me with data.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/b/0bca241d9ae0b7034623020c9dce4660974a9e6f.png)

The way I would like to do this is to grab 1 message from a sensor. Grab another message from another sensor (sensor 2) and then subtract value sensor 2 from value sensor 1 with only using the function node

---

<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 February 2022 16:53 UTC](https://discourse.nodered.org/t/function-node-how-to-refer-to-several-messages/58759/2 "2022-02-21T16:53:46Z")

</div>

> [@WBr](#):
>
> with only using the function node

You can do it in 1 function node however you should probably use a join node to get the values into 1 message first.

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: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [21 February 2022 16:54 UTC](https://discourse.nodered.org/t/function-node-how-to-refer-to-several-messages/58759/3 "2022-02-21T16:54:27Z")

</div>

You can join the two messages into one using the Join node. But of course that fails "only using the sensor node".

Alternatively you can save the sensor1 reading in a context variable **context.set('sensor1', msg.payload)**  
and use it when sensor2 reading arrives **let answer = context.get('sensor1') - msg.payload**  
You can maybe use msg.topic to distinguish the two messages.

---

<div class="post-metadata">

### Author: ![WBr](https://avatars.discourse-cdn.com/v4/letter/w/b3f665/32.png) [@WBr](https://discourse.nodered.org/u/WBr)
#### Post date: [21 February 2022 17:11 UTC](https://discourse.nodered.org/t/function-node-how-to-refer-to-several-messages/58759/4 "2022-02-21T17:11:50Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/2/c223834dc4f261a10820ceb49ee8016b4134175c.png)  
Indeed it did not work.

I can set a topic, but how do I refer to that in the change node?  
New to this  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/e/8e4599f687fbbf050df90755e4ff5eb94321959c.png)

---

<div class="post-metadata">

### Author: ![WBr](https://avatars.discourse-cdn.com/v4/letter/w/b3f665/32.png) [@WBr](https://discourse.nodered.org/u/WBr)
#### Post date: [21 February 2022 17:21 UTC](https://discourse.nodered.org/t/function-node-how-to-refer-to-several-messages/58759/5 "2022-02-21T17:21:48Z")

</div>

> [@jbudd](#):
>
> Alternatively you can save the sensor1 reading in a context variable **context.set('sensor1', msg.payload)**

Not sure how!

---

<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 February 2022 17:47 UTC](https://discourse.nodered.org/t/function-node-how-to-refer-to-several-messages/58759/6 "2022-02-21T17:47:03Z")

</div>

OK.

1. I do not understand "how do I refer to that in the change node". You have not previously mentioned a change node, nor have we seen your flow.

2. Your msg.payload is not a value 21.6 but an object {temperature: 26} so you access the value as msg.payload.temperature.

Here is a sample flow that uses a Join node to combine the two sensor readings into a single message, using the value of msg.topic to distinguish one from the other.  
The function node just does the subtraction.  
It's a bad idea to leap into coding javascript functions when you start with Node-red. It's seductive though because flow based and low-code programming is a different way of thinking.  
I am certain that the subtraction can be done without the function node, via a Jsonata expression in a change node, maybe a jsonata expert will advise how.

 ![Untitled 4](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/6/1620995e806e221e68bb1748323e354d8de33398.jpeg)

```auto
[
    {
        "id": "5607f6657ab75a36",
        "type": "inject",
        "z": "7344b8ed03ec977f",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "sensor1",
        "payload": "21.6",
        "payloadType": "num",
        "x": 230,
        "y": 280,
        "wires": [
            [
                "4246d7e7ea568180"
            ]
        ]
    },
    {
        "id": "9b5bb5f8dc7cf909",
        "type": "inject",
        "z": "7344b8ed03ec977f",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "sensor2",
        "payload": "45.6",
        "payloadType": "num",
        "x": 230,
        "y": 320,
        "wires": [
            [
                "4246d7e7ea568180"
            ]
        ]
    },
    {
        "id": "4246d7e7ea568180",
        "type": "join",
        "z": "7344b8ed03ec977f",
        "name": "",
        "mode": "custom",
        "build": "object",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "accumulate": true,
        "timeout": "",
        "count": "2",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "",
        "reduceFixup": "",
        "x": 390,
        "y": 300,
        "wires": [
            [
                "2a7fe71d569a3ecc"
            ]
        ]
    },
    {
        "id": "2a7fe71d569a3ecc",
        "type": "function",
        "z": "7344b8ed03ec977f",
        "name": "",
        "func": "msg.payload = msg.payload.sensor2 - msg.payload.sensor1;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 540,
        "y": 300,
        "wires": [
            [
                "e9cfe571d7d9df77"
            ]
        ]
    },
    {
        "id": "e9cfe571d7d9df77",
        "type": "debug",
        "z": "7344b8ed03ec977f",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 710,
        "y": 300,
        "wires": []
    }
]

```

---

<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 February 2022 18:04 UTC](https://discourse.nodered.org/t/function-node-how-to-refer-to-several-messages/58759/7 "2022-02-21T18:04:02Z")

</div>

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

Demo flow (use CTRL+I to import)

```auto
[{"id":"2064375997c3d07a","type":"inject","z":"c1ebfd3083f3904f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"sensor 1","payload":"18.9","payloadType":"num","x":990,"y":1540,"wires":[["440d073ef750cb4b"]]},{"id":"fda9172411bdbcec","type":"inject","z":"c1ebfd3083f3904f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"sensor 2","payload":"{ \"temperature\": 99 }","payloadType":"json","x":1040,"y":1580,"wires":[["30de3bb9e05be2a3"]]},{"id":"30de3bb9e05be2a3","type":"change","z":"c1ebfd3083f3904f","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.temperature","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1300,"y":1580,"wires":[["440d073ef750cb4b"]]},{"id":"440d073ef750cb4b","type":"join","z":"c1ebfd3083f3904f","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":1490,"y":1540,"wires":[["2282069c1153400a","ada6b2435bb3f0c5"]]},{"id":"2282069c1153400a","type":"function","z":"c1ebfd3083f3904f","name":"","func":"msg.payload = msg.payload[\"sensor 2\"] - msg.payload[\"sensor 1\"]\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1670,"y":1540,"wires":[["664bc70898372ce7"]]},{"id":"ada6b2435bb3f0c5","type":"debug","z":"c1ebfd3083f3904f","name":"after join","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1600,"y":1600,"wires":[]},{"id":"664bc70898372ce7","type":"debug","z":"c1ebfd3083f3904f","name":"after function","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1870,"y":1600,"wires":[]}]

```

* * *

> [@WBr](#):
>
> New to this

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: ![WBr](https://avatars.discourse-cdn.com/v4/letter/w/b3f665/32.png) [@WBr](https://discourse.nodered.org/u/WBr)
#### Post date: [22 February 2022 07:57 UTC](https://discourse.nodered.org/t/function-node-how-to-refer-to-several-messages/58759/8 "2022-02-22T07:57:10Z")

</div>

Thanks for the help and advise.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/0/1077507260b7fde00fe088b2888c188f150b0638.png)

I got this in my debug, the actual sensors are connected. Why does the second debug say, not a number..?

---

<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: [22 February 2022 08:02 UTC](https://discourse.nodered.org/t/function-node-how-to-refer-to-several-messages/58759/9 "2022-02-22T08:02:19Z")

</div>

> [@WBr](#):
>
> Why does the second debug say, not a number

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

* * *

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)

---

<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: [22 February 2022 08:03 UTC](https://discourse.nodered.org/t/function-node-how-to-refer-to-several-messages/58759/10 "2022-02-22T08:03:12Z")

</div>

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

You would pick up all the useful tips like the "copy path" one i gave you in the previous post.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [22 February 2022 08:08 UTC](https://discourse.nodered.org/t/function-node-how-to-refer-to-several-messages/58759/11 "2022-02-22T08:08:52Z")

</div>

The other thing which is sticking out to me is that in your screen shots BOTH messages have the same `topic`.

Post 8. Both `Sensor 2`

So there is something else going on here.  
Then, one message is an object (or not a simple message) and the other is `NaN`....

Ah! Nan bread! 😉  
Sorry.  
Bye.

---

<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: [22 February 2022 08:12 UTC](https://discourse.nodered.org/t/function-node-how-to-refer-to-several-messages/58759/12 "2022-02-22T08:12:22Z")

</div>

> [@Trying\_to\_learn](#):
>
> The other thing which is sticking out to me is that in your screen shots BOTH messages have the same `topic`

Andrew, the OP definitely has individual topics - clear from the debug node named "after join node"

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

the other message with topic "Sensor 2" is from a different debug (a function after the join node)

So that is not the issue.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [22 February 2022 08:13 UTC](https://discourse.nodered.org/t/function-node-how-to-refer-to-several-messages/58759/13 "2022-02-22T08:13:32Z")

</div>

Sorry.... Seems I am not parsing things properly.

I'll shut up then. 😉

Didn't mean to show I am THAT silly.

---

<div class="post-metadata">

### Author: ![WBr](https://avatars.discourse-cdn.com/v4/letter/w/b3f665/32.png) [@WBr](https://discourse.nodered.org/u/WBr)
#### Post date: [22 February 2022 08:52 UTC](https://discourse.nodered.org/t/function-node-how-to-refer-to-several-messages/58759/14 "2022-02-22T08:52:28Z")

</div>

Thank you all. I found the problem... my topic was "Sensor 1" instead of "sensor 1"  
Same with the other topic! I forgot they were capital letter sensitive....

It works!

---

<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: [8 March 2022 08:52 UTC](https://discourse.nodered.org/t/function-node-how-to-refer-to-several-messages/58759/15 "2022-03-08T08:52:53Z")

</div>

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