Author Topic: Lua CSV to Table  (Read 44 times)

Offline TamarackGhost

  • Newbie
  • *
  • Posts: 5
  • Karma: +0/-0
Lua CSV to Table
« on: June 23, 2018, 12:20:08 am »
The code below is a segment from my irrigation controller code. When I use Lua Test and add a few print commands, the pastPrecipTable[day] prints the value from the table, but the local precipForDayOne variable is returning a nil value. Any ideas?

The code is from here: http://forum.micasaverde.com/index.php?topic=24538.0 and here: https://forum.mysensors.org/uploads/files/1440956563038-pleg-irrigation.pdf

Code: [Select]
--Convert CSV string to a lua table(
function csvToTable(csvString)
   local t={}
   local i=1

   for str in string.gmatch(csvString, "([^, ]+)") do
      t[i] = str
      i = i + 1
   end

   return t
end

--Get past precipitation as CSV string
local pastPrecipCSV = luup.variable_get(RAIN_SID,"PastPrecip",RAIN_ID)

--Convert CSV string to a lua table
local pastPrecipTable = csvToTable(pastPrecipCSV)

--Number of elements in table
local tableSize = #pastPrecipTable

for day = 1, tableSize do
   --Get past precip for each day
   local precipForDayOne = pastPrecipTable[day]
end

for day = 2, tableSize do
   --Get past precip for each day
  local precipForDayTwo = pastPrecipTable[day]
end

luup.variable_set(VAR_SID,"Variable1",precipForDayOne,VAR_ID)
luup.variable_set(VAR_SID,"Variable2",precipForDayTwo,VAR_ID)