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.

How to run a scheduled event at Solar Noon?
#2
revised script

Code:
--[[
Warning: database functions are subject to change with firmware updates.
]]--

-- Update the start time for any solarNoon scheduler events.

local namePrefix = 'solarNoon%'
local maxOffsetHours = 8    -- avoid 24hr boundaries
local namePattern = "^solarNoon%s*([+-])%s*(0[0-"..maxOffsetHours.."])([0-5]%d)%D*"
local typeSpecificTime = ''

for _, evt in ipairs(db:getall('SELECT * FROM scheduler_events WHERE name LIKE ?', namePrefix)) do
  if evt then
    if (type(evt.start_hour) == "number") and (evt.start_hour % 1 == 0) and (type(evt.start_min) == "number") and (evt.start_min % 1 == 0) and (type(evt.type) == 'string') then
   
      local _, _, plusMinus, offsetHour, offsetMin = string.find(evt.name, namePattern)
      if plusMinus and offsetHour and offsetMin and (evt.type == typeSpecificTime) then
        local sunrise, sunset = rscalc(latitudeMel, longitudeMel)
        local solarNoon = math.floor(((sunset + sunrise) / 2) + 0.5) -- approximation

        local offsetMinutes = (offsetHour * 60) + offsetMin
        local eventMinutes
        if plusMinus == '+' then
          eventMinutes = solarNoon + offsetMinutes
        else
          eventMinutes = solarNoon - offsetMinutes
        end

        local eventStartHour = math.floor(eventMinutes / 60)
        local eventStartMin = math.floor(eventMinutes % 60)

        if (evt.start_hour ~= eventStartHour) or (evt.start_min ~= eventStartMin) then
          db:update('scheduler_events', {
            start_hour = eventStartHour,
            start_min = eventStartMin
          }, { id = evt.id })

          io.writefile('/tmp/lm-scheduler-clear', '')
        end
      else
        log("Error. '"..evt.name.."' event name must begin with 'solarNoon[+-]HHMM' where max HH = 0"..maxOffsetHours..", and event setting 'Run at:' must be 'Specific time'")
      end
     
    else
      log("Error. Firmware change, check evt.start_hour and evt.start_min are integers, and evt.type is a string.", evt)
    end
  end
end
Reply


Messages In This Thread
RE: How to run a scheduled event at Solar Noon? - by sgraystar - 03.09.2024, 00:19

Forum Jump: