How to set two same incoming message formats to different variables

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

[
    {
        "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": []
    }
]

Hi @Industry40 and welcome to the forum.

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

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.

2 Likes

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.

Wasn't @jbudd post the solution :thinking:

2 Likes

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