How to add a prefix to msg.payload

#1

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

0 Likes

#2

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?

0 Likes

#3

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

0 Likes

#4

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

0 Likes

#5

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;
0 Likes

#6

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