Holding Array, how to do it?

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?

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

[{"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. 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.

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

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.


new function code...

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;

Thank you, it works

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