How to extract a string value

Hello Everybody,

I am working on a RS485 serial to ifttt project.

I receive the string "INPUTSTATUS 0924400005454010004411444100444111447855" and I would like to extract only the second number, in this example "9".

I am using the palette "string" but I don't know how to setup it to extract the value.

Can you please help me?

I'd use a function node. Assuming the data is in msg.payload:

let number = msg.payload.split(' ')[1].substr(1,1)

you probably mean (to be complete for mike829)

msg.payload = msg.payload.split(' ')[1].substr(1,1);
return msg;
2 Likes

Yes, should have documented better. :slight_smile: Thanks @dceejay.

Thank you so much, so if i put substr(0,1); I read the first value, if i put substr(1,1); I read the second value, if I put substr(2,1); I will read the third value. Is it correct?

Correct..

Thank you so much for your help. It works fine...

Sorry again...Is it also possible to convert the hex I've extract to binary? If it is 9 I would like to obtain 1001.
So I will have

1 > state input 1 on
0 > state input 2 off
0 > state input 3 off
1 > state input 4 on

Now I am using the code in hex: If it is 9> 4 and 1 are on. If it is 1 > 1 is on. If it is 8 > 4 is on. If it is 3 > 1 and 2 are on.

Maybe this would help?

Thanks for the link. I will check into it

now I am trying to extract the string: { var: "5555 INFO 1" } to obtain only 5555 INFO 1.

I edit the function '' msg.payload = msg.payload.split(" ")[1].substr(5,10);
return msg;'''

but doesn't work...

That’s not a string. msg.payload in that example is an object.
Have you read the page “ working with messages” in the docs? Which would help you to differentiate them.
the bit you want is msg.payload.var

I will look into “ working with messages”, thank you. I think that I can use string palette and chomp. Thank you so much.

or as i said before just use msg.payload.var

It works using mg.payload.var and it is perfect to use chomp function to cut var: " and the final". I obtained the perfect string at the output. So thank you so much for the help.

1 Like