Logic Machine Forum
Creating a script that use a database or array - 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: Creating a script that use a database or array (/showthread.php?tid=3668)



Creating a script that use a database or array - khalil - 08.11.2021

hello 
I want to create a script that will trigger outputs if the current date and time matches the date and time in an array, array size will be 365*7
I have six-times columns for each day *365 days
for each column of the six columns a different output will be triggered.
what is the most efficient way to do this and which kind of scripts should I use?

   


RE: Creating a script that use a database or array - admin - 08.11.2021

Why don't you use schedulers for this?


RE: Creating a script that use a database or array - khalil - 08.11.2021

(08.11.2021, 09:36)admin Wrote: Why don't you use schedulers for this?

do you think that schedule is the best for this?
I think in scripts because it's more flexible and can be edited easily.

edit: Also because I have a lot of entries I think the script will be easy, I will get the table as excel instead of entering the data manually.


RE: Creating a script that use a database or array - admin - 08.11.2021

The question is where the schedule data comes from and if it should be editable by the end user. If the schedule is fixed then you can simply create a table with date/time and use a scheduled script that runs every minute to check if date/time matches.


RE: Creating a script that use a database or array - khalil - 08.11.2021

(08.11.2021, 09:45)admin Wrote: The question is where the schedule data comes from and if it should be editable by the end user. If the schedule is fixed then you can simply create a table with date/time and use a scheduled script that runs every minute to check if date/time matches.

thanks, admin
its a fixed date/time table 

the date/time table represents the pray time for each day of the year.

I want to control the light and AC of the masjid (mosque) eg. turn on the light before azan time by x minutes if sunset.
I hope the description is clear now, what is your suggestion admin?
regards,


RE: Creating a script that use a database or array - admin - 08.11.2021

Scheduled script that runs every minute. Fill schedule table with dates and times.
Code:
schedule = {
  ['1/1'] = { '1:05', '2:10' },
  -- ...
  ['12/31'] = { '1:11', '15:13' },
}

dtbl = os.date('*t')
date = string.format('%d/%d', dtbl.month, dtbl.day)
time = string.format('%d:%02d', dtbl.hour, dtbl.min)

schedtimes = schedule[ date ] or {}
for _, schedtime in ipairs(schedtimes) do
  if schedtime == time then
    -- execute action
  end
end
I suppose there should be a formula for calculating times for each date. Might be easier to implement it instead of a fixed table.


RE: Creating a script that use a database or array - khalil - 08.11.2021

(08.11.2021, 13:19)admin Wrote: I suppose there should be a formula for calculating times for each date. Might be easier to implement it instead of a fixed table.

Thanks Admin
Yeah, there is a formula, but I didn't find the accurate one, so a fixed table will be an alternate way if I failed to find the accurate formula.

regards,


RE: Creating a script that use a database or array - khalil - 09.11.2021

(08.11.2021, 13:19)admin Wrote: Scheduled script that runs every minute. Fill schedule table with dates and times.
Code:
schedule = {
  ['1/1'] = { '1:05', '2:10' },
  -- ...
  ['12/31'] = { '1:11', '15:13' },
}

dtbl = os.date('*t')
date = string.format('%d/%d', dtbl.month, dtbl.day)
time = string.format('%d:%02d', dtbl.hour, dtbl.min)

schedtimes = schedule[ date ] or {}
for _, schedtime in ipairs(schedtimes) do
  if schedtime == time then
    -- execute action
  end
end

Hello Admin,
the Array will be defined every time the script Run? if yes how to define it once not run every cycle?
regards


RE: Creating a script that use a database or array - admin - 09.11.2021

Script runs every minute and checks if there's a matching date/time inside the schedule table. The schedule is defined once with all dates and times for the year. Put your actions after the "-- execute action" line. It will only be executed for matching date and time.


RE: Creating a script that use a database or array - khalil - 13.11.2021

Hello Admin
is there a chance that the schedule script miss the table time so the execution occurs before or after the desired time?

another question, I am trying to compare real time with the table time e.g. if nowTime (>,<,=) tableTime[Y] (+-) X then do something
Edit: This is what I did, any advice admin?
Code:
dtbl = os.date('*t')
date = string.format('%d/%d', dtbl.day, dtbl.month)
time = string.format('%d:%02d', dtbl.hour, dtbl.min)

timeNum = (dtbl.hour-y)*60 + (dtbl.min-x) -- current time in minutes

c = 0 -- to know active table time (active pray)

schedtimes = schedule[ date ] or {}
for _, schedtime in ipairs(schedtimes) do
 
  c=c+1
 
prayTimeAry = string.split(schedtime, ':')
prayNum = tonumber(prayTimeAry[1]*60) + tonumber(prayTimeAry[2])

if (prayNum - timeNum ) == 1 then ----edit
   log ("amplifier CMD")
  elseif schedtime == time then
    log ('Azan CMD')
   -- grp.write('1/0/37',true)
    elseif (timeNum - prayNum) == 6 then
    log("Turn OFF System")
  end
end



RE: Creating a script that use a database or array - admin - 15.11.2021

It's very unlikely that the scheduled script will miss its execution. It would be better to use a formula and create scheduler events for the current day one per day.
Have you seen this Python script? Should be quite easy to rewrite it into Lua:
http://praytimes.org/code/git/?a=viewblob&p=PrayTimes&h=ce20d8c41d1d1891f82c3d3ba76e1066e3f85178&hb=HEAD&f=v2/python/praytimes.py


RE: Creating a script that use a database or array - khalil - 15.11.2021

(15.11.2021, 07:57)admin Wrote: It's very unlikely that the scheduled script will miss its execution. It would be better to use a formula and create scheduler events for the current day one per day.
Have you seen this Python script? Should be quite easy to rewrite it into Lua:
http://praytimes.org/code/git/?a=viewblob&p=PrayTimes&h=ce20d8c41d1d1891f82c3d3ba76e1066e3f85178&hb=HEAD&f=v2/python/praytimes.py

thank you admin for your time,
I see such websites that used formula but in our region they used a table while there are several minutes different between the table and the formula (not in all days and not in all prays). So I will use the table because its the most reliable right now.