# Store a value and compare it with previous value

**URL:** https://discourse.nodered.org/t/store-a-value-and-compare-it-with-previous-value/28369
**Category:** General
**Created:** [15 June 2020 18:29 UTC](https://discourse.nodered.org/t/store-a-value-and-compare-it-with-previous-value/28369 "2020-06-15T18:29:47Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![neelanjan](https://avatars.discourse-cdn.com/v4/letter/n/7cd45c/32.png) [@neelanjan](https://discourse.nodered.org/u/neelanjan)
#### Post date: [15 June 2020 18:29 UTC](https://discourse.nodered.org/t/store-a-value-and-compare-it-with-previous-value/28369/1 "2020-06-15T18:29:47Z")

</div>

Hi,

My Payload is this :

[{"D":[{"tag":"F1","value":"STOP"},{"tag":"F2","value":"STOP"},{"tag":"F3","value":"STOP"}]},{"D":[{"tag":"S1","value":"STOP"}]}]

Means ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/f/ff362a3b3d98dbdc942bd7c57ac40f1658ceda17.png)

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.

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [15 June 2020 19:14 UTC](https://discourse.nodered.org/t/store-a-value-and-compare-it-with-previous-value/28369/2 "2020-06-15T19:14:55Z")

</div>

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

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

---

<div class="post-metadata">

### Author: ![neelanjan](https://avatars.discourse-cdn.com/v4/letter/n/7cd45c/32.png) [@neelanjan](https://discourse.nodered.org/u/neelanjan)
#### Post date: [15 June 2020 19:23 UTC](https://discourse.nodered.org/t/store-a-value-and-compare-it-with-previous-value/28369/3 "2020-06-15T19:23:49Z")

</div>

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

---

<div class="post-metadata">

### Author: ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)
#### Post date: [15 June 2020 19:30 UTC](https://discourse.nodered.org/t/store-a-value-and-compare-it-with-previous-value/28369/4 "2020-06-15T19:30:10Z")

</div>

Have a read of this section in the docs:  
[https://nodered.org/docs/user-guide/context](https://nodered.org/docs/user-guide/context)  
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.

Johannes

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [15 June 2020 19:31 UTC](https://discourse.nodered.org/t/store-a-value-and-compare-it-with-previous-value/28369/5 "2020-06-15T19:31:26Z")

</div>

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  
[https://nodered.org/docs/creating-nodes/context](https://nodered.org/docs/creating-nodes/context)  
[https://nodered.org/docs/user-guide/context](https://nodered.org/docs/user-guide/context)

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.

---

<div class="post-metadata">

### Author: ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)
#### Post date: [15 June 2020 19:35 UTC](https://discourse.nodered.org/t/store-a-value-and-compare-it-with-previous-value/28369/6 "2020-06-15T19:35:25Z")

</div>

@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:

> **[Conditional (ternary) operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator)**
>
> The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark (?), then an expression to execute if the condition is truthy followed by a colon (:), and finally the expression to...

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [15 June 2020 19:36 UTC](https://discourse.nodered.org/t/store-a-value-and-compare-it-with-previous-value/28369/7 "2020-06-15T19:36:31Z")

</div>

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

---

<div class="post-metadata">

### Author: ![neelanjan](https://avatars.discourse-cdn.com/v4/letter/n/7cd45c/32.png) [@neelanjan](https://discourse.nodered.org/u/neelanjan)
#### Post date: [15 June 2020 19:37 UTC](https://discourse.nodered.org/t/store-a-value-and-compare-it-with-previous-value/28369/8 "2020-06-15T19:37:38Z")

</div>

thanks...let me go though these topics

---

<div class="post-metadata">

### Author: ![neelanjan](https://avatars.discourse-cdn.com/v4/letter/n/7cd45c/32.png) [@neelanjan](https://discourse.nodered.org/u/neelanjan)
#### Post date: [15 June 2020 19:38 UTC](https://discourse.nodered.org/t/store-a-value-and-compare-it-with-previous-value/28369/9 "2020-06-15T19:38:03Z")

</div>

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

---

<div class="post-metadata">

### Author: ![neelanjan](https://avatars.discourse-cdn.com/v4/letter/n/7cd45c/32.png) [@neelanjan](https://discourse.nodered.org/u/neelanjan)
#### Post date: [15 June 2020 19:54 UTC](https://discourse.nodered.org/t/store-a-value-and-compare-it-with-previous-value/28369/10 "2020-06-15T19:54:56Z")

</div>

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;

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [15 June 2020 20:08 UTC](https://discourse.nodered.org/t/store-a-value-and-compare-it-with-previous-value/28369/11 "2020-06-15T20:08:45Z")

</div>

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

Sorry just noticed msg.payload is an array  
```

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

---

<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: [29 June 2020 20:08 UTC](https://discourse.nodered.org/t/store-a-value-and-compare-it-with-previous-value/28369/12 "2020-06-29T20:08:45Z")

</div>

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