Include mathematical functions

Hi, i have a special node from a project called NibePi, that reads out modbus data:


2
I want to make the difference from two values (spread) and multiply this value by a third, multiply the result by a constant and output the result in the dashboard as the thermal power (W).
but I don't know how to get the values of the 3 registers as msg.payload for a function.
Any idea ?

Greetings

See this article in the cookbook for an example of how to join messages into one object.
Then you can access them in msg.payload.x and msg.payload.y to do your math.

thank you, join works.
how should i name the formula data:

Add a debug node to the output of the join & you can copy the path to the variables.

If you dont understandm then this and this should help you access the properies

I got my values: {"40012":26.7,"40017":26,"40072":0} in debug
but how can I link them mathematically and output the result ?
("40012"-"40017") * "40072" * "69,45" = output

the general format is...

Add a function node after the join

do your math

return the msg.

Example...

var temp = (msg.payload.a - msg.payload.b) * msg.payload.c * msg.payload.d
msg.originalPayload = msg.payload; //save for later debugging / verifying results
msg.payload = temp; 
return msg; //this causes the msg to be sent out the output & on to the next node

As for what msg.payload.a and msg.payload.b should really be - use the copy path from the debug output

1 Like

I´dont work:

So it seems to work:

var temp = (msg.payload[40012] - msg.payload[40017])
msg.originalPayload = msg.payload; //save for later debugging / verifying results
msg.payload = temp;
return msg;

For accessing properties with strange variable names (like yours) you should use strings

var temp = (msg.payload["40012"] - msg.payload["40017"]);
msg.originalPayload = msg.payload; //save for later debugging / verifying results
msg.payload = temp;
return msg;
1 Like

The general syntax for accessing JavaScript object (like msg) properties is that any property can be accessed quoted in brackets with a double or singe quote (" or '), e.g.
msg.payload['123'], msg.payload['temperature'], or msg.payload["my property"].

Alternatively if the property name consists of only alphanumerical characters or underscores, it can be accessed with just a period as a separator, e.g. msg.payload.temp1 or msg.payload.my_property. The property cannot start with a number though.

1 Like

but it works fine without " " or ´´ !?

In this case yes. The property names (object keys) are always strings (not numbers) internally in JavaScript. In the case of numbers the numeric keys are translated to strings, e.g. msg.payload[123] becomes msg.payload['123'].

The same does not apply if the key is alphanumerical though. This is why I said "the general syntax". msg.payload[temperature] for example would mean that variable named temperature would contain the key. I hope this makes sense.

1 Like

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