# How to set two same incoming message formats to different variables

**URL:** <https://discourse.nodered.org/t/how-to-set-two-same-incoming-message-formats-to-different-variables/79349>\
**Category:** General\
**Tags:** function-node\
**Created:** [22 June 2023 08:48 UTC](https://discourse.nodered.org/t/how-to-set-two-same-incoming-message-formats-to-different-variables/79349 "2023-06-22T08:48:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Industry40](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@Industry40](https://discourse.nodered.org/u/Industry40)\
**Post date:** [22 June 2023 08:48 UTC](https://discourse.nodered.org/t/how-to-set-two-same-incoming-message-formats-to-different-variables/79349/1 "2023-06-22T08:48:17Z")

</div>

Hello,

I am receiving two messages from different nodes from plc and I need to set it to two different variables.  
But its not working as planned.

To have a better understanding there is an example flow below. Do someone know how to do this?  
I am supposed to get "car" : 7 and "bike" : 8.  
But I am getting only bike value.  
 ![nodere](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/a/1ae12f19edc3c15fd8731c6f681da893105cb623.jpeg)

```auto
[
    {
        "id": "ab9a0a1cbf6d3818",
        "type": "inject",
        "z": "ea47a1366397a64c",
        "name": "",
        "props": [
            {
                "p": "payload.jam",
                "v": "7",
                "vt": "num"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 160,
        "y": 120,
        "wires": [
            [
                "b934d88217fe9eed"
            ]
        ]
    },
    {
        "id": "0d591409cde875ca",
        "type": "inject",
        "z": "ea47a1366397a64c",
        "name": "",
        "props": [
            {
                "p": "payload.jam",
                "v": "8",
                "vt": "num"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 160,
        "y": 200,
        "wires": [
            [
                "3eb1401e4f4bfc48"
            ]
        ]
    },
    {
        "id": "3eb1401e4f4bfc48",
        "type": "function",
        "z": "ea47a1366397a64c",
        "name": "function 123",
        "func": "flow.set(\"car\", msg.topic);\nx=flow.get(\"car\")\n\nflow.set(\"bike\",msg.payload.jam);\nx = flow.get(\"bike\")\nreturn msg;",
        "outputs": 1,
        "noerr": 2,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 670,
        "y": 120,
        "wires": [
            [
                "370bec198f8ba585"
            ]
        ]
    },
    {
        "id": "b934d88217fe9eed",
        "type": "change",
        "z": "ea47a1366397a64c",
        "name": "",
        "rules": [
            {
                "t": "move",
                "p": "payload.jam",
                "pt": "msg",
                "to": "topic",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 390,
        "y": 120,
        "wires": [
            [
                "3eb1401e4f4bfc48",
                "78fed6e97b9643f4"
            ]
        ]
    },
    {
        "id": "78fed6e97b9643f4",
        "type": "debug",
        "z": "ea47a1366397a64c",
        "name": "debug 20",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 340,
        "y": 40,
        "wires": []
    },
    {
        "id": "370bec198f8ba585",
        "type": "debug",
        "z": "ea47a1366397a64c",
        "name": "debug 21",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 870,
        "y": 120,
        "wires": []
    }
]

```

---

<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:** [22 June 2023 09:08 UTC](https://discourse.nodered.org/t/how-to-set-two-same-incoming-message-formats-to-different-variables/79349/2 "2023-06-22T09:08:13Z")

</div>

Hi @Industry40 and welcome to the forum.

> [@Industry40](#):
>
> I am receiving two messages from different nodes from plc and I need to set it to two different variables.

Perhaps I'm missing something but you have two distinct inputs and you want to set two distinct flow context variables...  
Why do you have a function node?  
Does this not do what you want?  
 ![Untitled 2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/b/0bc52a3618da89982595b9388f179010415b36f5.jpeg)

ps I suspect you are misunderstanding how messages work in Node-red.  
Almost all nodes\* operate on the current message with no knowledge of previous messages.

So for example message1 has {"payload": 100, "colour": "red"}  
message2 has {"topic": 42, "colour"green"}

When message2 arrives at a node, msg.payload does not still contain 100, it is **undefined**.

`* An obvious exception is the filter node.`

---

<div class="post-metadata">

**Author:** ![Industry40](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@Industry40](https://discourse.nodered.org/u/Industry40)\
**Post date:** [22 June 2023 10:39 UTC](https://discourse.nodered.org/t/how-to-set-two-same-incoming-message-formats-to-different-variables/79349/3 "2023-06-22T10:39:42Z")

</div>

Hello,  
Thanks a lot for the fast response.

That was well explained. I was setting two variables in same function which was the prob.

Now I set variables with two different change nodes and get the both values in same function node and display it in table. Which works well.

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [26 June 2023 00:47 UTC](https://discourse.nodered.org/t/how-to-set-two-same-incoming-message-formats-to-different-variables/79349/4 "2023-06-26T00:47:04Z")

</div>

Wasn't @jbudd post the solution 🤔

---

<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:** [10 July 2023 00:47 UTC](https://discourse.nodered.org/t/how-to-set-two-same-incoming-message-formats-to-different-variables/79349/5 "2023-07-10T00:47:47Z")

</div>

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