Splitting string into different values

Hi i want to split these values

msg.payload : string[27]

"RELAY-STATE-255,0,0,0,0,OK" the 4 zeros represent each a decimal number from 0-255. i want to isolate these 4 zeros . i managed this with $substring(payload, -9, 1 ) for example . but if the zero becomes lets say a 16 this method does not work .

not a programmer . just trying to seperate these values to set my relays in proper status

add a function node with this code in...

var str = msg.payload;
var parts = str.split(",");
msg.payload = {
  name: parts[0],
  item1: parseInt(parts[1]),
  item2: parseInt(parts[2]),
  item3: parseInt(parts[3]),
  item4: parseInt(parts[4]),
  status: parts[5],
}
return msg;
2 Likes

great ,

the fact its spitting out an error i just ignore it. this is what i get when putting in your code:

5-10-2020 14:59:45node: 2386070d.67c548msg : string[41]

"Input is not a number or parsable string."

5-10-2020 14:59:45node: f736b0a8.0a9fcmsg.payload : Object

object

name: "RELAY-STATE-255"

item1: 0

item2: 0

item3: 0

item4: 1

status: "OK"

now the matter of fact is getting the items sepratly :slight_smile:

That is coming from a different node, note the id number at the top. If you click on that it should highlight the node.

To understand how to use the message properties read the node-red docs page Working with Messages.

1 Like

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