How to compare full arrays in node red

Hi, I´m new to node red and Javascript and got a problem that maybe somebody can help me to short out.

I have a flow that in a point writes three 16bit status arrays in a database. It works ok but I´m trying to filter so it only should be writing a new row when a bit change in the incoming new msg.
To do so I´ve implemented a function node with the following JS code :

var old_State1;
var old_State2;
var old_State3;

if (msg.payload.State1 != old_State1) {
    old_State1 = msg.payload.State1;
    return msg;
}
if (msg.payload.State2 != old_State1){
    old_State2 = msg.payload.State2;
    return msg; 
} 
if (msg.payload.State3 != old_State1){
    old_State3 = msg.payload.State2;
    return msg;
}

Also at "On Start" section I´ve added the following :

old_State1 = [];
old_State2 = [];
old_State3 = [];

As you can imagine this is nos working so I gess I´m doing something wrong.

My node-red version is 2.2.2 and my node.js version is 16.16.0

Any help would be so much appreciated :slight_smile:

This is the array structure just in case it helps to clarify :

Try using the Filter node .. it should filter (block) any repeated msgs and not let them pass unless there is a change. Seems to work even for deep nested objects .. so no need to check them with a Function node.

Example Flow

[{"id":"e19bedf4f11386ff","type":"rbe","z":"54efb553244c241f","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":true,"property":"payload","topi":"topic","x":390,"y":1720,"wires":[["0422d9f7a02b816e"]]},{"id":"fa3b512570800543","type":"inject","z":"54efb553244c241f","name":"000","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"status1\":{\"bits\":[{\"bit1\":0},{\"bit2\":0},{\"bit3\":0}]},\"status2\":{\"bits\":[{\"bit1\":0},{\"bit2\":0},{\"bit3\":0}]}}","payloadType":"json","x":190,"y":1680,"wires":[["e19bedf4f11386ff"]]},{"id":"402c098817a3ee9f","type":"inject","z":"54efb553244c241f","name":"111","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"status1\":{\"bits\":[{\"bit1\":1},{\"bit2\":1},{\"bit3\":1}]},\"status2\":{\"bits\":[{\"bit1\":1},{\"bit2\":1},{\"bit3\":1}]}}","payloadType":"json","x":190,"y":1780,"wires":[["e19bedf4f11386ff"]]},{"id":"0422d9f7a02b816e","type":"debug","z":"54efb553244c241f","name":"debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":540,"y":1720,"wires":[]}]
1 Like

Many thanks!!!,

It works ok with filter node, sometimes the solution is in front of yourself and don´t see it !!

Best regards :slight_smile:

2 Likes

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