Logic Machine Forum
Search schedulers - 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: Search schedulers (/showthread.php?tid=4563)



Search schedulers - CarlS - 07.02.2023

Hi,

Received this function some years ago from support. 
It should find  on and off time for a specific scheduled GA
I get this error when I try to run it.

User script:21: bad argument #1 to 'ipairs' (table expected, got string)
stack traceback:
[C]: in function 'ipairs'
User script:21: in function 'findtime'
User script:33: in main chunk



Code:
function findtime(addr, value)
  local obj, file, data

  -- read data from cache file
  file = '/tmp/lm-scheduler-' .. os.date('%Y%m%d') .. '.lua'
  data = io.readfile(file)

  -- cache file missing, stop
  if not data then
    return
  end

  -- try decoding cache
  data = serialize.decode(data)
  if type(data) ~= 'table' then
    return
  end

  obj = knxlib.encodega(addr)
  for time, events in pairs(data) do
    for _, event in ipairs(events) do
      if event.object == obj and value == toboolean(event.value) then
        return time
      end
    end
  end
end


------------------------------------------


t1 = findtime('1/0/1', true)
t2 = findtime('1/0/1', false)



RE: Search schedulers - AEK - 07.02.2023

(07.02.2023, 08:28)CarlS Wrote: Hi,

Received this function some years ago from support. 
It should find  on and off time for a specific scheduled GA
I get this error when I try to run it.

User script:21: bad argument #1 to 'ipairs' (table expected, got string)
stack traceback:
[C]: in function 'ipairs'
User script:21: in function 'findtime'
User script:33: in main chunk



Code:
function findtime(addr, value)
  local obj, file, data

  -- read data from cache file
  file = '/tmp/lm-scheduler-' .. os.date('%Y%m%d') .. '.lua'
  data = io.readfile(file)

  -- cache file missing, stop
  if not data then
    return
  end

  -- try decoding cache
  data = serialize.decode(data)
  if type(data) ~= 'table' then
    return
  end

  obj = knxlib.encodega(addr)
  for time, events in pairs(data) do
    for _, event in ipairs(events) do
      if event.object == obj and value == toboolean(event.value) then
        return time
      end
    end
  end
end


------------------------------------------


t1 = findtime('1/0/1', true)
t2 = findtime('1/0/1', false)

argument "event" is not a table. log it and see whats inside. also log "data"


RE: Search schedulers - admin - 07.02.2023

Modify the function like this:
Code:
function findtime(addr, value)
  local obj, file, data

  -- read data from cache file
  file = '/tmp/lm-scheduler-' .. os.date('%Y%m%d') .. '.lua'
  data = io.readfile(file)

  -- cache file missing, stop
  if not data then
    return
  end

  -- try decoding cache
  data = serialize.decode(data)
  if type(data) ~= 'table' then
    return
  end

  obj = knxlib.encodega(addr)
  for time, events in pairs(data) do
    if type(time) == 'number' then
      for _, event in ipairs(events) do
        if event.object == obj and value == toboolean(event.value) then
          return time
        end
      end
    end
  end
end