# Global get/set variables not working

**URL:** <https://discourse.nodered.org/t/global-get-set-variables-not-working/13321>\
**Category:** General\
**Created:** [15 July 2019 21:07 UTC](https://discourse.nodered.org/t/global-get-set-variables-not-working/13321 "2019-07-15T21:07:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pautorras](https://avatars.discourse-cdn.com/v4/letter/p/c57346/32.png) [@pautorras](https://discourse.nodered.org/u/pautorras)\
**Post date:** [15 July 2019 21:07 UTC](https://discourse.nodered.org/t/global-get-set-variables-not-working/13321/1 "2019-07-15T21:07:00Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [15 July 2019 22:23 UTC](https://discourse.nodered.org/t/global-get-set-variables-not-working/13321/2 "2019-07-15T22:23:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [15 July 2019 22:32 UTC](https://discourse.nodered.org/t/global-get-set-variables-not-working/13321/3 "2019-07-15T22:32:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pautorras](https://avatars.discourse-cdn.com/v4/letter/p/c57346/32.png) [@pautorras](https://discourse.nodered.org/u/pautorras)\
**Post date:** [16 July 2019 07:27 UTC](https://discourse.nodered.org/t/global-get-set-variables-not-working/13321/4 "2019-07-16T07:27:05Z")

</div>

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).

 ![global%20dashboard](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/d/d52bb41079deb8a32f71477dbfdf43146f5b33b2.jpeg) ![global](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/1/1a9fc3e54e6b880b16903dafaa38285811963835.jpeg)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [16 July 2019 07:29 UTC](https://discourse.nodered.org/t/global-get-set-variables-not-working/13321/5 "2019-07-16T07:29:52Z")

</div>

Your return is incorrect

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

---

<div class="post-metadata">

**Author:** ![pautorras](https://avatars.discourse-cdn.com/v4/letter/p/c57346/32.png) [@pautorras](https://discourse.nodered.org/u/pautorras)\
**Post date:** [16 July 2019 13:20 UTC](https://discourse.nodered.org/t/global-get-set-variables-not-working/13321/6 "2019-07-16T13:20:21Z")

</div>

Thank you all.  
Solved.
