Logic Machine Forum
Global variables and constants - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Global variables and constants (/showthread.php?tid=2498)



Global variables and constants - camillsg - 04.03.2020

Hi! 

I am new to Lua and Logic Machine 4 (I do not have LM5), but trying to learn. 
I have a question I hope someone can help me with. 

I want to controll heating loads in my house, by turning these on and off to reduce peak power. 
I therefore need to define constants and variables that all scripts (resident, event and in user library) can find, I belive these are called "global". 
I belive the declaration of the constants and variables can be written like this: 

------------------Constants in the system (temperatures) ---------------------

T_s={
  comfort={kitchen=22, bathroom=24, hall=20},
  save={kitchen=18, bathroom=20, hall=18
}


--------Declaration of the variables in the system -----------

loads={
  vb={k=5, room='bathroom', ad='2/0/5', B=0, P=3000},
  vk={k=4, room='bathroom', ad='11/0/0' ,B=1, P=60, T=T_test.bathroom},
  po={k=3, room='kitchen', ad='11/0/4', B=0, P=800, T=T_test.kitchen},
  vf_gang={k=2, room='hall', ad='2/0/6', B=0, P=860, T=T_test.hall},
  vf_kj={k=1, room='kitchen', ad='11/0/2', B=0, P=15, T=T_test.kitchen}
  }



I have read something about storage.set(), but is this the only way to do it? If so, in what script? And I am going to use these values a lot, will I always have to write  storage.get()?
Or can I make a library in User Library and just define all constants and variables like above, and then they are "global"? 

Thank you for all help!


RE: Global variables and constants - admin - 04.03.2020

You can add constants to common functions. Use storage for any variables that you want to read and write from multiple scripts. You can also use objects for this if you want to tune some algorithm parameters from UI.


Global variables and constants - mjaanes - 05.03.2020

Related to this: Is there any difference in performance between using objects (e.g. above the KNX 32 range) and storage.get/set?


Sent from my iPhone using Tapatalk


RE: Global variables and constants - admin - 05.03.2020

No performance difference. But you can put larger and more complex data (like Lua tables) into storage whereas you are limited to 250 bytes when using objects.


Global variables and constants - mjaanes - 09.03.2020

Thanks
:-)


Sent from my iPhone using Tapatalk