UDP Netvar data to JSON for InfluxDB2

Hi,

How can I get the data from tis string back to JSON so I can put the values with the heading into InfluxDB, also without the "REAL"

{"0":["Fan01_BoolP","REAL "],"1":[" Fan01_Power","REAL",0],"2":["Fan01_Current ","REAL",0],"3":[" Fan01_Voltage","REAL",0],"4":[" Fan01_Frequency","REAL",0]}

Thanks

Sy

Send it thru the json node

Thanks for the info,

I have done that, the data comes as an array. I need to format it so I can dump it into InfluxDB. ??

This is how I need the data to look.

msg.payload = [{
    Power: 10',
    Current: 12,
    Voltage: 400,
    Frequency: 50
},
{
    tag1:"DP1",
    tag2:"Motor1"
}];
return msg;

Where did the values come from? In the data you show, Fan01_Power shows a value of 0. Where did the '10' come from?
where did the tags come from?
Will it always be the same number of items in the array?

You can start by using a split node to break up the array
a switch node to get rid of any of the array parts you don't want
a change node to change msg.payload[0] searching for Fan01_ and replace it with nothing, then set msg.topic to msg.payload and finally set msg.payload to the value of msg.payload[2]
Then you can use a join (in manual mode) to create a key/value object - using msg.topic - and send the message after 4 parts.

1 Like

If you are talking about codesys network variables you can use following node that I created some time ago to go from the raw udp data to a parsed object like you wanted:

node-red-contrib-nvl

1 Like

Yes it’s from codesys, I will check later on with this option.

Thank you !

The arrays may be different sizes and will always be in that format. I will give this option a try also to split the payloads in case I don’t want all of the data all of the time.

Thanks

Here is an example using our original data which should work no matter how many items in the array

[{"id":"f6682c5c76791b5e","type":"inject","z":"5168fd6160a74481","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"0\":[\"Fan01_BoolP\",\"REAL \"],\"1\":[\" Fan01_Power\",\"REAL\",10],\"2\":[\"Fan01_Current \",\"REAL\",22],\"3\":[\" Fan01_Voltage\",\"REAL\",0],\"4\":[\" Fan01_Frequency\",\"REAL\",0]}","payloadType":"str","x":170,"y":160,"wires":[["fcd538f35c937d31","268a38eb57176a2b"]]},{"id":"fcd538f35c937d31","type":"debug","z":"5168fd6160a74481","name":"debug 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":440,"y":160,"wires":[]},{"id":"27ce6547c7f62584","type":"debug","z":"5168fd6160a74481","name":"debug 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":660,"y":240,"wires":[]},{"id":"268a38eb57176a2b","type":"json","z":"5168fd6160a74481","name":"","property":"payload","action":"","pretty":false,"x":290,"y":240,"wires":[["27ce6547c7f62584","50921e227f9a421b"]]},{"id":"50921e227f9a421b","type":"split","z":"5168fd6160a74481","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":270,"y":300,"wires":[["a67b1f2cf348d5db","c4a2149cced3e105"]]},{"id":"a67b1f2cf348d5db","type":"debug","z":"5168fd6160a74481","name":"debug 3","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":660,"y":300,"wires":[]},{"id":"2a2a6a0a2d4eabe7","type":"change","z":"5168fd6160a74481","name":"","rules":[{"t":"change","p":"payload[0]","pt":"msg","from":"Fan01_","fromt":"str","to":"","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"payload[0]","tot":"msg"},{"t":"set","p":"payload","pt":"msg","to":"payload[2]","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":440,"wires":[["7e50cacea9ccaeb3","24863b2a47a1f6f1","089e4d9c1cd6e381"]]},{"id":"7e50cacea9ccaeb3","type":"debug","z":"5168fd6160a74481","name":"debug 4","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":660,"y":440,"wires":[]},{"id":"c4a2149cced3e105","type":"switch","z":"5168fd6160a74481","name":"","property":"payload[0]","propertyType":"msg","rules":[{"t":"neq","v":"Fan01_BoolP","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":270,"y":380,"wires":[["2a2a6a0a2d4eabe7"]]},{"id":"24863b2a47a1f6f1","type":"join","z":"5168fd6160a74481","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":390,"y":560,"wires":[["990af7ab478b6ee8"]]},{"id":"990af7ab478b6ee8","type":"debug","z":"5168fd6160a74481","name":"debug 5","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":680,"y":580,"wires":[]},{"id":"089e4d9c1cd6e381","type":"trigger","z":"5168fd6160a74481","name":"","op1":"1","op2":"0","op1type":"val","op2type":"val","duration":"250","extend":"false","overrideDelay":"false","units":"ms","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":120,"y":500,"wires":[["60b17705c78c6fcb"]]},{"id":"60b17705c78c6fcb","type":"change","z":"5168fd6160a74481","name":"","rules":[{"t":"set","p":"complete","pt":"msg","to":"complete","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":130,"y":560,"wires":[["24863b2a47a1f6f1"]]}]

Or possibly cleaner to use msg.part.key and join automatically. No switch node just delete the unwanted _Bool property when adding the new object to the payload array

[{"id":"f6682c5c76791b5e","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"0\":[\"Fan01_BoolP\",\"REAL \"],\"1\":[\" Fan01_Power\",\"REAL\",10],\"2\":[\"Fan01_Current \",\"REAL\",22],\"3\":[\" Fan01_Voltage\",\"REAL\",0],\"4\":[\" Fan01_Frequency\",\"REAL\",0]}","payloadType":"str","x":130,"y":3520,"wires":[["fcd538f35c937d31","268a38eb57176a2b"]]},{"id":"fcd538f35c937d31","type":"debug","z":"bf9e1e33.030598","name":"debug 1","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":440,"y":3440,"wires":[]},{"id":"268a38eb57176a2b","type":"json","z":"bf9e1e33.030598","name":"","property":"payload","action":"","pretty":false,"x":290,"y":3520,"wires":[["27ce6547c7f62584","50921e227f9a421b"]]},{"id":"27ce6547c7f62584","type":"debug","z":"bf9e1e33.030598","name":"debug 2","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":660,"y":3520,"wires":[]},{"id":"50921e227f9a421b","type":"split","z":"bf9e1e33.030598","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":270,"y":3580,"wires":[["a67b1f2cf348d5db","2a2a6a0a2d4eabe7"]]},{"id":"a67b1f2cf348d5db","type":"debug","z":"bf9e1e33.030598","name":"debug 3","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":660,"y":3580,"wires":[]},{"id":"2a2a6a0a2d4eabe7","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"change","p":"payload[0]","pt":"msg","from":"Fan01_","fromt":"str","to":"","tot":"str"},{"t":"set","p":"parts.key","pt":"msg","to":"payload[0]","tot":"msg"},{"t":"set","p":"payload","pt":"msg","to":"payload[2]","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":310,"y":3620,"wires":[["7e50cacea9ccaeb3","24863b2a47a1f6f1"]]},{"id":"7e50cacea9ccaeb3","type":"debug","z":"bf9e1e33.030598","name":"debug 4","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":650,"y":3640,"wires":[]},{"id":"24863b2a47a1f6f1","type":"join","z":"bf9e1e33.030598","name":"","mode":"auto","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":270,"y":3660,"wires":[["b4effb2a.eaea6"]]},{"id":"b4effb2a.eaea6","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"delete","p":"payload.BoolP","pt":"msg"},{"t":"move","p":"payload","pt":"msg","to":"payload[0]","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":310,"y":3700,"wires":[["990af7ab478b6ee8"]]},{"id":"990af7ab478b6ee8","type":"debug","z":"bf9e1e33.030598","name":"debug 5","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":590,"y":3700,"wires":[]}]

Hi,

I have installed your node. Looks like what I'm probably after for the job. However please can you give me an example, I have declared the same variables as the list but get an error. Have I missed something?


Hi,

Its working now. Nice little application for what I am looking for. Thank you very much.

You can find some examples here: https://github.com/Hopperpop/node-red-contrib-nvl/tree/main/examples
It looks like I need to fix something to have them in the Import>Examples menu.

The error you get is because the data length in the udp message doesn't match with the defined structure. The structures is 6 x REAL (4) = 24 bytes, and the packages contains 25 bytes.

As it's working now, can you elaborate what the issue was? Just to know if something needs to be improved.

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