# Holding Array, how to do it?

**URL:** <https://discourse.nodered.org/t/holding-array-how-to-do-it/49027>\
**Category:** General\
**Created:** [28 July 2021 14:50 UTC](https://discourse.nodered.org/t/holding-array-how-to-do-it/49027 "2021-07-28T14:50:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lunar](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lunar/32/41496_2.png) [@Lunar](https://discourse.nodered.org/u/Lunar)\
**Post date:** [28 July 2021 14:50 UTC](https://discourse.nodered.org/t/holding-array-how-to-do-it/49027/1 "2021-07-28T14:50:54Z")

</div>

Hello, I am newbie at Node Red, I create a project with modbus TCP, and I was tried to get the array with sampling poll 5 second. I would like to make a notification when any changed of value from my array. The notification only allowed when any value change from 0 to 1 or from 1 to 0. Could you help me please?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [28 July 2021 16:42 UTC](https://discourse.nodered.org/t/holding-array-how-to-do-it/49027/2 "2021-07-28T16:42:10Z")

</div>

For a dashboard notification, use the ui notification node. For a mobile alert, you could use telegram or email node etc.

For determining the change of an array element, this will get you started...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/a/8a17834cc546eb9fdad5015424f9d4031b0c2fb9.png)

```auto
[{"id":"95aab57b10544a1d","type":"function","z":"33ec0b0def2a3894","name":"watch msg.payload array for changes","func":"const currentData = msg.payload;\n\nif (currentData && Array.isArray(currentData)) {\n var previousData = context.get(\"data\") || [];\n context.set(\"data\", currentData)\n for (let index = 0; index < currentData.length; index++) {\n const curVal = currentData[index];\n const prevVal = previousData[index];\n if(curVal !== prevVal) {\n const m = {\n topic: msg.topic + \"/\" + index,\n payload: curVal,\n previous: prevVal\n }\n node.send(m);\n }\n }\n}\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2090,"y":280,"wires":[["decf6d321c682555"]]},{"id":"d3be044e58e72269","type":"inject","z":"33ec0b0def2a3894","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"ADDR1000","payload":"[0,0,0,0]","payloadType":"json","x":1790,"y":240,"wires":[["95aab57b10544a1d"]]},{"id":"2d61fb3b323afc46","type":"inject","z":"33ec0b0def2a3894","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"ADDR1000","payload":"[0,0,0,1]","payloadType":"json","x":1790,"y":280,"wires":[["95aab57b10544a1d"]]},{"id":"46bcadb99fb64e27","type":"inject","z":"33ec0b0def2a3894","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"ADDR1000","payload":"[0,1,0,1]","payloadType":"json","x":1790,"y":320,"wires":[["95aab57b10544a1d"]]},{"id":"decf6d321c682555","type":"debug","z":"33ec0b0def2a3894","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"topic","statusType":"msg","x":2170,"y":320,"wires":[]}]

```

* * *

PS, the above code is an export of the demo flow in the picture. To use it, press CTRL-I in the node-red editor to import it.

Lastly, as a newbie to node-red, I recommend watching this playlist: [Node-RED Essentials](https://www.youtube.com/playlist?list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6). The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour.

---

<div class="post-metadata">

**Author:** ![Lunar](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lunar/32/41496_2.png) [@Lunar](https://discourse.nodered.org/u/Lunar)\
**Post date:** [29 July 2021 02:37 UTC](https://discourse.nodered.org/t/holding-array-how-to-do-it/49027/3 "2021-07-29T02:37:14Z")

</div>

it work, but the output from your function is not array, how to make the output an array?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [29 July 2021 07:02 UTC](https://discourse.nodered.org/t/holding-array-how-to-do-it/49027/4 "2021-07-29T07:02:20Z")

</div>

You should really have made that clear in your post.

So the function I provided sends a message for each change. It is a simple matter of NOT sending the message but instead return the original msg which has the new values already.

![an7hBdhuSn](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/1/81bbe615d559b32236bffd0dd7d0a1ea53a2595b.gif)

new function code...

```auto
const currentData = msg.payload;
if (currentData && Array.isArray(currentData)) {
    var previousData = context.get("data") || [];
    context.set("data", currentData)
    for (let index = 0; index < currentData.length; index++) {
        const curVal = currentData[index];
        const prevVal = previousData[index];
        if(curVal !== prevVal) {
            return msg;
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![Lunar](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lunar/32/41496_2.png) [@Lunar](https://discourse.nodered.org/u/Lunar)\
**Post date:** [29 July 2021 07:26 UTC](https://discourse.nodered.org/t/holding-array-how-to-do-it/49027/5 "2021-07-29T07:26:07Z")

</div>

Thank you, it works

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [12 August 2021 07:26 UTC](https://discourse.nodered.org/t/holding-array-how-to-do-it/49027/6 "2021-08-12T07:26:49Z")

</div>

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