Split up numbers

hi all , i'm looking at getting my snips AI to change my tv channels for me by using a RMpro.

I can get snips to send me the correct intent and in node red i can change that message to the parts i need IE:

msg.payload.data.entity_id = channel number
msg payload = 54 <---- thats the channel number

how do i now change that payload to a single 5 and a single 4 so i can then tell the RMPRO to push button 5 and button 4 on my remote ? im picking id also have to work out how to write a function so if number is double digits split the number up but if its a single number like 7 then add a 0 in front of it like 07


Edited : should also mention im fairly new to node red and formula's etc so pointing me in the right direction to research and learn is also appreciated. however examples are awesome :slight_smile:

You can use split with the empty string argument to split characters:

let numParts = msg.payload.split('');
if (numParts.length === 1) {

You then test the length of the array and unshift the '0' onto the array if it's only one character.

Note these numbers are still actually strings, so you may need to recast them at some point if you need them to act as numbers later in your code.

thanks for that. think i may of actually worked out another way by trial and error as well. (albeit right or wrong it seems to work)

if (msg.topic == "channel number") {
    if (msg.payload !== null) {
        var str = msg.payload;
        var num1 = str.slice(0, 1);
        var num2 = str.slice(1, 2);
        msg.num1 = num1;
        msg.num2 = num2;
        return msg;

your method looks better than mine though so ill test that out too :slight_smile:

Your solution won't zero-pad the result if you only get a single digit channel value.