Making percentage calculations with two different msg.payload

#1

Hi

I have problem with my function block. First MQTT is sending me incoming water volume second MQTT is sending outgoing water volume and i wanna calculate whats the difference. My problem is that function block gives me two outputs one is correct and other i don’t even know where it comes.

Can someone please explain me or guide me how to make this thing to work. If i haven’t been understandable, let me know, i’ll try to explain better.

Best regards
Tamar

0 Likes

#2

Can you post the code you are using in your function block

0 Likes

#3

Node-RED doesn’t have a memory. So when the second message comes in, it has no memory of the previous message which I am guessing where the second message comes from.

There are different techniques to overcome this, either global variables or using the join node. But as already has been said without seeing your function it is hard to say

0 Likes

#4
if (msg.topic == 'sensor/ylekastmine/2') {
    context.ylekastmine = msg.payload;
} else if (msg.topic == 'sensor/spoon/2') {
    context.spoon = msg.payload;
}
var test = {payload:0};
test.payload = Math.round(100/Number(context.ylekastmine)*(Number(context.spoon)));
return test;
0 Likes

#5

check the syntax for setting and getting context as per the docs https://nodered.org/docs/writing-functions#storing-data

0 Likes

#6

You colud use the join node, in manual mode, then set it to combine every payload to create a key/value object using the topic as key and wait to get the msg.complete (that you’ll send when you send the second message)

0 Likes

#7

i made a quick flow for you with a formula.
just change the timestamp with th mqtt input.

[{“id”:“27ee89f8.d3a6c6”,“type”:“inject”,“z”:“77705272.2fb44c”,“name”:"",“topic”:"",“payload”:"",“payloadType”:“date”,“repeat”:"",“crontab”:"",“once”:false,“onceDelay”:0.1,“x”:240,“y”:180,“wires”:[[“3e97606.cf3b1a”]]},{“id”:“250c6f55.d881”,“type”:“inject”,“z”:“77705272.2fb44c”,“name”:"",“topic”:"",“payload”:"",“payloadType”:“date”,“repeat”:"",“crontab”:"",“once”:false,“onceDelay”:0.1,“x”:240,“y”:320,“wires”:[[“bb39ee40.1897d”]]},{“id”:“3e97606.cf3b1a”,“type”:“change”,“z”:“77705272.2fb44c”,“name”:"",“rules”:[{“t”:“set”,“p”:“income”,“pt”:“flow”,“to”:“payload”,“tot”:“msg”}],“action”:"",“property”:"",“from”:"",“to”:"",“reg”:false,“x”:620,“y”:180,“wires”:[[“714e8906.920db8”,“1cf69bf8.a51954”]]},{“id”:“bb39ee40.1897d”,“type”:“change”,“z”:“77705272.2fb44c”,“name”:"",“rules”:[{“t”:“set”,“p”:“outgoing”,“pt”:“flow”,“to”:".payload",“tot”:“msg”}],“action”:"",“property”:"",“from”:"",“to”:"",“reg”:false,“x”:610,“y”:320,“wires”:[[“7970dd98.a965f4”,“fa8138e8.1da4f8”]]},{“id”:“2d60b2fd.5aec4e”,“type”:“function”,“z”:“77705272.2fb44c”,“name”:“function”,“func”:“var income = flow.get(‘income’);\nvar outgoing = flow.get(‘outgoing’);\nmsg.payload = income - outgoing;//or what ever kind of formula you want\n\nreturn msg;”,“outputs”:1,“noerr”:0,“x”:680,“y”:600,“wires”:[[“c5ec5317.afe59”]]},{“id”:“7970dd98.a965f4”,“type”:“debug”,“z”:“77705272.2fb44c”,“name”:"",“active”:true,“tosidebar”:true,“console”:false,“tostatus”:false,“complete”:“false”,“x”:930,“y”:320,“wires”:[]},{“id”:“714e8906.920db8”,“type”:“debug”,“z”:“77705272.2fb44c”,“name”:"",“active”:true,“tosidebar”:true,“console”:false,“tostatus”:false,“complete”:“false”,“x”:930,“y”:180,“wires”:[]},{“id”:“1cf69bf8.a51954”,“type”:“link out”,“z”:“77705272.2fb44c”,“name”:“in”,“links”:[“6cf07c18.be9104”],“x”:975,“y”:120,“wires”:[]},{“id”:“fa8138e8.1da4f8”,“type”:“link out”,“z”:“77705272.2fb44c”,“name”:“out”,“links”:[“274642cd.16094e”],“x”:975,“y”:380,“wires”:[]},{“id”:“6cf07c18.be9104”,“type”:“link in”,“z”:“77705272.2fb44c”,“name”:"",“links”:[“1cf69bf8.a51954”],“x”:315,“y”:540,“wires”:[[“2d60b2fd.5aec4e”]]},{“id”:“274642cd.16094e”,“type”:“link in”,“z”:“77705272.2fb44c”,“name”:"",“links”:[“fa8138e8.1da4f8”],“x”:315,“y”:660,“wires”:[[“2d60b2fd.5aec4e”]]},{“id”:“c5ec5317.afe59”,“type”:“debug”,“z”:“77705272.2fb44c”,“name”:"",“active”:true,“tosidebar”:true,“console”:false,“tostatus”:false,“complete”:“false”,“x”:1210,“y”:600,“wires”:[]}]

0 Likes

#8

I dont know why, but i cant import this nodes. Is there any other option?

0 Likes