Percent calculation


I would calculat the percent value of 2 msg.payload, but I get only NaN value.

Could somebody explain me, how I can calculate percent in nodeRED ?

Thank you

percent.json (485 Bytes)

You have

var income = flow.get('income');
var outgoing = flow.get('outgoing');
msg.payload = ( 100 / ('income') * ( ('outgoing')));
//(100 / income * outgoing);//or what ever kind of formula you want
return msg;

The third line should not have income and outgoing in quotes, otherwise your are trying to use the string 'income' rather than the contents of that variable. So it should be (reorganising slightly to make the calcs a bit clearer too)
msg.payload = 100 * outgoing / income

Thank you very much. But I get still NaN.

Hi @Cedric

The Function node you have shared assumes you have already put the two values into flow context before the Function is triggered.

If you are getting NaN then one or both of your values is probably not set properly.

Can you share your wider flow that shows how income and outgoing are set in context, and how you are triggering this Function node?

Now, I get the solution.

The error was that I set 2times the msg.payload.

Thank you very much.

var maxHeight = flow.get('income');
var height = msg.payload;

msg.payload = height / maxHeight * 100;

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