Split a Message

Hi everyone,

i am trying to get the last 3 positions of the payload of this message

31/8/2020 16:37:23node: SWITCH 5test : msg.payload : string[25]

""DB0020_AUTO".PRUEBA5,101"

I need 101 value, but i dont want a split after "," because in this way i would get 2 messages and i dont kwow how to differenciate between the variable name ("DB0020_AUTO".PRUEBA5) and the value 101

in other way i wrote a function in order to get the last positions of the array but doesnt work

message = msg.payload
A =
A[0] = message[25]
A[1] = message[24]
A[2] = message[23]

msg=A;

return msg;

thank you for any clue in any way

In a function node you could use:

m = msg.payload.split(",")
msg.value = parseInt(m[1])

return msg

Output

Screenshot 2020-08-31 at 16.56.27

1 Like

@bakman2 No offence but I really think you should use a keyword to declare the variables in your examples, just to not teach bad practices to people new to JS. It's not Python even if Node-RED doesn't use a strict interpreter :slightly_smiling_face:.

1 Like

Or if you want the answer in the payload then

msg.payload = parseInt(msg.payload.split(",")[1])
return msg

If you want the answer left as a string then

msg.payload = msg.payload.split(",")[1]
return msg
2 Likes

No offence taken, it is a keyword: short for message :wink:

1 Like

:roll_eyes:

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.