question

victronvalhowell6283 avatar image
victronvalhowell6283 asked

trying to do some math in Node Red

I am trying to compare my DC voltage at two different locations on the boat. Is there a way I can use Node Red to take the two reading every few seconds and give me a difference between them? What JavaScript would I need to run in the function block? Or is there an easier way?

Node-RED
2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

2 Answers
johnparvic avatar image
johnparvic answered ·

Hi

This is kind of tricky in Node Red because it is event based and nodes don't know what events (inputs) are going to arrive in which order.

So, the way I've usually done this kind of thing is to store one of the numbers in a context (flow) variable using the Change node, and then in the function node, each time the second input arrives, read the context variable, do the math and generate the output.

I'm probably going to pull max and min cell voltages from the MQTT topics and calculate the difference to that I can generate alerts and graphs so quite similar - I can share the flow with you if it helps.

There are also math nodes that may do what you need - take a look at "Manage Palette".

Kind regards

John

1 comment
2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

victronvalhowell6283 avatar image victronvalhowell6283 commented ·
Yes. Please share


0 Likes 0 ·
hominidae avatar image
hominidae answered ·

look at https://flows.nodered.org/node/node-red-contrib-boolean-logic-ultimate ...there is a "math" node that can do what you want...just make sure that every input message from each "stream"/Event-Source" carries a different msg.topic.

2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.