# Writing a function to change the payload into a string with a different format

**URL:** https://discourse.nodered.org/t/writing-a-function-to-change-the-payload-into-a-string-with-a-different-format/10407
**Category:** General
**Created:** [21 April 2019 21:55 UTC](https://discourse.nodered.org/t/writing-a-function-to-change-the-payload-into-a-string-with-a-different-format/10407 "2019-04-21T21:55:40Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![juergens-nodered](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/juergens-nodered/32/7768_2.png) [@juergens-nodered](https://discourse.nodered.org/u/juergens-nodered)
#### Post date: [21 April 2019 21:55 UTC](https://discourse.nodered.org/t/writing-a-function-to-change-the-payload-into-a-string-with-a-different-format/10407/1 "2019-04-21T21:55:40Z")

</div>

Hey,  
I do get pretty accurate MQTT data from my weather station via MQTT but need to format all this content into a string. That's where I am failing.

i do have a payload looking like this:  
21.4.2019, 22:37:55node: Raw messages  
/home/aussen/wetterstation : msg : Object  
object  
topic: "/home/aussen/wetterstation"  
payload: object  
time: "2019-04-21 22:37:54"  
model: "Fine Offset Electronics WH1080/WH3080 Weather Station"  
msg\_type: 0  
id: 144  
temperature\_C: 12  
humidity: 44  
direction\_str: "N"  
direction\_deg: "0"  
speed: 0  
gust: 0  
rain: 103.8  
battery: "OK"  
qos: 0  
retain: false  
\_msgid: "ca74f20.6db601"

I need to convert this into a string which should look like this and the values behind the = should be the value from the payload above.

20;DC;Tunex;ID=7B02;TEMP=0001;HUM=99;BAT=OK;

I createad a function node to store the values into variables. var TEMP = msg.payload.temperature\_C  
var ID = msg.payload.id  
var HUM = msg.payload.humidity  
var WINDIR = msg.payload.direction\_str  
var WINGS = msg.payload.direction\_deg  
var WINSP = msg.payload.speed  
var RAIN = msg.payload.rain  
var BATT = msg.payload.battery

global.set("TEMP","ID","HUM","WINDIR","WINGS","WINSP","RAIN","BATT")

return msg;

I started with a CHANGE NODE to change the current msg.payload into the beginning of the requested string "20;DC;" . Working like a charm.

But the next function node is killing me as I do not get the variables from above concatenated with a string like "TEMP=" and the value of the TEMP add a ";" and go ahead with the HUM... and so on.

---

<div class="post-metadata">

### Author: ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)
#### Post date: [21 April 2019 22:10 UTC](https://discourse.nodered.org/t/writing-a-function-to-change-the-payload-into-a-string-with-a-different-format/10407/2 "2019-04-21T22:10:06Z")

</div>

I can’t follow your code

From the docs [https://nodered.org/docs/writing-functions#storing-data](https://nodered.org/docs/writing-functions#storing-data)

To set a value:

```auto
flow.set("count", 123);

```

Do you need to store the values as global values? If not do t bother and just look to change to the string in a function node.

If you want to end up with a string like you suggest, try googling “concatenate strings javascript” where you should find out an easy way to do it in a function node

---

<div class="post-metadata">

### Author: ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)
#### Post date: [21 April 2019 22:55 UTC](https://discourse.nodered.org/t/writing-a-function-to-change-the-payload-into-a-string-with-a-different-format/10407/3 "2019-04-21T22:55:55Z")

</div>

> [@ukmoose](#):
>
> Do you need to store the values as global values?

I was about to ask the same question as @ukmoose.

You already have an object with several properties. If all you need is to build a string then there are a number of solutions. I would prefer using the template node to generate such a string. No need to use functions neither JavaScript code.

```auto
[{"id":"525d27d5.bbddc8","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"de51d4f4.3b8d48","type":"inject","z":"525d27d5.bbddc8","name":"","topic":"","payload":"{\"time\":\"2019-04-21 22:37:54\",\"model\":\"Fine Offset Electronics WH1080/WH3080 Weather Station\",\"msg_type\":0,\"id\":144,\"temperature_C\":12,\"humidity\":44,\"direction_str\":\"N\",\"direction_deg\":\"0\",\"speed\":0,\"gust\":0,\"rain\":103.8,\"battery\":\"OK\",\"qos\":0,\"retain\":false}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":220,"wires":[["9f0b8c2d.515df"]]},{"id":"9f0b8c2d.515df","type":"template","z":"525d27d5.bbddc8","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"20;DC;Tunex;ID={{payload.id}};TEMP={{payload.temperature_C}};HUM={{payload.humidity}};BAT={{payload.battery}};","output":"str","x":320,"y":220,"wires":[["24746e3.5030592"]]},{"id":"24746e3.5030592","type":"debug","z":"525d27d5.bbddc8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":490,"y":220,"wires":[]}]

```
