How to calculate the length of msg.payload?

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 :slight_smile:

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.

msg.payload = msg.payload.padStart(5,"0")

1 Like
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?

1 Like


Many thanks to you! :slight_smile: both works
When I tried myself to use msg.payload.length I still received original value msg.payload...