Substract values from msg and using global variable

#1

Hi Node community!
I need some help in trying to do a subtraction operation between two messages that are msg.payload: Object like: {“contA”:20,“contB”:57}
In one Flow, let’s call it Flow1 I receive one message like previous one {“contA”:20,“contB”:57} and I need to store values for comparing when next message is received. One example:

  • Program start I receive {“contA”:20,“contB”:57}, one hour later I receive {“contA”:30,“contB”:77} so then I need to have first values stored for being able to substract [30-20 (contA1hour=10)]and [77-57 contB1hour=10] .
    I am trying to do the storing with global.set and global.get and the math part but I do not get it.
    The end of this operation is showing instant values in one grafana graph and total values in other, this part is already done using influxdb.
    Really thanks for your time, best regards!
    My function code:
    var ret = {};
    var old = {};
    ret.contadorA = msg.payload[2]+msg.payload[3]+msg.payload[4]+msg.payload[5]
    ret.contadorB = msg.payload[6]+msg.payload[7]+msg.payload[8]+msg.payload[9]
    msg.payload = ret;
    global.set(“old”, “msg.payload”);
    return msg;
    Sub:
    var myold = global.get(“old”) || 0;
    msg0 = myold.payload;
    final.A = msg0[0]-msg.payload[0]
    final.B = msg0[1]-msg.payload[1]
    msg.payload = final;
    return msg;

My code is returning always:
{“A”:null,“B”:null} it is not doing substraction.

0 Likes

#2

I can’t follow your function code

If your msg.payload is {“contA”:20,“contB”:57} The data is a javascript object not an array

what are you doing with these lines?
ret.contadorA = msg.payload[2]+msg.payload[3]+msg.payload[4]+msg.payload[5]
ret.contadorB = msg.payload[6]+msg.payload[7]+msg.payload[8]+msg.payload[9]

You should be able to access contA as msg.payload.contA and contB as msg.payload.contB
See https://nodered.org/docs/user-guide/messages

0 Likes

#3

Hi!
Sorry I receive a big message, I extract msg.payload (9bytes) and from it I only get the part I need, that is ret.contadorA(2-5) and ret.contadorB (6-9) from msg.payload. Copied from mesage:image
Then with message extracted and presented as {“contA”:20,“contB”:57} I need to store in a global variable for using in other flow and making the substraction operation as explained before.
Thanks!

0 Likes

#4

Hello Tzeeth

As ukmoose told I think you should do with the change node:

image

or with a function node:

global.set('contA ',msg.payload.contA ); 
global.set('contB ',msg.payload.contB );

return.msg  ```



Then on a functioin node do something similar to this like :

`

var contA= global.get("contA;");
var contB= global.get("contB;");

msg.payload = (contB - contA);

return msg ```

Note: if you wanted to get the out message as number instead of string with one decimal (as example)you will have to add a third variable, in this case I call it temp and do the conversion before return message as:

temp = (contB - contA);
msg.payload = parseFloat(temp).toFixed(1);

Add on the begining the variable var temp ;

Hope that helps u

Regards

0 Likes

#5

Hi! Really thanks both for your suggestions and info, I am out of office now but tomorrow will test and update.
Regards!

0 Likes