How to use global variables ??

Moderator: leecollings

Post Reply
PimJanse
Posts: 6
Joined: Saturday 03 June 2017 7:55
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

How to use global variables ??

Post by PimJanse »

Hello, I'm developing a quite complex script with several global variables that I need to use every time when the event gets triggered. It's kind of reference counter that I want to use to make certain decisions in the script. For that reason I have instantiated and initiated the GLOBAL variables at the top of the script (TestVar = 0) and increase in the script. See example below:

TestVar = 0

commandArray = { }

If (TestVar == 0) then
[Do action 1...]
TestVar = TestVar + 1
else
DoFunction(TestVar)
end

return

This should work in C-code, HOWEVER I have the problem that TestVar is always initialized to 0 in every event trigger. And as a result, the else-statement with DoFunction(TestVar) is never reached. I found a workaround by using a User Variable, but this is not a very elegant solution as I have many of these variables, including some array's.

So, does anybody know how the value of global variables can be initialized once and then retained in each event trigger?
Many thanks!
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: How to use global variables ??

Post by emme »

why don't use UserVariables instead ? ;)
The most dangerous phrase in any language is:
"We always done this way"
PimJanse
Posts: 6
Joined: Saturday 03 June 2017 7:55
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: How to use global variables ??

Post by PimJanse »

Because I see UserVariables as variables that provide "interaction" with the user. This is great, because it gives me the ability to set parameters for my script, or read actual values.

However, in my case I need persistent variables to let my script work - without any need for user interaction. I agree that using UserVariables is an option, but not ideal because it will make the real UserVariables (with interaction) kind of unfindable because there are so many.

So, is there a possibility anyway to initiate variables (local or global) once and let them keep their value each time the event script is triggered?
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest