# Inject dynamical variables in a node

**URL:** https://discourse.nodered.org/t/inject-dynamical-variables-in-a-node/85957
**Category:** Industrial
**Created:** [27 February 2024 14:14 UTC](https://discourse.nodered.org/t/inject-dynamical-variables-in-a-node/85957 "2024-02-27T14:14:00Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![MystiiK](https://avatars.discourse-cdn.com/v4/letter/m/c89c15/32.png) [@MystiiK](https://discourse.nodered.org/u/MystiiK)
#### Post date: [27 February 2024 14:14 UTC](https://discourse.nodered.org/t/inject-dynamical-variables-in-a-node/85957/1 "2024-02-27T14:14:00Z")

</div>

Hello,

I read data from an electronic card integrated in a climatic chamber.  
I use the node-red-contrib-telnet-client to do that (I can't use s7 nodes for example).

Because we have multiples chambers, I created an object (in a function) where there is the list of the chambers with their own properties (IP address, name, port). In the telnet nodes, I would like to inject these parameters in the fields, and not directly a value. In fact, I would like to establish the connection for one chamber, then read and treat data, and finally wait a little in order to do this cycle for each chamber (the delay is here for avoiding communication saturation).

How could I make a loop for this ? I just have to change the 3 fields in the "Connection" field of the telnet node. For instance, in the "Port" field in the telnet node, I can only put a number value. I can not enter a parameter like "msg.payload.port".

Here is my function with the list of the chambers :

```auto
let Etuves = {
    nomEtuve: "",
    adresseIP: "",
    port: 888,
}

let etuve217 = Object.create(Etuves);
etuve217.nomEtuve = "Etuve 217";
etuve217.adresseIP = "xx.xx.xx.217";

let etuve109 = Object.create(Etuves);
etuve109.nomEtuve = "Etuve 109";
etuve109.adresseIP = "xx.xx.xx.109";

let etuve111 = Object.create(Etuves);
etuve111.nomEtuve = "Etuve 111";
etuve111.adresseIP = "xx.xx.xx.111";

let etuve216 = Object.create(Etuves);
etuve216.nomEtuve = "Etuve 216";
etuve216.adresseIP = "xx.xx.xx.216";

let tab = [];
tab.push(etuve217)
tab.push(etuve109)
tab.push(etuve111)
tab.push(etuve216)

return {payload: tab}

```

You will find below the flow :

```auto
[
    {
        "id": "1a7e315fd340af0f",
        "type": "tab",
        "label": "Flow 3",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "a8c1d9281aea0673",
        "type": "function",
        "z": "1a7e315fd340af0f",
        "name": "List of the chambers",
        "func": "let Etuves = {\n nomEtuve: \"\",\n adresseIP: \"\",\n port: 888,\n}\n\n\n\nlet etuve217 = Object.create(Etuves);\netuve217.nomEtuve = \"Etuve 217\";\netuve217.adresseIP = \"xx.xx.xx.217\";\n\n\nlet etuve109 = Object.create(Etuves);\netuve109.nomEtuve = \"Etuve 109\";\netuve109.adresseIP = \"xx.xx.xx.109\";\n\n\nlet etuve111 = Object.create(Etuves);\netuve111.nomEtuve = \"Etuve 111\";\netuve111.adresseIP = \"xx.xx.xx.111\";\n\n\nlet etuve216 = Object.create(Etuves);\netuve216.nomEtuve = \"Etuve 216\";\netuve216.adresseIP = \"xx.xx.xx.216\";\n\n\nlet tab = [];\ntab.push(etuve217)\ntab.push(etuve109)\ntab.push(etuve111)\ntab.push(etuve216)\n\nreturn {payload: tab}\n\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 600,
        "y": 360,
        "wires": [
            [
                "133e8899145a6ab6",
                "a690ba198e52db7e"
            ]
        ]
    },
    {
        "id": "32fa05f1e9c7c923",
        "type": "inject",
        "z": "1a7e315fd340af0f",
        "name": "Fake Data",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "56",
        "payloadType": "num",
        "x": 400,
        "y": 360,
        "wires": [
            [
                "a8c1d9281aea0673"
            ]
        ]
    },
    {
        "id": "2b5a428a194e0fc4",
        "type": "debug",
        "z": "1a7e315fd340af0f",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 1050,
        "y": 280,
        "wires": []
    },
    {
        "id": "133e8899145a6ab6",
        "type": "change",
        "z": "1a7e315fd340af0f",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "listeEtuves",
                "pt": "global",
                "to": "tab",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 860,
        "y": 360,
        "wires": [
            [
                "2b5a428a194e0fc4",
                "16e8c2f9e428e79c"
            ]
        ]
    },
    {
        "id": "a690ba198e52db7e",
        "type": "debug",
        "z": "1a7e315fd340af0f",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 830,
        "y": 280,
        "wires": []
    },
    {
        "id": "4cc7ebb845e3a096",
        "type": "telnet-send",
        "z": "1a7e315fd340af0f",
        "name": "",
        "connection": "27ace6a62991b9b0",
        "x": 1230,
        "y": 360,
        "wires": []
    },
    {
        "id": "a4df93d5b4be84e1",
        "type": "telnet-read",
        "z": "1a7e315fd340af0f",
        "name": "",
        "connection": "",
        "x": 1230,
        "y": 420,
        "wires": [
            [
                "e1a6047482ce5f0e"
            ]
        ]
    },
    {
        "id": "e1a6047482ce5f0e",
        "type": "debug",
        "z": "1a7e315fd340af0f",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 1390,
        "y": 420,
        "wires": []
    },
    {
        "id": "16e8c2f9e428e79c",
        "type": "delay",
        "z": "1a7e315fd340af0f",
        "name": "",
        "pauseType": "delay",
        "timeout": "10",
        "timeoutUnits": "milliseconds",
        "rate": "1",
        "nbRateUnits": "1",
        "rateUnits": "second",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": false,
        "allowrate": false,
        "outputs": 1,
        "x": 1070,
        "y": 360,
        "wires": [
            [
                "4cc7ebb845e3a096"
            ]
        ]
    },
    {
        "id": "27ace6a62991b9b0",
        "type": "telnet-connection",
        "name": "",
        "address": "127.0.0.1",
        "port": "23",
        "inDelimiter": "",
        "outDelimiter": "",
        "timeOut": "1500",
        "clearOut": "",
        "openTries": "1"
    }
]

```

I am still a beginner in Javascript, so maybe there is a more efficient way to do this function.  
Thank you for your help 🙂

---

<div class="post-metadata">

### Author: ![MystiiK](https://avatars.discourse-cdn.com/v4/letter/m/c89c15/32.png) [@MystiiK](https://discourse.nodered.org/u/MystiiK)
#### Post date: [29 February 2024 11:50 UTC](https://discourse.nodered.org/t/inject-dynamical-variables-in-a-node/85957/2 "2024-02-29T11:50:15Z")

</div>

Hi, I come back to my previous message.

In fact, due to the impossibility to inject dynamical variables, I can't factorise the code like I wanted. The solution I've found is to create send and read data nodes for each chamber. However, the processing of data can be done in a subflow in order to shorten the code. The problem is the using of global variables in the subflow. I noticed that the values for both chambers are well read, but they are replaced when the same parameter is read of the next chamber. For instance, I would like to print the pressure value of the first and the second chamber. In my case, it prints first :

```auto
"Chamber 1 : 10.09"
"Chamber 2 : 10.09"

```

And then :

```auto
"Chamber 1 : 13.49"
"Chamber 2 : 13.49"

```

I would like to get :

```auto
"Chamber 1 : 10.09"
"Chamber 2 : 13.49"

```

Do you know how can I fix this problem ? I did one processing for each chamber, so I thought it will give me the value for each one. But apparently, it is not the case. Here is an idea of my flow.

 ![Capture](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/8/d8b19114df02a34c4660a1c4fd7020b847a35d90.png)  
In the subflow, I only have a change node which sets a global value for one parameter.  
I hope it was clear, but I definitely think that the problem comes from global values.

Flow :

```auto
[
    {
        "id": "2c411b946746dd66",
        "type": "subflow",
        "name": "Subflow 1",
        "info": "",
        "in": [
            {
                "x": 380,
                "y": 280,
                "wires": [
                    {
                        "id": "f22cb29dee88cede"
                    }
                ]
            }
        ],
        "out": [
            {
                "x": 880,
                "y": 280,
                "wires": [
                    {
                        "id": "f22cb29dee88cede",
                        "port": 0
                    }
                ]
            }
        ]
    },
    {
        "id": "f22cb29dee88cede",
        "type": "change",
        "z": "2c411b946746dd66",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "Etuve.MHpPrinc",
                "pt": "global",
                "to": "msg.payload",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 620,
        "y": 280,
        "wires": [
            []
        ]
    },
    {
        "id": "2e1fb91b064e0dbb",
        "type": "tab",
        "label": "Flow 4",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "8e3b11bfdc086999",
        "type": "inject",
        "z": "2e1fb91b064e0dbb",
        "name": "Fake data",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "10",
        "crontab": "",
        "once": true,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "56",
        "payloadType": "num",
        "x": 510,
        "y": 440,
        "wires": [
            [
                "a05f0162842cae4d",
                "40088b6b39ea021c"
            ]
        ]
    },
    {
        "id": "40088b6b39ea021c",
        "type": "telnet-send",
        "z": "2e1fb91b064e0dbb",
        "name": "",
        "connection": "8a3cd99875508432",
        "x": 750,
        "y": 380,
        "wires": []
    },
    {
        "id": "5b1dc18d931f5d53",
        "type": "telnet-read",
        "z": "2e1fb91b064e0dbb",
        "name": "",
        "connection": "8a3cd99875508432",
        "x": 1010,
        "y": 380,
        "wires": [
            [
                "5fa05bac2447ebfe"
            ]
        ]
    },
    {
        "id": "a05f0162842cae4d",
        "type": "telnet-send",
        "z": "2e1fb91b064e0dbb",
        "name": "",
        "connection": "1834e5b8655e71f1",
        "x": 750,
        "y": 520,
        "wires": []
    },
    {
        "id": "35d4f0aaad4e1ed6",
        "type": "telnet-read",
        "z": "2e1fb91b064e0dbb",
        "name": "",
        "connection": "1834e5b8655e71f1",
        "x": 1010,
        "y": 520,
        "wires": [
            [
                "ca8f57ae883f8f1b"
            ]
        ]
    },
    {
        "id": "6ffbf644f4de15a4",
        "type": "debug",
        "z": "2e1fb91b064e0dbb",
        "name": "Chamber 1",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1390,
        "y": 380,
        "wires": []
    },
    {
        "id": "bc9e4410b64aac46",
        "type": "debug",
        "z": "2e1fb91b064e0dbb",
        "name": "Chamber 2",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1390,
        "y": 480,
        "wires": []
    },
    {
        "id": "f1b525c41d22ab3c",
        "type": "ui_text",
        "z": "2e1fb91b064e0dbb",
        "group": "7b5442934adeb0ca",
        "order": 1,
        "width": 6,
        "height": 1,
        "name": "",
        "label": "MHpPrinc 1",
        "format": "{{msg.payload}}",
        "layout": "row-center",
        "className": "",
        "x": 1390,
        "y": 420,
        "wires": []
    },
    {
        "id": "74258a789825c2c3",
        "type": "ui_text",
        "z": "2e1fb91b064e0dbb",
        "group": "7b5442934adeb0ca",
        "order": 1,
        "width": 6,
        "height": 1,
        "name": "",
        "label": "MHpPrinc 2",
        "format": "{{msg.payload}}",
        "layout": "row-center",
        "className": "",
        "x": 1390,
        "y": 520,
        "wires": []
    },
    {
        "id": "ac54dc8d04b57ac6",
        "type": "comment",
        "z": "2e1fb91b064e0dbb",
        "name": "Chamber 1",
        "info": "",
        "x": 880,
        "y": 380,
        "wires": []
    },
    {
        "id": "75b10e05ba2ef27c",
        "type": "comment",
        "z": "2e1fb91b064e0dbb",
        "name": "Chamber 2",
        "info": "",
        "x": 880,
        "y": 520,
        "wires": []
    },
    {
        "id": "5fa05bac2447ebfe",
        "type": "subflow:2c411b946746dd66",
        "z": "2e1fb91b064e0dbb",
        "name": "",
        "x": 1180,
        "y": 380,
        "wires": [
            [
                "6ffbf644f4de15a4",
                "f1b525c41d22ab3c"
            ]
        ]
    },
    {
        "id": "ca8f57ae883f8f1b",
        "type": "subflow:2c411b946746dd66",
        "z": "2e1fb91b064e0dbb",
        "name": "",
        "x": 1180,
        "y": 520,
        "wires": [
            [
                "bc9e4410b64aac46",
                "74258a789825c2c3"
            ]
        ]
    },
    {
        "id": "8a3cd99875508432",
        "type": "telnet-connection",
        "name": "Chamber 1",
        "address": "127.0.0.1",
        "port": "23",
        "inDelimiter": "",
        "outDelimiter": "",
        "timeOut": "1500",
        "clearOut": "",
        "openTries": "1"
    },
    {
        "id": "1834e5b8655e71f1",
        "type": "telnet-connection",
        "name": "Chamber 2",
        "address": "127.0.0.2",
        "port": "23",
        "inDelimiter": "",
        "outDelimiter": "",
        "timeOut": "1500",
        "clearOut": "",
        "openTries": "1"
    },
    {
        "id": "7b5442934adeb0ca",
        "type": "ui_group",
        "name": "Group 1",
        "tab": "81690565567bb45b",
        "order": 1,
        "disp": true,
        "width": 9
    },
    {
        "id": "81690565567bb45b",
        "type": "ui_tab",
        "name": "TEST",
        "icon": "dashboard",
        "order": 5,
        "disabled": false,
        "hidden": false
    }
]

```

---

<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: [29 February 2024 11:56 UTC](https://discourse.nodered.org/t/inject-dynamical-variables-in-a-node/85957/3 "2024-02-29T11:56:43Z")

</div>

Your subflow is completely meaningless - it sets a global variable and nothing more

It is the payload value from these `read` nodes that set your UI elements.

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

that should be evident by the output of the debug nodes.

---

<div class="post-metadata">

### Author: ![MystiiK](https://avatars.discourse-cdn.com/v4/letter/m/c89c15/32.png) [@MystiiK](https://discourse.nodered.org/u/MystiiK)
#### Post date: [29 February 2024 13:44 UTC](https://discourse.nodered.org/t/inject-dynamical-variables-in-a-node/85957/4 "2024-02-29T13:44:58Z")

</div>

Thanks for your reply.  
I put only one value in the subflow for the example, but there are many others variables in my real flow. I also do some processing within it. That is why I created a subflow. There are multiples payload within it. Otherwise yes, the subflow would be meaningless.

 ![Capture1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/c/aceff2d9e50fa403c77eed5d5813529785335886.png)

This is an extract of my subflow. As you see, I define a global name for each parameter. For instance, I defined _Etuve.MHpPrinc_, then I send the value by MQTT. Doing that within the subflow, I thought it will give me the value for every chamber, because their is one subflow linked to one chamber (see flow).  
I can't achieve to print the value of _Etuve.MHpPrinc_ for the chamber 1 and 2 separately, it's either 1 or 2.

Does it more clear ?

---

<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: [29 February 2024 13:50 UTC](https://discourse.nodered.org/t/inject-dynamical-variables-in-a-node/85957/5 "2024-02-29T13:50:57Z")

</div>

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

output 1 will be triggered for EVERY subflow.

either move the MQTT stuff out of the subflow OR test the topic using a switch node AFTER the subflow.

---

<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: [14 March 2024 13:51 UTC](https://discourse.nodered.org/t/inject-dynamical-variables-in-a-node/85957/6 "2024-03-14T13:51:41Z")

</div>

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