Mathematical equation


I am trying to calculate the vapor pressure deficit in node red. So In two change nodes I want to use the formulas below:

but I have no idea what is the correct way to put this formula in there. Of course I have tried different ways but without success. Could someone help me with the correct syntax?

Thank you

How are you getting the values needed to do this equation?

They (ideally) should come in one message.
Otherwise you are doing the maths on varying things - which isn't wrong - but you haven't explained much from the Node-Red side of things.

For the sake of simplicity and ONLY AS AN EXAMPLE, here is something on which I hope you can build.

You want to add 2 numbers. For the sake of making it algebra, they are A and B.
and their values are 3 and 4. This is set further BACK and BEFORE it gets here.

The first example is where they both arrive in the one message.
Therefore the message has two parts:

//  get the values from the message
const a = msg.a;
const b = msg.b;

//  now `a` and `b` are holding the values you want to use in the equation.

//  Given the equation is `a` + `b`

let answer = a + b;

msg.payload = answer;
return msg;

If they arrive at different times, we have a whole other scenario.
But for now it is baby steps.

Please explain how the values are given to you.

Basically Andrew is trying to say to you that a change node will not be the place for this - a function node will be.

Presumably your values will come in from different devices at different times (although maybe not)

If they do come in at different times you can use the join node to put them into a single message - or dependant on frequency and rate of change you could store them as context variables

Also depending on how accurate your sensors are you may wish to feed them through the smooth node prior to doing anything to enable rogue values to be isolated.


1 Like

Show us the message coming in with the values in it, and your best guess at the function to do it.

Thank you for the feedback. This is the flow. I recieve the temperature via mqtt and parse it as payload to the change node.

In the change node I try to set the payload to the value of the equation with the temperature variable replaced with payload.

The problem is that I dont know the correct syntax to use for the equation.

I'm getting the value for the temperature via mqtt. All other parts of the equation are constants.

So only one message required. The example is already very helpful, I assume its meant to be put into a function node? Your example would be easy enough to modify for me, the only problem I still have is the correct syntax for this complex formula expression. I dont know how to properly write it in java and I cant find a good explanation online. Most examples I could find only cover the basics like "+" "-" "*" "/".

First off it is JavaScript not Java. Second use parentheses and remember the order of operations DT3 could be expressed as (D * (T * T * T)) or you could use the JavaScript method MATH and code it as (D*MATH.pow(T,3)).

If you do a google search ‘JavaScript power of’ you will find many explanations (I find I like the w3school ones best)

Here is a function which (I think) does the vpsat calculation.
It's broken down into individual terms to avoid a single intimidating calculation.
I think I can gauarantee at least one typo or mistake in converting to 0.000000... etc

const tCelsius = msg.payload;
const tRankin = (tCelsius * 1.8) + 491.67;

const first = -10440.397/tRankin;
const second = -11.29465; 
const third = tRankin * -0.027022355;
const fourth = Math.pow(tRankin,2) * 0.00001289036;
const fifth = Math.pow(tRankin,3) * -0.0000000024780681;
const sixth = 6.5459673 * Math.log(tRankin);

const exponent = first + second + third + fourth + fifth + sixth;

const vpsat = Math.pow(Math.E, exponent);

msg.payload = vpsat;
return msg;
1 Like

Looks good to me...




(ref: online calculator)

Demo Flow...

[{"id":"80ae5aced8630589","type":"function","z":"c1ebfd3083f3904f","name":"Calculate vpsat (PSI) from Celcius","func":"const tCelsius = msg.payload;\nmsg.temperature = tCelsius;\nconst tRankin = (tCelsius * 1.8) + 491.67;\n\nconst first = -10440.397/tRankin;\nconst second = -11.29465; \nconst third = tRankin * -0.027022355;\nconst fourth = Math.pow(tRankin,2) * 0.00001289036;\nconst fifth = Math.pow(tRankin,3) * -0.0000000024780681;\nconst sixth = 6.5459673 * Math.log(tRankin);\n\nconst exponent = first + second + third + fourth + fifth + sixth;\n\nconst vpsat = Math.pow(Math.E, exponent);\n\nmsg.payload = vpsat;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1430,"y":800,"wires":[["d594ae3a26e1d19a","2d899760b8ab37bd"]]},{"id":"dc8bc2128cb67153","type":"inject","z":"c1ebfd3083f3904f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"25","payloadType":"num","x":1170,"y":800,"wires":[["80ae5aced8630589"]]},{"id":"d594ae3a26e1d19a","type":"debug","z":"c1ebfd3083f3904f","name":"PSI","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1650,"y":825,"wires":[]},{"id":"96d334577c3cfc48","type":"inject","z":"c1ebfd3083f3904f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"100","payloadType":"num","x":1170,"y":760,"wires":[["80ae5aced8630589"]]},{"id":"9c9f676f21cb68dd","type":"inject","z":"c1ebfd3083f3904f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":1170,"y":840,"wires":[["80ae5aced8630589"]]},{"id":"2d899760b8ab37bd","type":"debug","z":"c1ebfd3083f3904f","name":"Celcius","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"temperature","targetType":"msg","statusVal":"payload","statusType":"auto","x":1660,"y":777,"wires":[]}]

Thanks for fixing my missing bracket Steve.

There's a significant difference between the answers at 100 C.
Not sure if that's a sign of another error in my code or a natural consequence of trying to condense water onto living plants at 100 C

No. I think it's down to there being quite a few formulas for this. That site may not be using the same formula or constants e.g humidity or leaf temperature. I think I also read it is an approximation anyway so the difference we see may be inconsequential. But then again, I'm not a biologist, chemist, physicist or a mathematician :slight_smile:

1 Like

Thanks you and everyone else, vpsat calcuation works.
I now tried to modify it further to calculate VPD.


... It doesn't work though. I'm not sure why. Debug only shows "NaN" as a result.

any ideas?

That's not how you get data from global context inside a function... see Writing Functions : Node-RED

thanks for the link...
changed it to "let relhum = global.get("humidity");

I'm getting the correct output now.
Thanks everyone <3

1 Like

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