How to make a Shift Register in Node Red?

How to make a shift (left) register in node red? Does anybody have a script? I want it to be just like in PLC program.
thanks in advance

What are you trying to shift left and where is it?

1 Like

Thanks for the reply. :slight_smile:

Suppose that I have an array size of 10. As shown in the image, i have to move that "number 1" to left one by one (by triggering). The rest should be filled with zeros until "number 1" goes to left corner which is the 10th position. Capture

Shifting should be happening for each manual trigger.
Thanks again,

You didn't say where it is, is it in msg.payload for example?

1 Like

That's an array. Not a string.

I don't think string was mentioned previously.

what string? maybe shift ?

I'm just saying that output is an array.
Though a string wasn't declared, it changes how things happen.

An array is easier to shift.

Assuming it is msg.payload then something like

msg.payload = msg.payload.shift().push(0)
return msg
1 Like

Oh, and do you want new entries to be included as it is shifted?

Do you want to carry the results (wrap them from the output to the input).

Oh sorry about it. As I understand, I mean, If we create that array in function node using script.
So basically array is in function node. that's what we create. (sorry if its confusing)

Each new array can be output to debug.

What is the input?

How do you want to handle the inputs?

Is it just for indication or do you need to output the original input?

So I think I have given you a possible solution using shift and push.

1 Like

Yeah i'm trying with that. Thanks for it. I will give the feedback soon.
Thanks again

I am missing what you are wanting to do.

If you get a message.... (not declared if needed)
shift it right tem times...
send the message... (Which? sorry)
I am not understanding msg.payload.shift().push(0) return msg.

If that the original message or the one just received?

Are the messages received in the mean time needed?

@Trying_to_learn I assumed that the array to be shifted is in msg.payload, obviously if it is in a variable instead then just shift and push the variable.

@Colin, sorry.... I wasn't having a go at you. I was merely stating my ignorance at understanding the code.

Only that what was wanting to be done isn't/wasn't declared.

An option would be to receive ......

I'll post the code shortly.

Something like:

var counter = context.get("counter")  || 0;
if (counter === 0)
{
    context.set("save_me",msg.payload);
}

counter = counter + 1;
context.set("counter",counter);
if (counter < 9)
{
    return;
}

if (counter === 9)  //  zero counting
{
    msg.payload = context.get("save_me");
    context.set("counter",0);
}

return msg;

@Trying_to_learn where is the array that is being shifted?

I was just doing what was requested differently.

Wouldn't that work if msg.payload is any format?
Number, text, array.... what ever.

Yes, "Shift register" is mentioned. But if only for lack of alternatives......

This is an alternative.

The requirement is to shift the contents of an array left and add a new item at the right hand side. Your code does nothing with msg.payload other than saving and restoring it. It does not shift it.

1 Like