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

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.

I can’t follow your code

From the docs https://nodered.org/docs/writing-functions#storing-data

To set a value:

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

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.

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