How to get global variable properties

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?

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.

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

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

1 Like

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

Thanks,
Alex

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

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)

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

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

1 Like

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

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

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

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

Sorry Colin!

Hello

Unfortunately, this code does not work.
What's wrong with me? What should I change to work?
The value of the two global variables is okay for it normally!

Hi @SwissAirplane

If you want to share code in the forum, its much easier if you copy and paste the code rather than take a screenshot.

Your syntax is not right. global.get is a function, so you should use () not []:

var temp = global.get("temptest");
var hum = global.get("humtest");

Looking at the rest of your code, you are not using those variables anywhere.

You do set msg.payload.svalue to the string "temp;hum;0" - did you mean for temp and hum in that string to be replaced by the values of those variables?

If so, you could use JavaScript string templates:

"svalue": `${temp};${hum};0`

or simple string concatenation:

"svalue": temp + ";" + hum + ";0"

Hi there,

I have a challenge that I´m working on now for days, which, for Node-Red Pros is probably the most easy thing to solve. But as I just can´t get my head around it, I just address this to the community and see if this can be easily solved :slight_smile:

Here is my setup: Beside Home Assistant I run another Smart Home System ("Homee") which is providing an API node for Node-Red. This API node is configured to store and constantly update data from all devices added to it in the global context of Node-Red (see picture attached).

Here is my challenge: The whole setup actually works quite well, as all device information gets synched to Node-Red. But now I want to use this information in I all kind of different flows but I just don´t know how to access this data. This can probably be done by any kind of Java Script magic in a function node but as I have literally zero coding skills, I´m quite limited here :frowning:

Request for help: This may sound pretty simple for you but if anyone could point me in the direction of how to i.e. access a current value from such node that has been stored in global context this would really help me a lot!

As a new user to node-red, I recommend watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

Also, for accessing parts of a message, there’s a great page in the docs that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

BX00Cy7yHi

https://nodered.org/docs/user-guide/messages

Thanks much @Steve-Mcl for your fast feedback. Maybe I didn´t make myself clear enough: In general I have no issue to access information from any kind of objects. I just struggle with the object data that are stored in the global context as shown in the picture

It's almost the same. Use the copy path button that appears on the item of interest when you hover over it.

Then you can paste that into the change node to copy the value into payload then pass that to the node you want to use.