# How to split UDP message

**URL:** <https://discourse.nodered.org/t/how-to-split-udp-message/49281>\
**Category:** General\
**Created:** [3 August 2021 20:00 UTC](https://discourse.nodered.org/t/how-to-split-udp-message/49281 "2021-08-03T20:00:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dohnalradek](https://avatars.discourse-cdn.com/v4/letter/d/ecc23a/32.png) [@dohnalradek](https://discourse.nodered.org/u/dohnalradek)\
**Post date:** [3 August 2021 20:00 UTC](https://discourse.nodered.org/t/how-to-split-udp-message/49281/1 "2021-08-03T20:00:58Z")

</div>

Im getting UDP message from my weather station in this format:

`payload: "tempinc=25.8 humidityin=52 baromrelhpa=1013.82 baromabshpa=988.01 tempc=17.8 humidity=86 winddir=350 windspeedkmh=1.13 windgustkmh=1.77 maxdailygust=29.45 rainratemm=0.0 eventrainmm=6.3 hourlyrainmm=0.0 dailyrainmm=6.3 weeklyrainmm=23.29 monthlyrainmm=23.29 totalrainmm=396.49 solarradiation=0.00 uv=0 wh65batt=0 freq=868M model=WS2900_V2.01.10 dewptc=15.4 windchillc=17.8 feelslikec=17.8 heatindexc=17.8 windspdkmh_avg10m=0.16 winddir_avg10m=346 windgustkmh_max10m=1.77 brightness=0.0 sunhours=5.7 ptrend1=2 pchange1=0.8 wnowlvl=2 wnowtxt=unstable ptrend3=-1 pchange3=0.6 wproglvl=5 wprogtxt="nice for a long time""`

I need to split it to separate MQTT topics likes:

"FOSHK/tempinc=25.8"  
"FOSHK/humidityin=52"  
...

for using sensors in Home Assistant.

Do you have any idea how to do this?

---

<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:** [3 August 2021 21:06 UTC](https://discourse.nodered.org/t/how-to-split-udp-message/49281/2 "2021-08-03T21:06:32Z")

</div>

Hello Radek ... you need some javascript in a Function node.  
Not the cleanest way to process this but here's my attempt

```auto
[{"id":"ae6ac6774bba3285","type":"inject","z":"9af9141769558c7a","name":"get","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"tempinc=25.8 humidityin=52 baromrelhpa=1013.82 baromabshpa=988.01 tempc=17.8 humidity=86 winddir=350 windspeedkmh=1.13 windgustkmh=1.77 maxdailygust=29.45 rainratemm=0.0 eventrainmm=6.3 hourlyrainmm=0.0 dailyrainmm=6.3 weeklyrainmm=23.29 monthlyrainmm=23.29 totalrainmm=396.49 solarradiation=0.00 uv=0 wh65batt=0 freq=868M model=WS2900_V2.01.10 dewptc=15.4 windchillc=17.8 feelslikec=17.8 heatindexc=17.8 windspdkmh_avg10m=0.16 winddir_avg10m=346 windgustkmh_max10m=1.77 brightness=0.0 sunhours=5.7 ptrend1=2 pchange1=0.8 wnowlvl=2 wnowtxt=unstable ptrend3=-1 pchange3=0.6 wproglvl=5 wprogtxt=\"nice for a long time\"","payloadType":"str","x":370,"y":540,"wires":[["904297a1bdcc7270","be093a984e271310"]]},{"id":"0e45d5c1f62c5a1a","type":"debug","z":"9af9141769558c7a","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":710,"y":540,"wires":[]},{"id":"904297a1bdcc7270","type":"function","z":"9af9141769558c7a","name":"Array to Object","func":"let obj = {};\n\n\nlet wprogtxt = msg.payload.split(\" wprogtxt=\")[1].replace(/\\\"/g, \"\") // special case\nlet data = msg.payload.split(\" wprogtxt=\")[0]\n//node.warn(data);\n\ndata = data.split(\" \")\ndata.forEach( el => {\n let arr = el.split(\"=\")\n obj[`FOSHK/${arr[0]}`] = !isNaN(arr[1]) ? Number(arr[1]) : arr[1]\n})\n\nobj[`FOSHK/wprogtxt`] = wprogtxt; // add back the special case\n\nmsg.payload = obj;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":540,"y":540,"wires":[["0e45d5c1f62c5a1a"]]},{"id":"be093a984e271310","type":"debug","z":"9af9141769558c7a","name":"1","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":480,"y":460,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![dohnalradek](https://avatars.discourse-cdn.com/v4/letter/d/ecc23a/32.png) [@dohnalradek](https://discourse.nodered.org/u/dohnalradek)\
**Post date:** [4 August 2021 05:08 UTC](https://discourse.nodered.org/t/how-to-split-udp-message/49281/3 "2021-08-04T05:08:20Z")

</div>

It works perfectly. Thanks!!

---

<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:** [3 October 2021 05:08 UTC](https://discourse.nodered.org/t/how-to-split-udp-message/49281/4 "2021-10-03T05:08:26Z")

</div>

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