# Inject dynamic variables in s7 nodes

**URL:** https://discourse.nodered.org/t/inject-dynamic-variables-in-s7-nodes/95536
**Category:** Industrial
**Created:** [20 February 2025 13:51 UTC](https://discourse.nodered.org/t/inject-dynamic-variables-in-s7-nodes/95536 "2025-02-20T13:51:56Z")
**Posts on this page:** 9
**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: [20 February 2025 13:51 UTC](https://discourse.nodered.org/t/inject-dynamic-variables-in-s7-nodes/95536/1 "2025-02-20T13:51:56Z")

</div>

Hello,

I created a flow that read PLC data with the s7 library.  
I would like to read the chamber configuration (ip, port, timeout and reading cycle) from a file and then inject those values in the s7 In node, rather than put them with hard code. I have read on the forum that is kind of possible using control s7 node, but I didn't really understand how to do it.

Here is a flow example (without reading file but just with an injection node) :

 ![example_flow](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/1/91d5da8f20ff44ae6f70d12bfd2bd63a6a22ecbc.png)

The PLC configuration would be set in a json file like this :

```auto
msg.payload = {
    "ACS": {
        "ip": "XX.XX.XX.XX",
        "port": 102,
        "reading_cycle": 10000,
        "timeout": 500
    }
}
return msg;

```

And I would like to inject the values like this :

 ![config_plc](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/b/8b7d5d0aff381c207bc6b837d7fdd1129ccd3550.png)

How could I handle this problem ?

Thank you for your help.

---

<div class="post-metadata">

### Author: ![KarolisL](https://avatars.discourse-cdn.com/v4/letter/k/a4c791/32.png) [@KarolisL](https://discourse.nodered.org/u/KarolisL)
#### Post date: [20 February 2025 20:00 UTC](https://discourse.nodered.org/t/inject-dynamic-variables-in-s7-nodes/95536/2 "2025-02-20T20:00:58Z")

</div>

Long story short - not possible with current s7 nodes.  
Only option is to use npm package nodes7.  
Check this first:

> <https://github.com/plcpeople/nodeS7/issues/129>
>
> Hey! I'm working with your node on node-red for reading from a plc S7-1200, rack… 0 and slot 1 with external functions. But every time I send a readAllItems I get the response I want but the connection resets every time and never closes, even with dropConnection and connectionCleanup... I've been trying to solve it for a few days now, if anyone has any ideas...
> 
> \`\`\`javascript
> var devices = msg.payload; // devices example: {"delta":0,"name":"10.255.3.214","rack":0,"slot":1,"data":"Q0.0","ip":"10.255.3.214","port":102}
> 
> msg = \[\];
> 
> let nodes7 = global.get('nodes7');
> var conn = new nodes7({ globalTimeout: 2000 });
> var doneReading = false;
> 
> devices.forEach((/\*\* @type {{ modbus\_id: any; modbus\_ip: any; modbus\_port: any; address: any; quantity: any; }} \*/ dev) =\> {
> var variables = {
> DATA: dev.data
> };
>    
> conn.initiateConnection({ port: dev.port, host: dev.ip, rack: dev.rack, slot: dev.slot, debug: false }, connected);
> function connected(err) {
> if (typeof (err) !== "undefined") {
> // PLC FORA DO ALCANCE
> //console.log(err);
> node.error({
> "ip": dev.ip,
> "port": dev.port,
> "message": "ERRO! Falha na leitura do host " + dev.ip + "!",
> "error": err.code
> });
> flow.set("aguarda", false);
> conn.dropConnection();
> conn.connectionCleanup();
> }
> conn.setTranslationCB(function (tag) { return variables\[tag\]; });
> conn.addItems('DATA');
> conn.readAllItems(valuesReady);
> }
> 
> function valuesReady(anythingBad, values) {
> if (anythingBad) { console.log("ERRO! Falha na leitura do host."); flow.set("aguarda", false); conn.dropConnection(); conn.connectionCleanup(); }
> //console.log(values);
> if (values.DATA != "BAD 255") {
> node.send({
> "topic": "TCP Response",
> "host": dev.ip,
> "port": dev.port,
> "rack": dev.rack,
> "slot": dev.slot,
> "data": dev.data,
> "payload": values.DATA
> });
> }
> doneReading = true;
> conn.dropConnection();
> conn.connectionCleanup();
> }
>    
>    
> });
> 
> return msg;
> 
> \`\`\`

My own examples:

> [@Siemens reading from PLC with nodeS7 npm](https://discourse.nodered.org/t/siemens-reading-from-plc-with-nodes7-npm/68926):
>
> Hello, I needed dynamic addressing of Siemens PLC, so standard NR nodes are not a case. I've imported [GitHub - plcpeople/nodeS7: Node.JS library for communication to Siemens S7 PLCs](https://github.com/plcpeople/nodeS7) as external module. Here is the simple flow which closes connection after data is read. It is necessary because S7 connections accumulate until ECONNRESET is thrown and NR needs to be restarted. My end goal was to read identical structures of data with offset for each module. Simple flow is here, PLC IP…

---

<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: [20 February 2025 20:42 UTC](https://discourse.nodered.org/t/inject-dynamic-variables-in-s7-nodes/95536/3 "2025-02-20T20:42:22Z")

</div>

There is an S7 package that supports runtime variable readings but I forget which one. Try some of the other contrib packages.

---

<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: [6 March 2025 13:16 UTC](https://discourse.nodered.org/t/inject-dynamic-variables-in-s7-nodes/95536/4 "2025-03-06T13:16:13Z")

</div>

Hello !

I have taken your tips into considerations, thanks. I have created my own node in order to put dynamic variables in the configuration file, which initiates the connection with the PLC. It is also possible to list all the variables you want to read in the same file. The output of this node will deliver an object with all those information.

For the ones who have the same issue as me, you can install it with `npm i nodes7-custom-node`.  
For more information, check the documentation : [https://www.npmjs.com/package/nodes7-custom-node](https://www.npmjs.com/package/nodes7-custom-node)

Thank you !

---

<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: [6 March 2025 13:26 UTC](https://discourse.nodered.org/t/inject-dynamic-variables-in-s7-nodes/95536/5 "2025-03-06T13:26:28Z")

</div>

Hey @MystiiK - it looks like you have created a contrib for node-red.

Great stuff. 👏👏

A few pointers if you dont mind:

The name of the node " **nodes7-custom-node**" is not really in line with Node-RED naming guidelines. The recommendation for naming node-RED contrib nodes is to publish it as `@scopename/node-name`. I feel something like `@mystiik/node-red-s7-dynamic` or similar may be better suited? (just my opinion)

Also, since the readme does not mention `node-red`, I actually had to look at the source to realise it was an installable contrib node for Node-RED.

It should also be tagged with `node-red` in the package.

Lastly, I note there is no repository link specified in your package. This is very useful for users to let you know if there is an issue.

---

<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: [6 March 2025 13:42 UTC](https://discourse.nodered.org/t/inject-dynamic-variables-in-s7-nodes/95536/6 "2025-03-06T13:42:16Z")

</div>

Also, did you try the existing packages?

> [@Steve-Mcl](#):
>
> Try some of the other contrib packages.

Like this one: [node-red-contrib-s7-dynamic - npm](https://www.npmjs.com/package/node-red-contrib-s7-dynamic)

---

<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: [6 March 2025 15:26 UTC](https://discourse.nodered.org/t/inject-dynamic-variables-in-s7-nodes/95536/7 "2025-03-06T15:26:37Z")

</div>

Thank you for your recommendations. So I renamed the package and the node properly, but I needed to create another package for that : `npm i node-red-contrib-nodes7-dynamic`.  
The link : [https://www.npmjs.com/package/node-red-contrib-nodes7-dynamic](https://www.npmjs.com/package/node-red-contrib-nodes7-dynamic)  
I've also put the keywords in the package, and I deleted the previous one.

To be honest, I didn't try the other packages for two reasons.  
First, because I didn't find a good package for my use case (the one you showed was created just one week ago).  
Secondly, as an apprentice, I wanted to take the opportunity to create a whole library by myself, for the exercise. It was very rewarding to do it and it helps to understand better how works NodeRED 🙂

Thank you for your help !

---

<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: [6 March 2025 15:35 UTC](https://discourse.nodered.org/t/inject-dynamic-variables-in-s7-nodes/95536/8 "2025-03-06T15:35:18Z")

</div>

> [@MystiiK](#):
>
> To be honest, I didn't try the other packages for two reasons.  
> First, because I didn't find a good package for my use case (the one you showed was created just one week ago).

I realise that was just added, but there were others and they do support dynamic (runtime specifiable addressing)

> [@MystiiK](#):
>
> as an apprentice, I wanted to take the opportunity to create a whole library by myself, for the exercise. It was very rewarding to do it and it helps to understand better how works NodeRED 🙂

Fair enough.

> [@MystiiK](#):
>
> Thank you for your recommendations. So I renamed the package and the node properly, but I needed to create another package for that : `npm i node-red-contrib-nodes7-dynamic`.  
> The link : [node-red-contrib-nodes7-dynamic - npm](https://www.npmjs.com/package/node-red-contrib-nodes7-dynamic)  
> I've also put the keywords in the package, and I deleted the previous one.

No worries.

A few other things you may wish to address.

1. you have not added a link to the source repository (ie. no link to the github/gitlab/whatever repository in your package)
  1. This will mark your package down in the node-red flows catalog scoring
  2. Example of high scoring package: [node-red-contrib-telegrambot (node) - Node-RED](https://flows.nodered.org/node/node-red-contrib-telegrambot/scorecard)
  3. Example of a low scoring package: [node-red-contrib-example-lower-case-mmm (node) - Node-RED](https://flows.nodered.org/node/node-red-contrib-example-lower-case-mmm/scorecard)

2. you should consider adding an examples folder and adding 1 or 2 example flows  
5. These become available to users in the `import` dialog (`ctrl-i`)  
6. Not having this will mark your package down in the node-red flows catalog scoring
3. you should add your node to the [flows catalog](https://flows.nodered.org/add/node) so that users can install it via "Manage Palette"

---

<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: [20 March 2025 15:35 UTC](https://discourse.nodered.org/t/inject-dynamic-variables-in-s7-nodes/95536/9 "2025-03-20T15:35:22Z")

</div>

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