# Update my Payload with calculated values (counting messages)

**URL:** https://discourse.nodered.org/t/update-my-payload-with-calculated-values-counting-messages/22706
**Category:** General
**Created:** [6 March 2020 12:37 UTC](https://discourse.nodered.org/t/update-my-payload-with-calculated-values-counting-messages/22706 "2020-03-06T12:37:22Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Chorum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chorum/32/47644_2.png) [@Chorum](https://discourse.nodered.org/u/Chorum)
#### Post date: [6 March 2020 12:37 UTC](https://discourse.nodered.org/t/update-my-payload-with-calculated-values-counting-messages/22706/1 "2020-03-06T12:37:22Z")

</div>

Hello there,  
it could be a stupid Question but a cant see the solution atm.

for my Question i use a simplified flow.

MQTTServer Node -\> Function Node -\> Debug Node  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/7/970ddbc48532c0bff279350002e41f5ed9f02aed.png)

```auto
[{"id":"8aad5e6d.f167b","type":"mqtt in","z":"af61c022.bc96e","name":"","topic":"my theme(nothing in here)","qos":"2","datatype":"auto","broker":"3ad0d234.33205e","x":1190,"y":1410,"wires":[["80d05e31.509bb"]]},{"id":"80d05e31.509bb","type":"function","z":"af61c022.bc96e","name":"","func":"var x = 0;\nx = x +1;\nnode.log(\"Runthroughs:\" + x);\nreturn msg;","outputs":1,"noerr":0,"x":1400,"y":1410,"wires":[["1de5d02d.0bd1f"]]},{"id":"1de5d02d.0bd1f","type":"debug","z":"af61c022.bc96e","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1560,"y":1410,"wires":[]},{"id":"3ad0d234.33205e","type":"mqtt-broker","z":"","name":"Mosquito","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

```

I would like to get the results from the Funktion node in the same rate as from the MQTT Server. (1200 MS)

It seems like my Funktion node runs only once but passes through the payload from the MQTT -Server in the wanted rate.

I hope the Question is understandable 🙂

dearly  
Chorum

---

<div class="post-metadata">

### Author: ![Christian-Me](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/christian-me/32/10774_2.png) [@Christian-Me](https://discourse.nodered.org/u/Christian-Me)
#### Post date: [6 March 2020 12:49 UTC](https://discourse.nodered.org/t/update-my-payload-with-calculated-values-counting-messages/22706/2 "2020-03-06T12:49:08Z")

</div>

Can you please use the code \</\> button and paste your flow in compact form there.  
And it is always good to paste a screenshot of your flow too. Many problems can be solved without importing your flow. Or can be answered on mobile devices.  
And perhaps place debug nodes at relevant places with “hole message object” not only payload  
And the source of your function node too.  
You can edit your original post.

---

<div class="post-metadata">

### Author: ![Chorum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chorum/32/47644_2.png) [@Chorum](https://discourse.nodered.org/u/Chorum)
#### Post date: [6 March 2020 12:51 UTC](https://discourse.nodered.org/t/update-my-payload-with-calculated-values-counting-messages/22706/3 "2020-03-06T12:51:01Z")

</div>

I am Sorry i clicked "post" to early...

The Flow is only a principle, the whole flow would be to much.  
For the Debug Node : No Errors , Nothing. The Function node passes everything through to the Dashboard. So Long everything is fine.

Chorum

---

<div class="post-metadata">

### Author: ![Christian-Me](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/christian-me/32/10774_2.png) [@Christian-Me](https://discourse.nodered.org/u/Christian-Me)
#### Post date: [6 March 2020 12:52 UTC](https://discourse.nodered.org/t/update-my-payload-with-calculated-values-counting-messages/22706/4 "2020-03-06T12:52:55Z")

</div>

Me too 🤪

There is a nice preview (close the blue half of the message window on the right.

---

<div class="post-metadata">

### Author: ![Christian-Me](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/christian-me/32/10774_2.png) [@Christian-Me](https://discourse.nodered.org/u/Christian-Me)
#### Post date: [6 March 2020 12:59 UTC](https://discourse.nodered.org/t/update-my-payload-with-calculated-values-counting-messages/22706/5 "2020-03-06T12:59:49Z")

</div>

> [@Chorum](#):
>
> ```auto
> var x = 0;
> x = x +1;
> node.log("Runthroughs:" + x);
> return msg;
> 
> ```

So every time the message arrives x=0+1 = 1

Perhaps take a look here:

[https://nodered.org/docs/user-guide/writing-functions#storing-data](https://nodered.org/docs/user-guide/writing-functions#storing-data)

---

<div class="post-metadata">

### Author: ![Chorum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chorum/32/47644_2.png) [@Chorum](https://discourse.nodered.org/u/Chorum)
#### Post date: [6 March 2020 13:06 UTC](https://discourse.nodered.org/t/update-my-payload-with-calculated-values-counting-messages/22706/6 "2020-03-06T13:06:10Z")

</div>

Yeap, i needed to read more about writing functions 🙂

```auto
var x = context.get('x')||0;
x += 1;
context.set('x',x);
node.log("Runthroughs:" + x);
msg.payload.x = x;
return msg;

```

Thank You 🙂

Chorum

---

<div class="post-metadata">

### Author: ![Christian-Me](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/christian-me/32/10774_2.png) [@Christian-Me](https://discourse.nodered.org/u/Christian-Me)
#### Post date: [6 March 2020 13:34 UTC](https://discourse.nodered.org/t/update-my-payload-with-calculated-values-counting-messages/22706/7 "2020-03-06T13:34:55Z")

</div>

Jep ... always a good idea RTFM - aber nichts für ungut 😉

BTW:

```auto
msg.payload.x = x; // gives you msg.payload = {"x":10} and deletes what was in payload before
msg.payload=x; // gives you msg.payload=x and deletes what was in payload before
msg.x=x: // gives you msg.x=x but you have to switch the debug node to "hole msg object" to see it. and msg.payload is still what it was when it came form the broker

```

---

<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: [20 March 2020 13:34 UTC](https://discourse.nodered.org/t/update-my-payload-with-calculated-values-counting-messages/22706/8 "2020-03-20T13:34:56Z")

</div>

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