Global get/set variables not working

Hi,

I'm having troubles to set and get global variables.
In the first flow some ModbusRTU variables are read (buffered), all ok. Some of those var I want to set it global to read it (get) in the second flow.

There is the code function on flow 1:
// TRACTAMENT DE VARS JS
// https://nodejs.org/api/buffer.html
// readDoubleBE - readDoubleLE
let VL1= msg.payload.buffer.readDoubleBE(0,2)
let AL1= msg.payload.buffer.readDoubleBE(2,2)
let KWL1= msg.payload.buffer.readDoubleBE(4,2)
let KVARL1= msg.payload.buffer.readDoubleBE(6,2)
let PFL1= msg.payload.buffer.readDoubleBE(8,2)

let VL2= msg.payload.buffer.readDoubleBE(10,2)
let AL2= msg.payload.buffer.readDoubleBE(12,2)
let KWL2= msg.payload.buffer.readDoubleBE(14,2)
let KVARL2= msg.payload.buffer.readDoubleBE(16,2)
let PFL2= msg.payload.buffer.readDoubleBE(18,2)

let VL3= msg.payload.buffer.readDoubleBE(20,2)
let AL3= msg.payload.buffer.readDoubleBE(22,2)
let KWL3= msg.payload.buffer.readDoubleBE(24,2)
let KVARL3= msg.payload.buffer.readDoubleBE(26,2)
let PFL3= msg.payload.buffer.readDoubleBE(28,2)

let KWIII= msg.payload.buffer.readDoubleBE(30,2)
let KVARI= msg.payload.buffer.readDoubleBE(32,2)
let KVARC= msg.payload.buffer.readDoubleBE(34,2)
let COSFI= msg.payload.buffer.readDoubleBE(36,2)

global.set("VL1",VL1);
global.set("VL2",VL2);
global.set("VL3",VL3);
global.set("AL1",AL1);
global.set("AL2",AL2);
global.set("AL3",AL3);
global.set("KWL1",KWL1);
global.set("KWL2",KWL2);
global.set("KWL3",KWL3);
global.set("KWLTRI",KWIII);

return [
{
payload: {
L1:{
VOLTATGEL1:VL1,
INTENSITATL1:AL1,
POTENCIAL1:KWL1,
POTREACL1:KVARL1,
FACTORPOTL1:PFL1
},
L2:{
VOLTATGEL2:VL2,
INTENSITATL2:AL2,
POTENCIAL2:KWL2,
POTREACL2:KVARL2,
FACTORPOTL2:PFL2
},
L3:{
VOLTATGEL3:VL3,
INTENSITATL3:AL3,
POTENCIAL3:KWL3,
POTREACL3:KVARL3,
FACTORPOTL3:PFL3
},
TRIFASIC:{
POTENCIAACTIVAIII:KWIII,
POTENCIAINDUCTIVAIII:KVARI,
POTENCIACAPACITIVAIII:KVARC,
COSPHI:COSFI
}
}
}
];

And the function on flow 2:
var XAVL1=global.get("VL1");
var XAVL2=global.get("VL2");
var XAVL3=global.get("VL3");
var XAAL1=global.get("AL1");
var XAAL2=global.get("AL2");
var XAAL3=global.get("AL3");
var XAKWL1=global.get("KWL1");
var XAKWL2=global.get("KWL2");
var XAKWL3=global.get("KWL3");
var XAKWIII=global.get("KWLTRI");

return[XAVL1,XAVL2,XAVL3,XAAL1,XAAL2,XAAL3,XAKWL1,XAKWL2,XAKWL3,XAKWIII];

The values of these variables it isn't tranferred in the GET action.

I can't find the problem.
Thanks in advance.

  1. what version of NR and node.js
  2. in your settings.js file oe do you have context storage setup?
  3. If you go to the right sidebar and select the context tab (looks like a disk drive icon) do you see your globals?
  4. are you sure the second function is running after the first?

also when inserting code use the preformated text icon (</>) before and after your code

Assuming these 2 function nodes are in the same tab, try replacing all instances of global with flow. See if that works.

I've seen one or 2 contrib node mess up global context.

Also, let us know what you see in the context tab. Be sure to click the refresh button in the context view title bar.

Hi and thanks @zenofmud

As you say, if I go to the context view in global area, I can see all global variables defined.
All variables have de 0 value right now.

Maybe the problem is the output format I use in fuction on flow 2.
In this function I've 10 outputs, that are conected to 10 gauge (in dashboard).
All gauges output 0 value (minimum configured), but 3 of this gauges, the output value is (-5) because is the minimum range configured in this 3 gauges.
The value of all global vars are 0, but in dashboard, in this 3 gauges the value showed are -5 (must show 0 value of global variable).

Your return is incorrect

You need to return objects, each with .payload set as the value.

2 Likes

Thank you all.
Solved.