Using cosine inside function block for calculation

Dear all,

I'm playing a little bit with my NodeRed application.
I'd like to integrate a current smartmeter which is attached to the ttyUSB0 of my maschine.
This works quite good and I get all desired values.

Moreover I'm interestend in the actual power consumption of each powerline L1, L2, L3.
The formula is as follow Leistung_L1 = SpannungL1 x StromL1 x CosPhi_L1

But before doing the caluclation, it's necessary to cos(CosPhi_1).
And at this point I'm struggeling, how to do it easily inside my function block.

Any recommondations?

Thanks in advance

var Zaehlerwert = { payload: msg.payload["1-0:1.8.0*255"].values[0].value };
var MomentanVerbrauch = { payload: msg.payload["1-0:16.7.0*255"].values[0].value};
var SpannungL1= { payload: msg.payload["1-0:32.7.0*255"].values[0].value };
var SpannungL2= { payload: msg.payload["1-0:52.7.0*255"].values[0].value };
var SpannungL3= { payload: msg.payload["1-0:72.7.0*255"].values[0].value };
var Netzfreq = { payload: msg.payload["1-0:14.7.0*255"].values[0].value };
var StromL1 = { payload: msg.payload["1-0:31.7.0*255"].values[0].value };
var StromL2 = { payload: msg.payload["1-0:51.7.0*255"].values[0].value };
var StromL3 = { payload: msg.payload["1-0:71.7.0*255"].values[0].value };
var CosPhi_L1 = { payload: msg.payload["1-0:81.7.4*255"].values[0].value };
var CosPhi_L2 = { payload: msg.payload["1-0:81.7.15*255"].values[0].value };
var CosPhi_L3 = { payload: msg.payload["1-0:81.7.26*255"].values[0].value };
var Leistung_L1 = { payload: SpannungL1.payload * StromL1.payload * CosPhi_L1.payload };
var Leistung_L2 = { payload: SpannungL2.payload * StromL2.payload * CosPhi_L2.payload};
var Leistung_L3 = { payload: SpannungL3.payload * StromL3.payload * CosPhi_L3.payload};


return [
    Zaehlerwert, 
    MomentanVerbrauch,
    SpannungL1,
    SpannungL2,
    SpannungL3,
    Netzfreq,
    StromL1,
    StromL2,
    StromL3,
    Leistung_L1,
    Leistung_L2,
    Leistung_L3,
    CosPhi_L1,
    CosPhi_L2,
    CosPhi_L3
    ];


Welcome to the forum @julianpe

Show us what the data going into the function looks like.

What do you want out of that function? Usually one would output a message with all the data in an object, but you are outputting to multiple outputs. Is that really what you want, and if so, why?

Hi Colin,

I put everything in a single function, to extract all necessary signals from the smartmeter function:

Do you have any recommendation to get the cos() function inside working?

You have not done as I asked:

Unless we know what the input data looks like it is impossible to know how to code the function. Add a debug node showing what is coming out of the SM1 node.

What are the green nodes that you are feeding the data into?

I'm sorry, I forgot to add:

This comes directly from SM1 module:

{"1-0:96.1.0*255":{"medium":1,"channel":0,"measurement":96,"measureType":1,"tariffRate":0,"previousMeasurement":255,"rawValue":"001LOG00YYYYYYY","values":[{"value":"001LOG00YYYYYY","unit":""}]},"1-0:1.8.0*255":{"medium":1,"channel":0,"measurement":1,"measureType":8,"tariffRate":0,"previousMeasurement":255,"rawValue":"002887.5545*kWh","values":[{"value":2887.5545,"unit":"kWh"}]},"1-0:2.8.0*255":{"medium":1,"channel":0,"measurement":2,"measureType":8,"tariffRate":0,"previousMeasurement":255,"rawValue":"000000.0000*kWh","values":[{"value":0,"unit":"kWh"}]},"1-0:16.7.0*255":{"medium":1,"channel":0,"measurement":16,"measureType":7,"tariffRate":0,"previousMeasurement":255,"rawValue":"000128*W","values":[{"value":128,"unit":"W"}]},"1-0:32.7.0*255":{"medium":1,"channel":0,"measurement":32,"measureType":7,"tariffRate":0,"previousMeasurement":255,"rawValue":"240.4*V","values":[{"value":240.4,"unit":"V"}]},"1-0:52.7.0*255":{"medium":1,"channel":0,"measurement":52,"measureType":7,"tariffRate":0,"previousMeasurement":255,"rawValue":"241.7*V","values":[{"value":241.7,"unit":"V"}]},"1-0:72.7.0*255":{"medium":1,"channel":0,"measurement":72,"measureType":7,"tariffRate":0,"previousMeasurement":255,"rawValue":"240.5*V","values":[{"value":240.5,"unit":"V"}]},"1-0:31.7.0*255":{"medium":1,"channel":0,"measurement":31,"measureType":7,"tariffRate":0,"previousMeasurement":255,"rawValue":"000.54*A","values":[{"value":0.54,"unit":"A"}]},"1-0:51.7.0*255":{"medium":1,"channel":0,"measurement":51,"measureType":7,"tariffRate":0,"previousMeasurement":255,"rawValue":"000.11*A","values":[{"value":0.11,"unit":"A"}]},"1-0:71.7.0*255":{"medium":1,"channel":0,"measurement":71,"measureType":7,"tariffRate":0,"previousMeasurement":255,"rawValue":"000.77*A","values":[{"value":0.77,"unit":"A"}]},"1-0:81.7.1*255":{"medium":1,"channel":0,"measurement":81,"measureType":7,"tariffRate":1,"previousMeasurement":255,"rawValue":"125*deg","values":[{"value":125,"unit":"deg"}]},"1-0:81.7.2*255":{"medium":1,"channel":0,"measurement":81,"measureType":7,"tariffRate":2,"previousMeasurement":255,"rawValue":"242*deg","values":[{"value":242,"unit":"deg"}]},"1-0:81.7.4*255":{"medium":1,"channel":0,"measurement":81,"measureType":7,"tariffRate":4,"previousMeasurement":255,"rawValue":"084*deg","values":[{"value":84,"unit":"deg"}]},"1-0:81.7.15*255":{"medium":1,"channel":0,"measurement":81,"measureType":7,"tariffRate":15,"previousMeasurement":255,"rawValue":"056*deg","values":[{"value":56,"unit":"deg"}]},"1-0:81.7.26*255":{"medium":1,"channel":0,"measurement":81,"measureType":7,"tariffRate":26,"previousMeasurement":255,"rawValue":"057*deg","values":[{"value":57,"unit":"deg"}]},"1-0:14.7.0*255":{"medium":1,"channel":0,"measurement":14,"measureType":7,"tariffRate":0,"previousMeasurement":255,"rawValue":"49.9*Hz","values":[{"value":49.9,"unit":"Hz"}]},"1-0:1.8.0*96":{"medium":1,"channel":0,"measurement":1,"measureType":8,"tariffRate":0,"previousMeasurement":96,"rawValue":"00006.8*kWh","values":[{"value":6.8,"unit":"kWh"}]},"1-0:1.8.0*97":{"medium":1,"channel":0,"measurement":1,"measureType":8,"tariffRate":0,"previousMeasurement":97,"rawValue":"00047.4*kWh","values":[{"value":47.4,"unit":"kWh"}]},"1-0:1.8.0*98":{"medium":1,"channel":0,"measurement":1,"measureType":8,"tariffRate":0,"previousMeasurement":98,"rawValue":"00176.7*kWh","values":[{"value":176.7,"unit":"kWh"}]},"1-0:1.8.0*99":{"medium":1,"channel":0,"measurement":1,"measureType":8,"tariffRate":0,"previousMeasurement":99,"rawValue":"02359.5*kWh","values":[{"value":2359.5,"unit":"kWh"}]},"1-0:1.8.0*100":{"medium":1,"channel":0,"measurement":1,"measureType":8,"tariffRate":0,"previousMeasurement":100,"rawValue":"02887.5*kWh","values":[{"value":2887.5,"unit":"kWh"}]},"1-0:0.2.0*255":{"medium":1,"channel":0,"measurement":0,"measureType":2,"tariffRate":0,"previousMeasurement":255,"rawValue":"ver.03,432F,20170504","values":[{"value":"ver.03,432F,20170504","unit":""}]},"1-0:96.90.2*255":{"medium":1,"channel":0,"measurement":96,"measureType":90,"tariffRate":2,"previousMeasurement":255,"rawValue":"0F66","values":[{"value":"0F66","unit":""}]},"1-0:97.97.0*255":{"medium":1,"channel":0,"measurement":97,"measureType":97,"tariffRate":0,"previousMeasurement":255,"rawValue":"00000000","values":[{"value":"00000000","unit":""}]}}

My function will parse all related OBIS codes, and transfer them into a analogue output module, which will be published in my smart home system (Openhab) to use these values inside my smart home system.

Admin Edit: added triple backticks round data so that the forum does not mess with the contents

It should be as simple as

const CosPhi_L1 = { payload: Math.cos(msg.payload["1-0:81.7.4*255"].values[0].value * Math.PI/180) }

Did something like that not work for you?

With modern javascript it is better to use const or let rather than var, though in this case it will not make much difference.

Perfect. Thats exactly what I was searching for.
My suggestion, was doing the calculation in an external calculation module.
But this was also not working.
I think it's now time to spend more time into testing and playing with NodeRed :slight_smile: