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

Hi,

I have a TCP In node is reading 2 values below:
image

"DMM1:0"
"DMM2:0"

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

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

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

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.

1 Like

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

Now my debug value is like this:

image

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 ```)

``` 
   code goes here 
```

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

See this post for more details - How to share code or flow json

1 Like

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.

[{"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

1 Like

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

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