# TCP In node read 2 values how to separate into 2 different global variable?

**URL:** <https://discourse.nodered.org/t/tcp-in-node-read-2-values-how-to-separate-into-2-different-global-variable/81755>\
**Category:** General\
**Created:** [4 October 2023 07:33 UTC](https://discourse.nodered.org/t/tcp-in-node-read-2-values-how-to-separate-into-2-different-global-variable/81755 "2023-10-04T07:33:27Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Open1333](https://avatars.discourse-cdn.com/v4/letter/o/278dde/32.png) [@Open1333](https://discourse.nodered.org/u/Open1333)\
**Post date:** [4 October 2023 07:33 UTC](https://discourse.nodered.org/t/tcp-in-node-read-2-values-how-to-separate-into-2-different-global-variable/81755/1 "2023-10-04T07:33:27Z")

</div>

Hi,

I have a TCP In node is reading 2 values below:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/3/23d61c6508d1e2f9f8defecf1648756a22177c88.png)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/5/45111f02ee4d4cde08e9a710b4753b8ff304ea02.jpeg)

"DMM1:0"  
"DMM2:0"

Is it possible to separate them into 2 different global variable?

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [4 October 2023 07:50 UTC](https://discourse.nodered.org/t/tcp-in-node-read-2-values-how-to-separate-into-2-different-global-variable/81755/2 "2023-10-04T07:50:48Z")

</div>

Sure it is possible and many ways to do it.  
here is one example

```auto
[{"id":"5b26eba22615a23e","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"DMM1:0","payloadType":"str","x":180,"y":560,"wires":[["22c3e06b74b92d0b"]]},{"id":"22c3e06b74b92d0b","type":"csv","z":"d1395164b4eec73e","name":"","sep":":","hdrin":"","hdrout":"none","multi":"one","ret":"\\n","temp":"","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":350,"y":580,"wires":[["e1cdf1acdca5c81e"]]},{"id":"8f565fb3c0f94299","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"DMM2:0","payloadType":"str","x":180,"y":600,"wires":[["22c3e06b74b92d0b"]]},{"id":"e1cdf1acdca5c81e","type":"change","z":"d1395164b4eec73e","name":"","rules":[{"t":"set","p":"dim[msg.payload.col1]","pt":"global","to":"payload.col2","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":640,"wires":[["07208ba47a0cf097"]]},{"id":"07208ba47a0cf097","type":"debug","z":"d1395164b4eec73e","name":"debug 344","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":590,"y":580,"wires":[]}]

```

 ![chrome_screenshot_1696405793100](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/0/c0ca1142050c3e73138efd9a69ae6c502d14a3e9.png)

Global `dim.DIMM1` & `dim.DIMM2`

[edit] If you have control over device sending the strings it might be easier if you can send JSON strings  
e.g `{"DIMM1":0}` which you could then pass through a json node.

---

<div class="post-metadata">

**Author:** ![Open1333](https://avatars.discourse-cdn.com/v4/letter/o/278dde/32.png) [@Open1333](https://discourse.nodered.org/u/Open1333)\
**Post date:** [4 October 2023 08:31 UTC](https://discourse.nodered.org/t/tcp-in-node-read-2-values-how-to-separate-into-2-different-global-variable/81755/3 "2023-10-04T08:31:00Z")

</div>

> [@E1cid](#):
>
> ou have control over device sending the strings it might be easier if you can send JSON strings  
> e.g `{"DIMM1":0}` which you could then pass through a json node.

I think i am able to change to a JSON strings, after changing how should i pass through a JSON node?

---

<div class="post-metadata">

**Author:** ![Open1333](https://avatars.discourse-cdn.com/v4/letter/o/278dde/32.png) [@Open1333](https://discourse.nodered.org/u/Open1333)\
**Post date:** [4 October 2023 08:46 UTC](https://discourse.nodered.org/t/tcp-in-node-read-2-values-how-to-separate-into-2-different-global-variable/81755/4 "2023-10-04T08:46:54Z")

</div>

Now my debug value is like this:

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

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [4 October 2023 09:18 UTC](https://discourse.nodered.org/t/tcp-in-node-read-2-values-how-to-separate-into-2-different-global-variable/81755/5 "2023-10-04T09:18:47Z")

</div>

Without seeing what you did, it's hard to tell you what you might have done wrong.

Create a small flow demonstrating your problem and export it and attach to a reply. (add a `debug` node to your `TCP` node to capture some data. Then put that in an `inject` node in the sample flow)

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote `````)

````auto
``` 
   code goes here 
```

````

You can edit and correct your post by clicking the pencil ✏ icon.

See this post for more details - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [4 October 2023 09:36 UTC](https://discourse.nodered.org/t/tcp-in-node-read-2-values-how-to-separate-into-2-different-global-variable/81755/6 "2023-10-04T09:36:11Z")

</div>

> [@Open1333](#):
>
> I think i am able to change to a JSON strings, after changing how should i pass through a JSON node?

Just pass the JSON string through a JSON node, the node will then convert it to an Javascript object. You can then use a change node to move any object property to any context var you wish. In my example i set all var in a main property called `dim`, you can use any name you wish. This has a benefit that 1, all your vars are kept in a single property for easy identification 2, easy to delete all context vars when needed.

```auto
[{"id":"1b804bee1d9852dd","type":"inject","z":"d1395164b4eec73e","name":"JSON","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"topic\":\"DMM1\",\"current\":0}","payloadType":"str","x":110,"y":680,"wires":[["80cac75c56de3c12"]]},{"id":"80cac75c56de3c12","type":"switch","z":"d1395164b4eec73e","name":"","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"UncertainInitialValue","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":250,"y":680,"wires":[["2237129905b5e351"],["06f0e31a1cec356c"]]},{"id":"55e88695b11a332f","type":"inject","z":"d1395164b4eec73e","name":"uncertain","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"blah blau UncertainInitialValue blah blah\"","payloadType":"str","x":100,"y":740,"wires":[["80cac75c56de3c12"]]},{"id":"06f0e31a1cec356c","type":"json","z":"d1395164b4eec73e","name":"","property":"payload","action":"","pretty":false,"x":270,"y":760,"wires":[["a46a068cb5d423fa"]]},{"id":"2237129905b5e351","type":"debug","z":"d1395164b4eec73e","name":"filtered out messages","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":420,"y":640,"wires":[]},{"id":"a46a068cb5d423fa","type":"change","z":"d1395164b4eec73e","name":"","rules":[{"t":"set","p":"dim[msg.payload.topic]","pt":"global","to":"payload.current","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":500,"y":760,"wires":[["4a9e82a69475d891"]]},{"id":"4a9e82a69475d891","type":"debug","z":"d1395164b4eec73e","name":"debug 344","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":650,"y":800,"wires":[]}]

```

[edit] You will need to filter those `uncertainInitilizeValue` messages out also. Added example in flow example above

---

<div class="post-metadata">

**Author:** ![Open1333](https://avatars.discourse-cdn.com/v4/letter/o/278dde/32.png) [@Open1333](https://discourse.nodered.org/u/Open1333)\
**Post date:** [5 October 2023 03:23 UTC](https://discourse.nodered.org/t/tcp-in-node-read-2-values-how-to-separate-into-2-different-global-variable/81755/7 "2023-10-05T03:23:12Z")

</div>

Thank you @E1cid for your idea on filtering the message, it helped!

---

<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:** [19 October 2023 03:23 UTC](https://discourse.nodered.org/t/tcp-in-node-read-2-values-how-to-separate-into-2-different-global-variable/81755/8 "2023-10-19T03:23:48Z")

</div>

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