# Function with different values in different times

**URL:** https://discourse.nodered.org/t/function-with-different-values-in-different-times/6024
**Category:** General
**Created:** [21 December 2018 09:55 UTC](https://discourse.nodered.org/t/function-with-different-values-in-different-times/6024 "2018-12-21T09:55:59Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Lance99](https://avatars.discourse-cdn.com/v4/letter/l/6de8d8/32.png) [@Lance99](https://discourse.nodered.org/u/Lance99)
#### Post date: [21 December 2018 09:55 UTC](https://discourse.nodered.org/t/function-with-different-values-in-different-times/6024/1 "2018-12-21T09:55:59Z")

</div>

I want to calculate an equation with 3 variables. The variable are coming asynchrone so I have to stor the latest values and then calculate the equation. Then output the result. Then wait for a new next value and then calucate with the new incoming value and withthe former two values that not have been updated. And so it continious ... Any good tip how to arrange this function? I'm a rookie Node-Red guy ....

---

<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: [21 December 2018 10:29 UTC](https://discourse.nodered.org/t/function-with-different-values-in-different-times/6024/2 "2018-12-21T10:29:27Z")

</div>

sounds like you will need to use [context variables](https://nodered.org/docs/user-guide/context)

---

<div class="post-metadata">

### Author: ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)
#### Post date: [21 December 2018 10:55 UTC](https://discourse.nodered.org/t/function-with-different-values-in-different-times/6024/3 "2018-12-21T10:55:47Z")

</div>

see the example here

> [@Output two attributes in the template](https://discourse.nodered.org/t/output-two-attributes-in-the-template/5983/6):
>
> Which is why I also suggested you look at how to store data. It sounds as if you want both values to be stored. So that when new message comes in for door it picks up the latest value of help and if a new help message comes in it picks up the latest version of door. See [{"id":"bc95c600.8a7ff","type":"inject","z":"44c6969d.6be6c","name":"","topic":"","payload":"Door1","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":125.5,"y":121,"wires":[["e74f067f.68d2e8"]]},…

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [21 December 2018 11:37 UTC](https://discourse.nodered.org/t/function-with-different-values-in-different-times/6024/4 "2018-12-21T11:37:53Z")

</div>

> [@zenofmud](#):
>
> sounds like you will need to use [context variables](https://nodered.org/docs/user-guide/context)

Or alternatively use the Join node, which saves the hassle of working out when you have got all three values and so on. Set it to generate key/value pairs using topic as the key, waiting for three topic values before sending anything and then sending every time a new value is received. Then you get all three values in the payload and can do what you want with them.

---

<div class="post-metadata">

### Author: ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)
#### Post date: [21 December 2018 13:09 UTC](https://discourse.nodered.org/t/function-with-different-values-in-different-times/6024/5 "2018-12-21T13:09:03Z")

</div>

Or alternatively use the Batch node. Configure the node to group three messages, overlapping two.

This results in the node accumulating the first 3 messages and afterward sending the last two plus the new incoming payload. In other words, do the same as Colin said about the Join node. I just wanted to make the point that there are many different ways of achieving the same result. By the way, before Colin posted I was trying a much more complex solution using function node+ storage + lots of things. Only then I realized I was overthinking.

```auto
[{"id":"7f4082f3.ed5ecc","type":"batch","z":"b785aa2d.7963e8","name":"","mode":"count","count":"3","overlap":"2","interval":10,"allowEmptySequence":false,"topics":[],"x":530,"y":200,"wires":[["a15a0642.9fa4f8"]]},{"id":"309f5716.11e278","type":"inject","z":"b785aa2d.7963e8","name":"","topic":"mov","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":200,"wires":[["22f696db.2fd31a"]]},{"id":"22f696db.2fd31a","type":"change","z":"b785aa2d.7963e8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload%10","tot":"jsonata"},{"t":"set","p":"topic","pt":"msg","to":"mov","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":200,"wires":[["7f4082f3.ed5ecc"]]},{"id":"a15a0642.9fa4f8","type":"debug","z":"b785aa2d.7963e8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":710,"y":200,"wires":[]}]

```

![r-01](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/a/a01ca0cef0389a5db534ec90e5e3e538dfeaea53.png)
