Update my Payload with calculated values (counting messages)

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

[{"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 :slight_smile:

dearly
Chorum

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.

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

Me too :crazy_face:

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

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

Yeap, i needed to read more about writing functions :slight_smile:

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

Thank You :slight_smile:

Chorum

1 Like

Jep ... always a good idea RTFM - aber nichts fĂĽr ungut :wink:

BTW:

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

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