# IP Address as a variable

**URL:** <https://discourse.nodered.org/t/ip-address-as-a-variable/53225>\
**Category:** General\
**Created:** [4 November 2021 18:11 UTC](https://discourse.nodered.org/t/ip-address-as-a-variable/53225 "2021-11-04T18:11:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![traderjoe](https://avatars.discourse-cdn.com/v4/letter/t/f9ae1b/32.png) [@traderjoe](https://discourse.nodered.org/u/traderjoe)\
**Post date:** [4 November 2021 18:11 UTC](https://discourse.nodered.org/t/ip-address-as-a-variable/53225/1 "2021-11-04T18:11:21Z")

</div>

I guess I'm reopening up my question from before:

> [@Modbus TCP IP a variable?](https://discourse.nodered.org/t/modbus-tcp-ip-a-variable/38290):
>
> I'm using Modbus TCP. Can the client IP be a variable? [image]

I am collecting data using Modbus TCP. I'd like the remain flexible and let the user enter their own IP address. it was brought up to use environmental variables. But maybe I'm not using the variable properly:

I have a text entry and it's payload is set to $IP  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/7/97ec681323c06d5bb9e1caac8bd39188956c6074.png)

Then under host I have this  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/a/ea8c5e832dfcf437aa035ad59b62d7eeac498174.png)

calling the IP.

```auto
[{"id":"dbf7fbb9.bab5b8","type":"tab","label":"Configuration","disabled":false,"info":""},{"id":"f5547811.4bbe48","type":"ui_text_input","z":"dbf7fbb9.bab5b8","name":"IP Address Entry","label":"Enter IP Address","tooltip":"","group":"b3ef20e6.67bde","order":0,"width":0,"height":0,"passthru":true,"mode":"text","delay":300,"topic":"test","x":210,"y":120,"wires":[["d9b2798f.888c48","d2eccda6.fa55a","8c43767.d750388"]]},{"id":"d9b2798f.888c48","type":"function","z":"dbf7fbb9.bab5b8","name":"","func":"\nglobal.set(\"IPAddress\", msg.payload)\n\n// global.set(\"1750_1_dc_sp\", msg.payload);\n\n// msg_o = {topic: \"DC Setpoint\", payload: msg.payload};\n\nreturn false;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":400,"y":340,"wires":[[]]},{"id":"d2eccda6.fa55a","type":"ui_text","z":"dbf7fbb9.bab5b8","group":"b3ef20e6.67bde","order":1,"width":0,"height":0,"name":"","label":"text","format":"{{msg.payload}}","layout":"row-spread","x":680,"y":240,"wires":[]},{"id":"8c43767.d750388","type":"change","z":"dbf7fbb9.bab5b8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"IP","tot":"env"}],"action":"","property":"","from":"","to":"","reg":false,"x":700,"y":280,"wires":[[]]},{"id":"38b8a176.32dfce","type":"modbus-read","z":"dbf7fbb9.bab5b8","name":"1750+ 1","topic":"1750 1","showStatusActivities":false,"logIOActivities":false,"showErrors":false,"unitid":"0","dataType":"Coil","adr":"19","quantity":"27","rate":"1","rateUnit":"s","delayOnStart":false,"startDelayTime":"","server":"fdbc6bac.988e58","useIOFile":false,"ioFile":"","useIOForPayload":false,"emptyMsgOnFail":false,"x":620,"y":340,"wires":[[],[]]},{"id":"13f7f91a.ce2587","type":"debug","z":"dbf7fbb9.bab5b8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":670,"y":100,"wires":[]},{"id":"b3ef20e6.67bde","type":"ui_group","name":"Configuration","tab":"a2c64017.1f61b","order":3,"disp":true,"width":"6","collapse":false},{"id":"fdbc6bac.988e58","type":"modbus-client","name":"1750+","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":false,"queueLogEnabled":false,"tcpHost":"${IP}","tcpPort":"502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","unit_id":1,"commandDelay":1,"clientTimeout":1000,"reconnectOnTimeout":true,"reconnectTimeout":2000,"parallelUnitIdsAllowed":true},{"id":"a2c64017.1f61b","type":"ui_tab","name":"Configuration","icon":"dashboard","disabled":false,"hidden":false}]

```

Where am I going wrong?

This person did something similar. I thought I did everything the same, I guess not:

> [@Assign IP and Port of MQTT node dynamically](https://discourse.nodered.org/t/assign-ip-and-port-of-mqtt-node-dynamically/34890):
>
> I need to assign IP and Port values (rectangles in picture) of MQTT node depending on a value from UI. Is there a way to do so? [Capture]

---

<div class="post-metadata">

**Author:** ![traderjoe](https://avatars.discourse-cdn.com/v4/letter/t/f9ae1b/32.png) [@traderjoe](https://discourse.nodered.org/u/traderjoe)\
**Post date:** [8 November 2021 13:41 UTC](https://discourse.nodered.org/t/ip-address-as-a-variable/53225/2 "2021-11-08T13:41:11Z")

</div>

self bump

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [8 November 2021 14:09 UTC](https://discourse.nodered.org/t/ip-address-as-a-variable/53225/3 "2021-11-08T14:09:24Z")

</div>

For this to work you will need to set the system environment variable and then restart node red.

---

<div class="post-metadata">

**Author:** ![traderjoe](https://avatars.discourse-cdn.com/v4/letter/t/f9ae1b/32.png) [@traderjoe](https://discourse.nodered.org/u/traderjoe)\
**Post date:** [8 November 2021 14:13 UTC](https://discourse.nodered.org/t/ip-address-as-a-variable/53225/4 "2021-11-08T14:13:28Z")

</div>

How do I set the system environment variable?

How do I restart node red from the node red dashboard?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [8 November 2021 14:25 UTC](https://discourse.nodered.org/t/ip-address-as-a-variable/53225/5 "2021-11-08T14:25:01Z")

</div>

> [@traderjoe](#):
>
> How do I set the system environment variable?

I don't think it is possible from within node-red.

> [@traderjoe](#):
>
> How do I restart node red from the node red dashboard?

Restarting node-red has been discussed here many times, a forum search should find it easily.

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [15 November 2021 18:20 UTC](https://discourse.nodered.org/t/ip-address-as-a-variable/53225/7 "2021-11-15T18:20:04Z")

</div>

> [@traderjoe](#):
>
> I am collecting data using Modbus TCP. I'd like the remain flexible and let the user enter their own IP address.

Maybe this is possible to change the modbus config settings  
with a node in the collection called **Modbus Flex Connector**

I havent used it in a real world example but i did some tests with a modbus software simulator  
and seems to work.  
Here is the flow that sets, as an example, a different port for that modbus config.

**Flow:**

```auto
[{"id":"c312db7a02c1f4dd","type":"modbus-flex-sequencer","z":"5847b7aa62131d37","name":"","sequences":[{"name":"1","unitid":"1","fc":"FC1","address":"1","quantity":"1"}],"server":"ebeab3422d31a778","showStatusActivities":true,"showErrors":true,"logIOActivities":false,"useIOFile":false,"ioFile":"","useIOForPayload":false,"emptyMsgOnFail":false,"keepMsgProperties":false,"x":540,"y":80,"wires":[["552a9e388d7eda29"],[]]},{"id":"99da7dfd84cbd186","type":"inject","z":"5847b7aa62131d37","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":160,"y":80,"wires":[["6c8c4aeace8044d9"]]},{"id":"6c8c4aeace8044d9","type":"function","z":"5847b7aa62131d37","name":"","func":"msg.sequences = [\n {\n name: \"Foo\",\n unitid: 1,\n fc: 3,\n address: 1,\n quantity: 1\n },\n {\n name: \"Bar\",\n unitid: 1,\n fc: 3,\n address: 10,\n quantity: 2\n },\n {\n name: \"Baz\",\n unitid: 1,\n fc: 'FC3',\n address: 20,\n quantity: 3\n }]\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":320,"y":80,"wires":[["c312db7a02c1f4dd"]]},{"id":"e387b5cf3a56528a","type":"debug","z":"5847b7aa62131d37","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1130,"y":80,"wires":[]},{"id":"552a9e388d7eda29","type":"join","z":"5847b7aa62131d37","name":"","mode":"custom","build":"object","property":"responseBuffer.buffer","propertyType":"msg","key":"modbusRequest.name","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"3","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":770,"y":80,"wires":[["0cd883411cc554c7"]]},{"id":"0cd883411cc554c7","type":"change","z":"5847b7aa62131d37","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"responseBuffer.buffer","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":940,"y":80,"wires":[["e387b5cf3a56528a"]]},{"id":"602c2fb9189b3c62","type":"modbus-flex-getter","z":"5847b7aa62131d37","d":true,"name":"","showStatusActivities":true,"showErrors":true,"logIOActivities":false,"server":"ebeab3422d31a778","useIOFile":false,"ioFile":"","useIOForPayload":false,"emptyMsgOnFail":false,"keepMsgProperties":false,"x":620,"y":200,"wires":[["167f353c05c28461"],[]]},{"id":"9eb2b47107b96ed8","type":"inject","z":"5847b7aa62131d37","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":250,"y":200,"wires":[["2996681554f2e749"]]},{"id":"2996681554f2e749","type":"function","z":"5847b7aa62131d37","name":"","func":"msg.payload = {\n name: \"Foo\",\n fc: 3,\n address: 1,\n unitid: 1,\n quantity: 10,\n}\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":410,"y":200,"wires":[["602c2fb9189b3c62"]]},{"id":"167f353c05c28461","type":"debug","z":"5847b7aa62131d37","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":910,"y":180,"wires":[]},{"id":"5e8844296c95858c","type":"modbus-flex-connector","z":"5847b7aa62131d37","name":"","maxReconnectsPerMinute":4,"emptyQueue":false,"showStatusActivities":false,"showErrors":false,"server":"ebeab3422d31a778","x":650,"y":360,"wires":[[]]},{"id":"de343ff69f1f4da3","type":"inject","z":"5847b7aa62131d37","name":"Device port 502","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":260,"y":320,"wires":[["b8f8914c9d5d69ae"]]},{"id":"cd1baf7d357f6329","type":"inject","z":"5847b7aa62131d37","name":"Device port 503","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":260,"y":400,"wires":[["a46d180a4664a236"]]},{"id":"b8f8914c9d5d69ae","type":"function","z":"5847b7aa62131d37","name":"","func":"msg.payload = {\n 'connectorType': 'TCP',\n 'tcpHost': '192.168.0.66',\n 'tcpPort': '502',\n 'unitId': 1\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":440,"y":320,"wires":[["5e8844296c95858c"]]},{"id":"a46d180a4664a236","type":"function","z":"5847b7aa62131d37","name":"","func":"msg.payload = {\n 'connectorType': 'TCP',\n 'tcpHost': '192.168.0.66',\n 'tcpPort': '503',\n 'unitId': 1\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":440,"y":400,"wires":[["5e8844296c95858c"]]},{"id":"b90d7f7951814165","type":"comment","z":"5847b7aa62131d37","name":"Change Mod. Config Settings","info":"","x":680,"y":300,"wires":[]},{"id":"ebeab3422d31a778","type":"modbus-client","name":"","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":false,"queueLogEnabled":false,"tcpHost":"192.168.0.66","tcpPort":"502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","serialAsciiResponseStartDelimiter":"","unit_id":"0","commandDelay":"1","clientTimeout":"1000","reconnectOnTimeout":false,"reconnectTimeout":"2000","parallelUnitIdsAllowed":true}]

```

---

<div class="post-metadata">

**Author:** ![traderjoe](https://avatars.discourse-cdn.com/v4/letter/t/f9ae1b/32.png) [@traderjoe](https://discourse.nodered.org/u/traderjoe)\
**Post date:** [16 November 2021 14:08 UTC](https://discourse.nodered.org/t/ip-address-as-a-variable/53225/8 "2021-11-16T14:08:18Z")

</div>

This did it! Thanks!  
I can't believe there was a block that helped with this this entire time!. Thanks for the script too! Helped a lot.

---

<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:** [30 November 2021 14:08 UTC](https://discourse.nodered.org/t/ip-address-as-a-variable/53225/9 "2021-11-30T14:08:56Z")

</div>

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