Hi there,
i like to use a function node to bring incomming messages to a global value.
My recent code:
The "name" in the global.set context should be the var from above. And I have no idea how the right code shoud look like.
Any hints?
Best wishes
             
            
              
            
           
          
            
            
              I don't see any problems here. If it stays that simple a normal "change" node would do the job too.
             
            
              
            
           
          
            
            
              
 bibi-b:
 
My recent code:
The "name" in the global.set context should be the var from above. And I have no idea how the right code shoud look like.
 
 
Remove the quotes...
var name = msg.channelName;
var status = msg.payload;
//global.set('name', status); // << wrong
global.set(name, status); // << correct
return msg;
 
            
              
            
           
          
            
            
              Changed to this:
var name = msg.channelName;
and getting this error:
             
            
              
            
           
          
            
            
              Change code to this...
var name = msg.channelName;
var status = msg.payload;
node.warn("name = " + name );
node.warn("status = " + status );
global.set(name, status);
return msg;
what do you see in debug side bar?
             
            
              
            
           
          
            
            
              oh stop. the "status" is sometimes boolean, sometimes a string (0 or 1). Maybe that ist why?
             
            
              
            
           
          
            
            
              
 bibi-b:
 
Maybe that ist why?
 
 
No, you can store bool/string/object/whatever in the global context.
It is the name that is important - it must be a non zero length string
             
            
              
            
           
          
            
            
              
Maybe because of the spaces in name?
             
            
              
            
           
          
            
            
              
no, spaces are valid in the name.
Are you still getting an error? Have you checked the context data side bar panel? Have you presed the refresh button?
             
            
              
            
           
          
            
            
              
 Steve-Mcl:
 
non zero length string
 
 
yes, i did. Can you see the screenshot i uploaded from the sidebar? if not:
name = Schlafzimmer Fenster
name = Wohnzimmer links Tür
... and so on. Seems to be correct.
             
            
              
            
           
          
            
            
              
 bibi-b:
 
Seems to be correct
 
 
So it is working now?
Is there anything in the context data  view like I showed
 Steve-Mcl:
 
 
 
             
            
              
            
           
          
            
            
              No 
Here is the second screen with the errors:
             
            
              
            
           
          
            
              
                Colin  
              
                  
                    25 June 2021 08:19
                   
                  13 
               
             
            
              
I thought that too, but testing, even with a fixed string, does give an errorglobal.set("some string", "test")@bibi-b  is seeing (but at position 4)
             
            
              
            
           
          
            
            
              Wow, could have sworn I tested that - you are right colin lol
@bibi-b  you will have to remove spaces from the names OR use an object to store the values in then store the object.
             
            
              
            
           
          
            
            
              Thank you both!
Do you have a quick code i can insert in the function node to change spaces to an underscore _ ?
             
            
              
            
           
          
            
            
              Try this instead first - you might find it preferable to having hundreds of top level global variables - especially if you want to deleted them all in one go...
const channelData = global.get("channelData") || {};
const name = msg.channelName;
const status = msg.payload;
channelData[name] = status;
global.set("channelData", channelData);
return msg;
 
            
              
            
           
          
            
            
              
Working perfectly. Thanks a lot for your time and help!
             
            
              
            
           
          
            
              
                system  
              
                  
                    24 August 2021 08:30
                   
                  18 
               
             
            
              This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.