How to get global variable properties

#1

Hi guys, I working on my device and I have doubts how to working with context global variable object properties. You can see bellow my global context variable:

My global variable object and properties
image

I was use this function code to get object property storage on global variable, but I don´t know how to read for example, the property nwk_addr and the value storage (528b).

 var ext_addr = global.get ("00137a0000009a99");
 msg.payload = ext_addr;
 return msg;

When I using the unique id (ext_addr = 00137a0000009a99) I receiving all properties, as you can see bellow. How to I can filter only one specific property for each unique id?
image

Someone can help me, please?

0 Likes

#2

How did you set the global variable ?

global.set("device",value/object)

For your question:

msg.payload = ext_addr.payload.properties.ModelID

note that when you hover your mouse behind the properties you see 2 icons; the first is: copy path, this you can use to eliminate writing the path yourself.

0 Likes

#3

As @bakman2 suggests the debug window can help you here.
There’s a page in the docs “working with messages” that’s worth a read.

How to navigate javascript objects is very important one Node-RED so it is also worth working through one of the many tutorials you will find online

0 Likes

#4

Hi bakman2 thanks a lot. I got success after you advise me how to do it!

I set it up like this:

 global.set(unique,
 {"ext_addr":unique,
 "properties":properties}, "memoryOnly");

I used:

 var ext_addr = global.get("00137a0000009a99");
 msg.payload = ext_addr.properties.nwk_addr;
 return msg;

Thanks a lot!
Best Regards,
Alex

0 Likes

#5

Hi ukmoose,
I read this page, but I could not develop well after that.

Thanks,
Alex

0 Likes

#6

Hi Guys, I working very well using a global variable in the memory, but i found problem in those variables after rpi3 reboot, the object properties disappeared (undefined) as can see below:

image

I storage it in the memory using this code:

  var unique = String(msg.payload.name);
    var Epcount = global.get('EP_Count');
    var prop1 = global.get('nwk_addr1');
    var prop2 = global.get('nwk_addr2');
    var prop3 = global.get('ZDP_EP1');
    var prop4 = global.get('ZDP_EP2');
    var prop5 = global.get('attr.str');
    var prop6 = global.get('ext_addr1');
    var prop7 = global.get('ext_addr2');
    var prop8 = global.get('ext_addr3');
    var prop9 = global.get('ext_addr4');
    var prop10 = global.get('ext_addr5');
    var prop11 = global.get('ext_addr6');
    var prop12 = global.get('ext_addr7');
    var prop13 = global.get('ext_addr8');

if (Epcount ==1) {
    
        switch (prop5) {
    
            case 'ZBHT-2':{
                    var properties = {
                    "nwk_addr": prop1+prop2,
                    "nwk_addrb1":prop1, 
                    "nwk_addrb2":prop2, 
                    "EP1":prop3,
                    "ModelID":prop5,
                    "Ext_addrb1":prop6,
                    "Ext_addrb2":prop7,
                    "Ext_addrb3":prop8,
                    "Ext_addrb4":prop9,
                    "Ext_addrb5":prop10,
                    "Ext_addrb6":prop11,
                    "Ext_addrb7":prop12,
                    "Ext_addrb8":prop13,
                    "Type":"Netvox Thermometer"
                    };

Someone have idea how to fix it?

Best Regards,
Alex

0 Likes

#7

show us how you are setting the variables rather than getting them.

You will see that some are listed as “memory only” which is why they disappear on restart (as described in the docs)

0 Likes

#8

Hi unkmosse, thanks!
do you can share with me a doc or article to explain how to get global variable in the file, similar i did in the memory?

Best Regards,
Alex

0 Likes

#9

Maybe reading this thread will help you A guide to understanding 'Persistent Context'

1 Like

#10

HI zenofmud, thanks a lote. I already read this tutorial. But, it served for me to define how to set global variables in the memory or file.
I tried to get a global variable storage in file using:
var nwk_add = global.get('nwk_addr1',"file") but is not working.

I have success to get the object propertites just using global variable in the memory. As you can see above i have object storage in the memory and file!
Do you give me help to undertanding it well?

Best Regards,
Alex

0 Likes

#11

I don't see where in the link posted it says to use that syntax for global.get()

0 Likes

#12

What did you set in settings.js to define it would be saved in storage?

0 Likes

#13

Guys!
So sorry for my mistake. I was defined to get global variable in the file in the wrong node position. Now is working very well..

var device = global.get("00137a000000a3bc","file");

Best Regards,
Alex

0 Likes

#14

Sorry Colin!

0 Likes