# Compare payload value to last received payload

**URL:** https://discourse.nodered.org/t/compare-payload-value-to-last-received-payload/48351
**Category:** General
**Created:** [13 July 2021 08:59 UTC](https://discourse.nodered.org/t/compare-payload-value-to-last-received-payload/48351 "2021-07-13T08:59:53Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![JoshuaG](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@JoshuaG](https://discourse.nodered.org/u/JoshuaG)
#### Post date: [13 July 2021 08:59 UTC](https://discourse.nodered.org/t/compare-payload-value-to-last-received-payload/48351/1 "2021-07-13T08:59:53Z")

</div>

I receive a payload value that rises every second, after a while it'll stop for a moment and reset to 0 again.

What I want to do is capture that max value.

I'm struggling to hold onto the last value to compare it against the new one.

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [13 July 2021 09:19 UTC](https://discourse.nodered.org/t/compare-payload-value-to-last-received-payload/48351/2 "2021-07-13T09:19:10Z")

</div>

You will need to save the last value. You could use a `function` node to save the value as a node variable and check it each time a msg comes thru or you could use a `switch` node and a `change` node to test and save (if the new value is greater) the value as a flow variable.

If you haven't worked with context, take a read of the documentation here: [Working with context : Node-RED](https://nodered.org/docs/user-guide/context)

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [13 July 2021 09:30 UTC](https://discourse.nodered.org/t/compare-payload-value-to-last-received-payload/48351/3 "2021-07-13T09:30:55Z")

</div>

You can also look at the smooth node - as that also can return the max value seen (as well as mean, min, etc) - and can be reset by a msg.reset message.

---

<div class="post-metadata">

### Author: ![JoshuaG](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@JoshuaG](https://discourse.nodered.org/u/JoshuaG)
#### Post date: [13 July 2021 10:04 UTC](https://discourse.nodered.org/t/compare-payload-value-to-last-received-payload/48351/4 "2021-07-13T10:04:33Z")

</div>

Solution:

```auto
pl = msg.payload.CycleTimeMach;
pl2 = msg.payload.CycleTimeMan;

avg = flow.get("AvgOldMach.S8")
avg2 = flow.get("AvgOldMan.S8")

if (pl < avg) {
    flow.set("AvgNewMach", {S8: avg})
}
if (pl2 < avg2) {
    flow.set("AvgNewMan", {S8: avg})
}

flow.set("AvgOldMach", {S8: pl})
flow.set("AvgOldMan", {S8: pl2})

```

I was trying to do it with the flow context last night but my head just wouldn't compute. I guess all I really needed was a good coffee and fresh eyes.

Edit:  
Probably worth adding that I setup the context in another node using

```auto
flow.set("AvgOldMach", {S8: 0})
flow.set("AvgNewMach", {S8: 0})

flow.set("AvgOldMan", {S8: 0})
flow.set("AvgNewMan", {S8: 0})

```

from a one-hit timestamp injection that fires every time the flow starts

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [13 July 2021 10:24 UTC](https://discourse.nodered.org/t/compare-payload-value-to-last-received-payload/48351/5 "2021-07-13T10:24:56Z")

</div>

> [@JoshuaG](#):
>
> Probably worth adding that I setup the context in another node using
> 
> ```auto
> flow.set("AvgOldMach", {S8: 0})
> flow.set("AvgNewMach", {S8: 0})
> 
> flow.set("AvgOldMan", {S8: 0})
> flow.set("AvgNewMan", {S8: 0})
> 
> ```
> 
> from a one-hit timestamp injection that fires every time the flow starts

In the `function` node you could use this

```auto
avg = flow.get("AvgOldMach.S8") || 0 // get flow var, it it doesn't exist use 0
avg2 = flow.get("AvgOldMan.S8") || 0 // get flow var, it it doesn't exist use 0

```

and eliminate the need to have the other node to set up the context.

---

<div class="post-metadata">

### Author: ![JoshuaG](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@JoshuaG](https://discourse.nodered.org/u/JoshuaG)
#### Post date: [13 July 2021 10:56 UTC](https://discourse.nodered.org/t/compare-payload-value-to-last-received-payload/48351/6 "2021-07-13T10:56:40Z")

</div>

Ah brilliant, I couldn't figure out how to achieve this.

thanks

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [13 July 2021 11:00 UTC](https://discourse.nodered.org/t/compare-payload-value-to-last-received-payload/48351/7 "2021-07-13T11:00:26Z")

</div>

The users guide is very informative: [Writing Functions : Node-RED](https://nodered.org/docs/user-guide/writing-functions#storing-data)

---

<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: [27 July 2021 11:01 UTC](https://discourse.nodered.org/t/compare-payload-value-to-last-received-payload/48351/8 "2021-07-27T11:01:00Z")

</div>

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