How to add a prefix to msg.payload

hi i know its going to be something simple what i what is to help me with:

i want to convert the number 1-255 that comes from a dashboard slider to 0:number for exmle 0:255 and then send it via mqtt ?
how is that possible ?
thanks in advance
chris giaitzoglou

You could use a change node with a jsonata expression
or a change node with a cuple rules to set x = '0:qaz then replace qaz with msg.payload then move x to msg.payload
or a function node

what have you attemped so far?

wow i never would have through to use a change node like this ever! but it makes really sense the logic thanks for that you help a lot also to think differently .
As you may have guess i am newbie i also just starting to learn java script , itried a function node where i had something like this
var msg=msg.payload
var newMsg= 0;
var newMsg1=":"
return [newMsg,newMsg1,msg]
but it diidnt work (in my head the logic was good but it got me to an error)
thanks again for your help

Try a change node with a jaonata item and a value of “0:”&payload

If you want to use a function then it would be something like

msg.payload = "0:" + msg.payload;
return msg;

or if you are using a current version of node.js then you can use the new string interpolation syntax which is neater

msg.payload = `0:${msg.payload}`;
return msg;
1 Like

There are probably a dozen ways to concatenate your incoming payload with another string -- besides those already suggested, I think the simplest is the template node. You can use the following "mustache" syntax to get the output you want:

image

Note that the msg. prefix is not used inside the variable substitution braces, e.g. {{ payload }}, since all the named fields are expected to come from the incoming msg object.

1 Like