LUA1: Writing item values to CSV files

The integrated LUA engine provides also the possibility to read and write to files. This short LUA tutorial shall demonstrate how item values can be written to a CSV file in a defined interval.

To use this tutorial, please do the following steps in advance:

  • Install the NETx BMS Server. The setup can be found here at our website.
  • Create a new BMS server workspace and integrate some data points that you want to store to a CSV file.

Creating a LUA function to store data to CSV files

First, we will create a new LUA function which provides the possibility to write the current value and the current time to a CSV file. Within the toolbar of the NETx BMS Studio, click on the "Edit Script" button.

Afterwards, you can select the LUA file that you want to manipulate. Select "nxaDefinitions.lua" to open the main LUA file.

LUA code can be split into different files to organize it. The "nxaDefinitions.lua" is the main file which is loaded by the NETx BMS Server automatically. All other LUA files must be imported by using the "Require" statement at the beginning of "nxaDefinitions.lua".

Then start to implement a new LUA function called "LogToCsv". You can implement at any location outside the other location. Paste in the following code:

function LogToCsv(item, filename)
 
    local value = nxa.GetValue(item)
    local timeStamp = nxa.DateToString(nxa.Now())
 
    local fp = assert(io.open(filename, "a"))
    
    fp:write(item .. ";" .. timeStamp .. ";" .. value .. "\n")
 
    fp:close()
 
end

The function takes two parameters as input:

  • "item": this parameter specifies the ItemID of the data point which shall be monitored
  • "filename": here the name of the CSV file is specified.

The function gets the current value of the specified Server Item and stores it together with the ItemID and the current time into the defined file. ";" is used as delimiter. After having finished the implementation, press the "Save" and "Reload" button within the LUA editor in order to reload the newly defined LUA code.

Testing the LUA code

To test the LUA code, select "Execute LUA script ..." from the "Tools" menu within the NETx BMS Server. A small dialog opens which can be used to invoke LUA function directly during runtime. Enter "LogToCsv" within the dialog and provide an ItemID and a filename as parameter. Within the following example, the KNX group address 5/1/0 of the KNXnet/IP router 192.168.1.36 is used as data point and "C:\Users\wolfgang.granzer\Desktop\test.csv" as CSV file.

Note that the escape character ("\") which is used as delimiter within the ItemID is a special character within LUA. Therefore, double escapes ("\\") have to be used.

 

Execute the LUA code periodically

In order to execute the "LogToCsv" LUA function periodically, you can used the predefined LUA timer callbacks "OnSecondTimerEvent()", "OnMinuteTimerEvent()" and "OnHourTimerEvent()". If you want to invoke the LUA function within an interval different than 1 second, 1 minute or 1 hour, you can use the modulo operator. The following example shows, how the LogToCsv function is invoked every 15 minutes.

--==============================================================================
-- OnMinuteTimerEvent() - function is called every minute
--==============================================================================
function OnMinuteTimerEvent()
    if (nxa.Minute(nxa.Now()) % 15 == 0)
    then
        LogToCsv("NETx\\XIO\\KNX\\192.168.1.36\\05/1/000", "C:\\Users\\wolfgang.granzer\\Desktop\\test.csv")
    end
end