Store a value and compare it with previous value


My Payload is this :


Means image

Now this msg arrives at every 1 min interval. I need to check whether the value of Tag F1 or F2 has changed or not.

How to to compare previous value of F1/F2 with their current value. Means first to store the value and then to compare it with the current value.

Pls help. Thanks in advance.

You could do this in a function node using context data to store the previous value.

msg.payload.same = ( msg.payload.yourpath === context.get('hold') ) ? "same" : "not same";
context.set('hold', msg.payload.yourpath);
return msg;

msg.payload.yourpath would be the path to your value.
msg.payload.same would then tell you if the previous was the same.
edit/ added missing semi colon.

Can you please explain a bit more....I am not very much conversant with node red.....I am not getting your solution

Have a read of this section in the docs:
It will explain to you how to save data to context.
By understanding this you will be able to save data everytime it arrives to context and that way be able to compare it to the next value that arrives.
This is what @E1cid is using in the solution he showed you.


I do not know the lay out of your flow so i could not tell you where to use it, or if it would work for you.
context.set() and context.get() store and retreive data from the context store

The function node would compare the value in the payload and if same create a new element in the payload call "same" with value "same" or "not same". You can then use this value to tell if the payload has changed.

1 Like

@E1cid is using the ternary operator in the code snippet above which may also have lead to some confusion. You can read more about it here:

1 Like

I was just ging to edit my post to explain that, cheers

1 Like

thanks...let me go though these topics

thanks....let me go though these topics....

just tell me onething, i am getting msg.payload.mypath but msg.payload.same is not returning any value...

msg.payload.same = ( msg.payload[0].D[0].value === context.get('hold') ) ? "same" : "not same";
context.set('hold', msg.payload[0].D[0].value)
return msg;

place a debug after the function and show us the msg returned.

Sorry just noticed msg.payload is an array

msg.same = ( msg.payload[0].D[0].value === context.get('hold') ) ? "same" : "not same";
context.set('hold', msg.payload[0].D[0].value)
return msg;

msg.same will hold the value.

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