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.

Time dependent script
#1
I´m not too familiar with Lua as a scripting language, and my brief investigation into the script functions leaves some questions un-answered.

I would like some PIR´s to control lights in common areas and bathrooms dependent on time of day. 

If its night/early morning, I would like one value to be sent to the dimmer, and the rest of the day a different value.

The only time functions I´ve seen so far, is timers. 

I would also like to send system time to the bus, but haven't yet found how to do that.

Any input?
Reply
#2
I guess you need something like this in your script:

Code:
12345678910
date = os.date('*t') hour = date.hour -- morning between 6:00 and 11::59 if 6 <= hour and hour <= 11 then  domorningstuff() -- other time else  dootherstuff() end
Reply
#3
Thank you! I´m reading a book on Lua right now, hopefully I´ll get to grips with this soon enough.

I´ll play with your example and see how I do Wink
Reply
#4
I´m getting an error with this script:

Code:
12345678910111213141516
-- Changes light depending on time of day date = os.date('*t') hour = date.hour --PIR addr1 = '1/3/3' --Lights val addr2 = '1/1/74' obj = grp.find(addr1) if obj and obj.data and 1 <= hour and hour <= 7 then  grp.write(addr2, 20)     else  grp.write(addr2, 80) end  if not obj.data then  grp.write(addr2, 0) end

The error reads: Attempt to index global ' grp'(a nil value)

Shouldn't the system check the group database for correct datatype and act accordingly..? Or am I misinterpreting the error..?
Reply
#5
Again, remove spaces before grp / else / if. Chrome copies non-breaking spaces which Lua inteprets as a valid variable characters, that's why you end up with " grp" instead of "grp".
Reply
#6
Ah, ok, good to know I´m interpreting the code correctly so far Big Grin

Thank you!
Reply
#7
tried to get the above script working but it keeps jumping around on value's

Code:
12345678910111213141516
-- Changes light depending on time of day date = os.date('*t') hour = date.hour --Light ON addr1 = '1/1/30' --Lights val addr2 = '1/4/30' obj = grp.find(addr1) if obj and obj.data and 19 <= hour and hour <= 7 then grp.write(addr2, 10)    else  grp.write(addr2, 70) end  if not obj.data then  grp.write(addr2, 0) end
Reply
#8
This logic part is wrong: 19 <= hour and hour <= 7
Another issue with this script is that if the input object value is false it will first write 70 then 0 to the output. This can cause the light to flicker.

Corrected version (event script, as it should be):
Code:
123456789101112131415
value = event.getvalue() if value then   hour = os.date('*t').hour   if hour <= 7 or hour >= 19 then     out = 10   else     out = 70   end else   out = 0 end grp.checkwrite('1/4/30', out)
Reply
#9
(13.09.2023, 07:44)admin Wrote: This logic part is wrong: 19 <= hour and hour <= 7
Another issue with this script is that if the input object value is false it will first write 70 then 0 to the output. This can cause the light to flicker.

Corrected version (event script, as it should be):
Code:
123456789101112131415
value = event.getvalue() if value then   hour = os.date('*t').hour   if hour <= 7 or hour >= 19 then     out = 10   else     out = 70   end else   out = 0 end grp.checkwrite('1/4/30', out)

Tnx, is it also possible to add multiple time frames by just copying them and changing the timr?
Reply
#10
You can add multiple elseif statements. Just make sure that hour checks are in an ascending order.
Code:
1234567891011121314151617181920212223
value = event.getvalue() if value then   hour = os.date('*t').hour   -- 0:00 - 7:59   if hour <= 7 then     out = 10   -- 8:00 - 16:59   elseif hour <= 16 then     out = 70   -- 17:00 - 21:59   elseif hour <= 21 then     out = 30   -- 22:00 - 23:59   else     out = 10   end else   out = 0 end grp.checkwrite('1/4/30', out)
Reply
#11
(13.09.2023, 14:16)admin Wrote: You can add multiple elseif statements. Just make sure that hour checks are in an ascending order.
Code:
1234567891011121314151617181920212223
value = event.getvalue() if value then   hour = os.date('*t').hour   -- 0:00 - 7:59   if hour <= 7 then     out = 10   -- 8:00 - 16:59   elseif hour <= 16 then     out = 70   -- 17:00 - 21:59   elseif hour <= 21 then     out = 30   -- 22:00 - 23:59   else     out = 10   end else   out = 0 end grp.checkwrite('1/4/30', out)

Thanks for the support! I only added a seperate telegram delay to it as the LED light has to start at 25% before able to dim back to 10% (KNX dimm actor was set to 25% starting value)
Reply


Forum Jump: