Hi all
I'm struggling with calculate length msg.payload. Is there any way to do it? In addition I want to put 0 in front of msg.payload in case of length of msg.payload is lower than 5 characters. Msg.payload is string.
Thanks for help
Hi all
I'm struggling with calculate length msg.payload. Is there any way to do it? In addition I want to put 0 in front of msg.payload in case of length of msg.payload is lower than 5 characters. Msg.payload is string.
Thanks for help
Strings and arrays have a length property.
msg.payload.length
will return the string length as an integer
You can also just pad the start of the string if less than x chars.
str.padstart()
msg.payload = msg.payload.padStart(5,"0")
var alt_var = msg.payload ;
let msglength = alt_var.length ;
If ( msglength < 5 ) {
msg.payload = "0" + alt_var ;
}
return msg;
This would be a way to do it but best to read up in javascript functions?