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

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:


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