# Importing two payloads as variables and use them for calculation

**URL:** <https://discourse.nodered.org/t/importing-two-payloads-as-variables-and-use-them-for-calculation/89455>\
**Category:** General\
**Tags:** function-node\
**Created:** [12 July 2024 21:04 UTC](https://discourse.nodered.org/t/importing-two-payloads-as-variables-and-use-them-for-calculation/89455 "2024-07-12T21:04:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lenaschitz](https://avatars.discourse-cdn.com/v4/letter/l/dfb087/32.png) [@Lenaschitz](https://discourse.nodered.org/u/Lenaschitz)\
**Post date:** [12 July 2024 21:04 UTC](https://discourse.nodered.org/t/importing-two-payloads-as-variables-and-use-them-for-calculation/89455/1 "2024-07-12T21:04:11Z")

</div>

hi guys,  
I wanna import two different payloads (with two different topics) to a function node and use them to calculate someting. At the end the result should be send out as a payload again.  
I get the two values from another flow, that´s working. If I replace the two variables with static values, the funtion node works. But not using the payloads. Any hints for a beginner?  
Regards

```auto
[
   {
       "id": "df744e750e5a9e81",
       "type": "tab",
       "label": "Taupunkte",
       "disabled": false,
       "info": "",
       "env": []
   },
   {
       "id": "589a22c4f044264e",
       "type": "function",
       "z": "df744e750e5a9e81",
       "name": "Td außen",
       "func": "var es = { };\nvar ed = { };\nvar eln = { };\nvar td = { };\nvar msg1 = {};\nvar msg3 = {};\nmsg1.payload = msg.payload.tempout;\nmsg3.payload = msg.payload.humidityout;\nif (msg1.payload >= 0)\n{\n\t\t es.payload = 6.1078*Math.pow(10,((7.5*msg1.payload)/(237.3 + msg1.payload)));\n\t\t ed.payload = msg3.payload/100.0*es.payload;\n\t\t eln.payload = Math.log10(ed.payload/6.1078);\n\t\t\ttd.payload = 237.3*eln.payload/(7.5 - eln.payload);\n}\nelse\n{\n\t\t es.payload = 6.1078*Math.pow(10,((7.6*msg1.payload)/(240.7 + msg1.payload)));\n\t\t ed.payload = msg3.payload/100.0*es.payload;\n\t\t eln.payload = Math.log10(ed.payload/6.1078);\n\t\t\ttd.payload = 240.7*eln.payload/(7.6 - eln.payload);\n}\nreturn [msg1,msg3];",
       "outputs": 1,
       "timeout": 0,
       "noerr": 0,
       "initialize": "",
       "finalize": "",
       "libs": [],
       "x": 500,
       "y": 120,
       "wires": [
           [
               "a4cc9938fa255750"
           ]
       ]
   },
   {
       "id": "e3019f3175327953",
       "type": "link in",
       "z": "df744e750e5a9e81",
       "name": "Humidity out",
       "links": [
           "995f7c2d3ba33d99"
       ],
       "x": 65,
       "y": 140,
       "wires": [
           [
               "da8bfa18928117f9"
           ]
       ]
   },
   {
       "id": "33412de7df28bca2",
       "type": "link in",
       "z": "df744e750e5a9e81",
       "name": "Temp out",
       "links": [
           "ffc8b882aa95a7cd"
       ],
       "x": 65,
       "y": 100,
       "wires": [
           [
               "f860fc114b0a2b09"
           ]
       ]
   },
   {
       "id": "a4cc9938fa255750",
       "type": "debug",
       "z": "df744e750e5a9e81",
       "name": "debug 11",
       "active": true,
       "tosidebar": true,
       "console": false,
       "tostatus": false,
       "complete": "payload",
       "targetType": "msg",
       "statusVal": "",
       "statusType": "auto",
       "x": 1100,
       "y": 100,
       "wires": []
   },
   {
       "id": "da8bfa18928117f9",
       "type": "change",
       "z": "df744e750e5a9e81",
       "name": "",
       "rules": [
           {
               "t": "set",
               "p": "topic",
               "pt": "msg",
               "to": "humidityout",
               "tot": "str"
           }
       ],
       "action": "",
       "property": "",
       "from": "",
       "to": "",
       "reg": false,
       "x": 200,
       "y": 140,
       "wires": [
           [
               "54d2c21d96f0c541",
               "589a22c4f044264e"
           ]
       ]
   },
   {
       "id": "35bcef6655a8a3ca",
       "type": "debug",
       "z": "df744e750e5a9e81",
       "name": "tempout",
       "active": true,
       "tosidebar": true,
       "console": false,
       "tostatus": true,
       "complete": "payload",
       "targetType": "msg",
       "statusVal": "payload",
       "statusType": "auto",
       "x": 400,
       "y": 40,
       "wires": []
   },
   {
       "id": "54d2c21d96f0c541",
       "type": "debug",
       "z": "df744e750e5a9e81",
       "name": "humidityout",
       "active": true,
       "tosidebar": true,
       "console": false,
       "tostatus": true,
       "complete": "payload",
       "targetType": "msg",
       "statusVal": "payload",
       "statusType": "auto",
       "x": 410,
       "y": 200,
       "wires": []
   },
   {
       "id": "f860fc114b0a2b09",
       "type": "change",
       "z": "df744e750e5a9e81",
       "name": "",
       "rules": [
           {
               "t": "set",
               "p": "topic",
               "pt": "msg",
               "to": "tempout",
               "tot": "str"
           }
       ],
       "action": "",
       "property": "",
       "from": "",
       "to": "",
       "reg": false,
       "x": 200,
       "y": 100,
       "wires": [
           [
               "35bcef6655a8a3ca",
               "589a22c4f044264e"
           ]
       ]
   }
]

```

---

<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:** [12 July 2024 23:02 UTC](https://discourse.nodered.org/t/importing-two-payloads-as-variables-and-use-them-for-calculation/89455/2 "2024-07-12T23:02:45Z")

</div>

Hi @Lenaschitz,

You are almost there but you need to use a join node so that the humidity and temperature are both available in _a single message_ as `msg.payload.humidityout` and `msg.payload.tempout`.  
A function node. like almost all nodes, only sees one message at a time.

Try this

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

```auto
[{"id":"589a22c4f044264e","type":"function","z":"df744e750e5a9e81","name":"Td außen","func":"var es = { };\nvar ed = { };\nvar eln = { };\nvar td = { };\nvar msg1 = {};\nvar msg3 = {};\nmsg1.payload = msg.payload.tempout;\nmsg3.payload = msg.payload.humidityout;\nif (msg1.payload >= 0)\n{\n\t\t es.payload = 6.1078*Math.pow(10,((7.5*msg1.payload)/(237.3 + msg1.payload)));\n\t\t ed.payload = msg3.payload/100.0*es.payload;\n\t\t eln.payload = Math.log10(ed.payload/6.1078);\n\t\t\ttd.payload = 237.3*eln.payload/(7.5 - eln.payload);\n}\nelse\n{\n\t\t es.payload = 6.1078*Math.pow(10,((7.6*msg1.payload)/(240.7 + msg1.payload)));\n\t\t ed.payload = msg3.payload/100.0*es.payload;\n\t\t eln.payload = Math.log10(ed.payload/6.1078);\n\t\t\ttd.payload = 240.7*eln.payload/(7.6 - eln.payload);\n}\nreturn [msg1,msg3];","outputs":2,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":640,"y":120,"wires":[["a4cc9938fa255750"],["b8d5476b4242a6aa"]]},{"id":"a4cc9938fa255750","type":"debug","z":"df744e750e5a9e81","name":"msg1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":790,"y":100,"wires":[]},{"id":"da8bfa18928117f9","type":"change","z":"df744e750e5a9e81","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"humidityout","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":270,"y":160,"wires":[["54d2c21d96f0c541","dcca29ba6dddb46b"]]},{"id":"35bcef6655a8a3ca","type":"debug","z":"df744e750e5a9e81","name":"tempout","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":460,"y":60,"wires":[]},{"id":"54d2c21d96f0c541","type":"debug","z":"df744e750e5a9e81","name":"humidityout","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":470,"y":180,"wires":[]},{"id":"f860fc114b0a2b09","type":"change","z":"df744e750e5a9e81","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"tempout","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":270,"y":80,"wires":[["35bcef6655a8a3ca","dcca29ba6dddb46b"]]},{"id":"dcca29ba6dddb46b","type":"join","z":"df744e750e5a9e81","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":450,"y":120,"wires":[["589a22c4f044264e","5ec7c68d4ca8a604"]]},{"id":"5ec7c68d4ca8a604","type":"debug","z":"df744e750e5a9e81","name":"Joined payload","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":660,"y":60,"wires":[]},{"id":"24e426bdec5f9431","type":"inject","z":"df744e750e5a9e81","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"85","payloadType":"num","x":90,"y":140,"wires":[[]]},{"id":"7acf594ecde0d970","type":"inject","z":"df744e750e5a9e81","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"85","payloadType":"num","x":90,"y":140,"wires":[["da8bfa18928117f9"]]},{"id":"7334d6d8751fc2d0","type":"inject","z":"df744e750e5a9e81","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"87","payloadType":"num","x":90,"y":180,"wires":[["da8bfa18928117f9"]]},{"id":"82bd99e51ba158b2","type":"inject","z":"df744e750e5a9e81","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"22","payloadType":"num","x":90,"y":100,"wires":[["f860fc114b0a2b09"]]},{"id":"bf5031b05ae89954","type":"inject","z":"df744e750e5a9e81","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"21","payloadType":"num","x":90,"y":60,"wires":[["f860fc114b0a2b09"]]},{"id":"b8d5476b4242a6aa","type":"debug","z":"df744e750e5a9e81","name":"msg3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":790,"y":140,"wires":[]}]

```

There are a couple of things about your function node too.

- You have `return [msg1,msg3];` which implies that the node has two outputs. The Setup tab lets you control this.
- You calculate values for the objects `es`, `ed`, `eln` and `td` but you don't pass these on as message properties for use further along the wires

---

<div class="post-metadata">

**Author:** ![Lenaschitz](https://avatars.discourse-cdn.com/v4/letter/l/dfb087/32.png) [@Lenaschitz](https://discourse.nodered.org/u/Lenaschitz)\
**Post date:** [13 July 2024 04:45 UTC](https://discourse.nodered.org/t/importing-two-payloads-as-variables-and-use-them-for-calculation/89455/3 "2024-07-13T04:45:19Z")

</div>

Hi jbudd,  
many thanks for your reply, works now also with real data. Did read already a lot about the join node, but wasn´t aware to add a "2" for handling two messages instead of one.  
And in the end I need td, but I was sure, were the issue comes from, so I wanted to see if both variables pass the function node.  
Many thanks for your support.

---

<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:** [27 July 2024 04:46 UTC](https://discourse.nodered.org/t/importing-two-payloads-as-variables-and-use-them-for-calculation/89455/4 "2024-07-27T04:46:18Z")

</div>

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