This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Logic script
#1
Hello,
I need group address which will show if any of windows is opened. So, I made event-based script, but problem is that only one event (window) affect result. How can I make script that will send result if any of windows open?

Thank you
Reply
#2
post code, it will help us to understand the problem
Reply
#3
There is no special code... For example, here is logic "a = b and c and d". If I use event based scripting, only change of one group address (for example "b") will change "a", but I need script where change of "b","c" or "d" will change "a". This is because I can choose only one "group address/tag" on which the script will be triggered.

In concrete situation, lets say there are 5 windows, and I want to have object which will show "ON" if any of window is opened, and "OFF" if all of them are "OFF". So this object will change it state if any of this window. It is the most basic logic.
Reply
#4
You can tag objects b,c,d and make a script that runs on specific tag.


your script should look like:


Code:
b = grp.getvalue('5/1/2')
c = grp.getvalue('5/1/3')
d = grp.getvalue('5/1/4')

a=  b or c or d

grp.write('5/1/1', a)
Reply
#5
This is a perfect use case for TAGS in LogicMachine
For every window you have assign a tag to it, then set a resident script that checks the tags and performs some logic on it. 

example: 

Code:
myobjects = grp.tag('ABC')
myobject = '1/1/1'
a = 0

for index, value in ipairs(myobjects) do
 if (value.data) then
   a = a + 1
 end
end

if (a > 0) then
 grp.write(myobject, true, dt.bool)
else
 grp.write(myobject, false, dt.bool)
end

The above scripts checks every n seconds boolean group adresses with the tag ABC, then sets 1/1/1 to true if one of the tags are true.
No need to create a huge script with declarations for each and every group address, just tag the object and this will fix it.

There are most likely other ways to do this, but I'm still learning LUA and would love to see other implementations Smile

Added bonus here is that you also get a count of how many windows that are open.
Reply
#6
You can make it a little bit smaller by replacing this:
Code:
if (a > 0) then
 grp.write(myobject, true, dt.bool)
else
 grp.write(myobject, false, dt.bool)
end

With this:
Code:
grp.write(myobject, a > 0, dt.bool)

If you need AND instead of OR you can do this:
Code:
grp.write(myobject, a == #myobjects, dt.bool)
Reply
#7
Thank you for that elegant (a > 0). Also didnt think of the # operator, was abit sceptical to use it with the whole true / false / nil statement. 
Will a false also show as a nil when it comes to the #?  Would it be better to replace it with table.getn(myobjects) since that one cant suddenly stop on a nil value and end the statement like #  (according to some other forums).
Reply
#8
objects table is guaranteed a be normal array without any holes (nil elements), so # operator does the same as table.getn

From Lua manual: Both nil and false make a condition false; any other value makes it true.
Reply


Forum Jump: