Splitting a string after space

I have a incoming msg.payload.Name, which I want to split.
The msg.payload.Name always starts with Watts , which is what I want to split.
Examples for msg.payload.Name are
Watts Power TV
Watts Kitchen TV
So if I have 'Watts Power TV' in msg.payload.Name, I want to have msg.payload.Name 'Power TV'

I have tried this in a function node but it doesnt work

const n = msg.payload.Name;
msg.payload.name = n.slice(msg.payload.Name, 5)
return msg;

First split it, then slice the array, then you can rejoin the array.

const n = msg.payload.Name;
msg.payload.name = n.split(" ").slice(1).join(" ")
return msg;

Or you could use string.replace("Watts ", "")

But why call a function you could just use a change node and search for Watts and replace with nothing

1 Like

If msg.payload.Name ALWAYS starts with "Watts " then your code should be

msg.payload.name = n.slice(6)

Or you could use

msg.payload.name = n.substr(6)

NB you are creating msg.payload.name, not the same thing as msg.payload.Name that you started off with.

It is actually msg.payload.nodeName

so I changed it to

const n = msg.payload.nodeName;
msg.payload.nodeName = n.slice(6)
return msg;

But I am getting a error

TypeError: Cannot read properties of undefined (reading 'slice')

If you use the built in helper tools you will avoid the guess work...

There’s a great page in the docs (Working with messages : Node-RED) that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

BX00Cy7yHi


Basically it is NOT msg.payload.name nor is it msg.payload.nodeName - so use the copy path button :slight_smile:

You are correct ! I did see the copy but never used it (I am still a newbie)

This is from the copy:

{"topic":"sync","_msgid":"64525d3a941a3005","nodeName":"Watts Lounge TV","payload":{"ref":3137,"name":"Watts Lounge TV","location":"Lounge","location2":"AV","value":0,"status":"0 Watts","device_type_string":"Z-Wave Electric Meter","last_change":"/Date(1667909087397+0000)/","relationship":4,"hide_from_view":false,"associated_devices":[3135],"device_type":{"Device_API":256,"Device_API_Description":"Energy API","Device_Type":1,"Device_Type_Description":"Watts","Device_SubType":50,"Device_SubType_Description":""},"device_type_values":null,"UserNote":"","UserAccess":"Any","status_image":"/images/HomeSeer/status/electricity.gif","voice_command":"","misc":4352,"interface_name":"Z-Wave"}}

So I changed the function to

const n = msg.topic.nodeName;
msg.topic.nodeName = n.slice(6);
return msg;

But I still have the same error

It's easier to provide support if you don't keep moving the goalposts. :wink:

Edit - I think you might find it's msg.nodeName. At least it was at one time.

Sorry I find this debug info a bit confusing, as Watts Loung TV is there twice.

If you play a bit with the buttons that SteveMcl showed you, you will see that this particular message has:
msg.topic (equals "sync")
msg.nodeName (equals "Watts Lounge TV")
msg.payload (an object comprising msg.payload.ref, msg.payload.name [ equals "Watts Lounge TV" ], msg.payload.location, etc)

So it's up to you if you operate on msg.nodeName or msg.payload.name. But whichever you pick, stick to your choice and pay attention to precise naming and upper/lower case.

Personally I'd stick with msg.payload.name but no very good reason for that choice.

Looked at the debug info again. I thought it should be msg.payload.name, but it isnt, same error. But I tried
const n = msg.nodeName;
msg.nodeName = n.slice(6);
return msg;

Thats working. Great. But dont really understand why msg.payload.name did not work.

But copying from the debug node does help me for the future, I thought I had it worked out how to get to message data. Obviously still have to learn !
Yoyr help is much appreciated !

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