Change node data type issue

I am using change node to convert two string to number, but I am getting the output in the string data type only. why is it so ? How to change string payload to number data type payload ? Please help me.

Thanks in advance

Put a debug node on the node feeding the change node to show what data is coming in.
What does your flow look like?
are you sendig two different msgs to the change node and trying to change the values from the two nodes?


Hi Sir,
Actually the string is from serial input node, I want to replace that payload with a number, here by using change node, it is converting to respective number but the output is in string data type. And I am using debug node only, through that I figured out the output is in string data type

You still haven't told us how you have configured the change node...

@ukmoose can that be done in a change node?

Of course! Just waiting for @georgethomas08 to show us how they have done it...

@ukmoose @Colin,

Hi ,
Sorry for the delay reply,
Please find the screenshots below.

Well thats not what I expected to see! From your description I thought you wanted to convert "1" to 1

But your image shows what is happening.
If you have control over what is sending the messages I'd suggest fixing it there as the two messages have different problems.

Firstly I'd suggest getting in the habit of naming your debug nodes. It makes it far easier to see what is happening in the debug panel.

If you look as the LGTON example. you will see a space at the end (and the string has two characters (string[2]) so you need to include the space in your search.

In the LGTOFF example if you look at the debug there is the arrow character at the start ( a carriage return) again you need to include that in your search. But you can use the debug output to copy and then paste into the change node if you red this page in the docs.

The Search/Replace option of the Change node only operates on Strings - the end result will always be a String.

Aside from the issues that @ukmoose mentions, to convert a payload of the string "1" to the number 1 you can add another rule to Change node to set payload to the result of the Expression $number(payload) .

Dare I say it...

Thats not what i see... If I do a Search and replace and search for the full contents of msg.payload and replace it with type number and a value. The output would appear to be a number, a switch node set to is of type number switches the value as if it was a number.

[{"id":"1e9967bc.2f7178","type":"inject","z":"9143676c.6ea94","name":"On","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":89.5,"y":163,"wires":[["67d3b17f.a25ae8"]]},{"id":"67d3b17f.a25ae8","type":"change","z":"9143676c.6ea94","name":"\"LGTON_\"","rules":[{"t":"set","p":"payload","pt":"msg","to":"LGTON ","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":241.5,"y":163,"wires":[["9c30e1db.6681c","fbdc8d64.e32a18"]]},{"id":"9c30e1db.6681c","type":"change","z":"9143676c.6ea94","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"LGTON ","fromt":"str","to":"1","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":467.5,"y":163,"wires":[["38c83e17.7c0012"]]},{"id":"fbdc8d64.e32a18","type":"debug","z":"9143676c.6ea94","name":"INPUT","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":511.5,"y":272,"wires":[]},{"id":"4aa52c95.5d2dfc","type":"debug","z":"9143676c.6ea94","name":"NUMBER","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":753.5,"y":58,"wires":[]},{"id":"38c83e17.7c0012","type":"switch","z":"9143676c.6ea94","name":"","property":"payload","propertyType":"msg","rules":[{"t":"istype","v":"number","vt":"number"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":644.5,"y":164,"wires":[["4aa52c95.5d2dfc"],["41508f24.57b348"]]},{"id":"41508f24.57b348","type":"debug","z":"9143676c.6ea94","name":"NOT NUMBER","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":797,"y":217,"wires":[]}]

Ah yes, I knew we did some edge case handling but couldnt remember exactly where it applies. Back to the beach.

1 Like

@ukmoose Thank you for the guidance, Sure i will name the debug node hereafter, I am aware of the null space and next line symbol, but still i don't know how to omit that. I thought to select the exact string what we need, we can use change node. But the outcome of the change node will also have a null space in the string. As @knolleary mentioned I have tried to convert the string: "1 " to number using the $number(payload) I got this result: "Invalid JSONata expression: Unable to cast value to a number: "1\r"" .

Copy and paste exactly what you get from the debug into the search. The flow I included in my previous post shows this.
You would need to remove the space and the carriage return BEFORE @knolleary suggestion works, but actually you don’t need to do @knolleary suggestion as Node-RED automatically does it for you.

I have tried both, by putting the space my myself and copy from the debug node as it is.

@ukmoose previously in the serial input node configuration the instead of \n i used \r. So when I used \r every input will have new lime symbol in the first and null space at the last except of the first input. Now I changed to \n, so now I am getting new line symbol at the end of the every string

If I put a null space in the change node configuration it will give the output will be same as the input.

Now I would like to know how to omit the new line symbol.? please look into debug screenshot, the change node output string size is [3] even though one charcter and new line symbol visible why is it so?

Look at what you have defined as the split character how many characters is it?

You will need to strip your newline symbols out separately try this in a function node

msg.payload= msg.payload.replace(/\n/, "");
return msg;

@ukmoose Big thanks, By the way that didn't get work, but I tried like this, Its working now.

msg.payload= msg.payload.replace(/\r\n/, "");
return msg;

The change node output datatype also now in number

msg.payload= msg.payload.replace(/\r\n/, "");
return msg;