Logic Machine Forum
Time dependent script - 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: Time dependent script (/showthread.php?tid=139)



Time dependent script - FatMax - 18.11.2015

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?


RE: Time dependent script - admin - 19.11.2015

I guess you need something like this in your script:

Code:
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



RE: Time dependent script - FatMax - 19.11.2015

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


RE: Time dependent script - FatMax - 14.01.2016

I´m getting an error with this script:

Code:
-- 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..?


RE: Time dependent script - admin - 14.01.2016

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".


RE: Time dependent script - FatMax - 14.01.2016

Ah, ok, good to know I´m interpreting the code correctly so far Big Grin

Thank you!


RE: Time dependent script - KoBra - 12.09.2023

tried to get the above script working but it keeps jumping around on value's

Code:
-- 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



RE: Time dependent script - admin - 13.09.2023

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:
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)



RE: Time dependent script - KoBra - 13.09.2023

(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:
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?


RE: Time dependent script - admin - 13.09.2023

You can add multiple elseif statements. Just make sure that hour checks are in an ascending order.
Code:
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)



RE: Time dependent script - KoBra - 14.09.2023

(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:
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)