# How to make a Shift Register in Node Red?

**URL:** <https://discourse.nodered.org/t/how-to-make-a-shift-register-in-node-red/25353>\
**Category:** General\
**Created:** [23 April 2020 08:55 UTC](https://discourse.nodered.org/t/how-to-make-a-shift-register-in-node-red/25353 "2020-04-23T08:55:17Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jude.Robise](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jude.robise/32/13970_2.png) [@Jude.Robise](https://discourse.nodered.org/u/Jude.Robise)\
**Post date:** [23 April 2020 08:55 UTC](https://discourse.nodered.org/t/how-to-make-a-shift-register-in-node-red/25353/1 "2020-04-23T08:55:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 April 2020 09:27 UTC](https://discourse.nodered.org/t/how-to-make-a-shift-register-in-node-red/25353/2 "2020-04-23T09:27:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jude.Robise](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jude.robise/32/13970_2.png) [@Jude.Robise](https://discourse.nodered.org/u/Jude.Robise)\
**Post date:** [23 April 2020 09:36 UTC](https://discourse.nodered.org/t/how-to-make-a-shift-register-in-node-red/25353/3 "2020-04-23T09:36:43Z")

</div>

Thanks for the reply. 🙂

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/1/912d7c06ea7b61ad1a9e42ffb0d93a608566eb58.jpeg)

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 April 2020 09:41 UTC](https://discourse.nodered.org/t/how-to-make-a-shift-register-in-node-red/25353/4 "2020-04-23T09:41:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [23 April 2020 09:41 UTC](https://discourse.nodered.org/t/how-to-make-a-shift-register-in-node-red/25353/5 "2020-04-23T09:41:40Z")

</div>

That's an array. Not a string.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 April 2020 09:42 UTC](https://discourse.nodered.org/t/how-to-make-a-shift-register-in-node-red/25353/6 "2020-04-23T09:42:36Z")

</div>

I don't think string was mentioned previously.

---

<div class="post-metadata">

**Author:** ![Jude.Robise](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jude.robise/32/13970_2.png) [@Jude.Robise](https://discourse.nodered.org/u/Jude.Robise)\
**Post date:** [23 April 2020 09:43 UTC](https://discourse.nodered.org/t/how-to-make-a-shift-register-in-node-red/25353/7 "2020-04-23T09:43:32Z")

</div>

what string? maybe shift ?

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [23 April 2020 09:43 UTC](https://discourse.nodered.org/t/how-to-make-a-shift-register-in-node-red/25353/8 "2020-04-23T09:43:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 April 2020 09:45 UTC](https://discourse.nodered.org/t/how-to-make-a-shift-register-in-node-red/25353/9 "2020-04-23T09:45:53Z")

</div>

Assuming it is msg.payload then something like

```auto
msg.payload = msg.payload.shift().push(0)
return msg

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [23 April 2020 09:45 UTC](https://discourse.nodered.org/t/how-to-make-a-shift-register-in-node-red/25353/10 "2020-04-23T09:45:54Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![Jude.Robise](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jude.robise/32/13970_2.png) [@Jude.Robise](https://discourse.nodered.org/u/Jude.Robise)\
**Post date:** [23 April 2020 09:46 UTC](https://discourse.nodered.org/t/how-to-make-a-shift-register-in-node-red/25353/11 "2020-04-23T09:46:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [23 April 2020 09:48 UTC](https://discourse.nodered.org/t/how-to-make-a-shift-register-in-node-red/25353/12 "2020-04-23T09:48:05Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 April 2020 09:54 UTC](https://discourse.nodered.org/t/how-to-make-a-shift-register-in-node-red/25353/13 "2020-04-23T09:54:15Z")

</div>

> [@Jude.Robise](#):
>
> So basically array is in function node.

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

---

<div class="post-metadata">

**Author:** ![Jude.Robise](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jude.robise/32/13970_2.png) [@Jude.Robise](https://discourse.nodered.org/u/Jude.Robise)\
**Post date:** [23 April 2020 09:55 UTC](https://discourse.nodered.org/t/how-to-make-a-shift-register-in-node-red/25353/14 "2020-04-23T09:55:10Z")

</div>

> [@Colin](#):
>
> msg.payload = msg.payload.shift().push(0) return msg

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

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [23 April 2020 09:58 UTC](https://discourse.nodered.org/t/how-to-make-a-shift-register-in-node-red/25353/15 "2020-04-23T09:58:29Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 April 2020 10:00 UTC](https://discourse.nodered.org/t/how-to-make-a-shift-register-in-node-red/25353/16 "2020-04-23T10:00:29Z")

</div>

> [@Trying\_to\_learn](#):
>
> I am not understanding `msg.payload.shift().push(0) return msg`

@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.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [23 April 2020 10:03 UTC](https://discourse.nodered.org/t/how-to-make-a-shift-register-in-node-red/25353/17 "2020-04-23T10:03:59Z")

</div>

@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:

```auto
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;

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 April 2020 10:13 UTC](https://discourse.nodered.org/t/how-to-make-a-shift-register-in-node-red/25353/18 "2020-04-23T10:13:59Z")

</div>

@Trying_to_learn where is the array that is being shifted?

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [23 April 2020 10:15 UTC](https://discourse.nodered.org/t/how-to-make-a-shift-register-in-node-red/25353/19 "2020-04-23T10:15:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 April 2020 10:21 UTC](https://discourse.nodered.org/t/how-to-make-a-shift-register-in-node-red/25353/20 "2020-04-23T10:21:17Z")

</div>

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.

[Next page](https://discourse.nodered.org/t/how-to-make-a-shift-register-in-node-red/25353.md?page=2)
