Read values in scheduled script store in ftp - 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: Read values in scheduled script store in ftp (/showthread.php?tid=1622) Pages:
1
2
|
Read values in scheduled script store in ftp - Rauschentofft - 03.10.2018 Hi. I need help with a scheduled script. I want to read 4 byte floating point values throu tags or miltiple tags and store the values to local lm ftp in a csv file. The value from each object should contain: Date, Time, Name, Value and unit/suffix. Is this possible? RE: Read values in scheduled script store in ftp - admin - 03.10.2018 You can use this example as a starting point: http://openrb.com/example-export-last-hour-csv-object-log-file-to-external-ftp-server-from-lm2/ RE: Read values in scheduled script store in ftp - Rauschentofft - 03.10.2018 I was thinking a script like this. But this script is only working in an event. -- Write data to a file in the local FTP value = event.getvalue() logdate = os.date('%Y.%m.%d %H:%M:%S', os.time()) csv = string.format('%q,%q,%q,%q\r\n', logdate, event.dst, grp.alias(event.dst), tostring(value)) log(csv) file, error = io.open("home/ftp/outdoor_temp1.txt","a") log(file,error) file:write(csv) file:close() RE: Read values in scheduled script store in ftp - admin - 03.10.2018 You need to provide an absolute path: Code: file, error = io.open("/home/ftp/outdoor_temp1.txt","a") RE: Read values in scheduled script store in ftp - Rauschentofft - 04.10.2018 Hi. I got this script to work. But i have two qiestions. It is possible to skip past hour data and get the acctual time instead? How read the unit/suffix and add it to the result. Result: --Date, Time, Adress, Name, Value "2018.10.04 07:34:10","33/1/3","Meter 2","1495" --------Script write to FTP---------- -- get past hour data (3600 seconds) logtime = os.time() - 10 * 1 -- list of objects by id objects = {'Mätare'} -- objects with logging enabled query = 'SELECT address, datatype, name FROM objects WHERE disablelog=0' for _, object in ipairs(db:getall(query)) do objects[ tonumber(object.address) ] = { datatype = tonumber(object.datatype), name = tostring(object.name or ''), } end -- csv buffer buffer = { } -- get object logs query = 'SELECT src, address, datahex, logtime, eventtype FROM objectlog WHERE logtime >= ? ORDER BY id DESC' for _, row in ipairs(db:getall(query, logtime)) do object = objects[ tonumber(row.address) ] -- found matching object and event type is group write if object and row.eventtype == 'write' then datatype = object.datatype -- check that object datatype is set if datatype then -- decode data data = knxdatatype.decode(row.datahex, datatype) -- remove null chars from char/string datatype if datatype == dt.char or datatype == dt.string then data = data:gsub('%z+', '') -- date to DD.MM.YYYY elseif datatype == dt.date then data = string.format('%.2d.%.2d.%.2d', data.day, data.month, data.year) -- time to HH:MM:SS elseif datatype == dt.time then data = string.format('%.2d:%.2d:%.2d', data.hour, data.minute, data.second) end else data = '' end -- format csv row logdate = os.date('%Y.%m.%d %H:%M:%S', row.logtime) csv = string.format('\r%q,%q,%q,%q', logdate, knxlib.decodega(row.address), object.name, tostring(data)) -- add to buffer table.insert(buffer, csv) end end ftpfile = string.format('/home/ftp/%s.csv', os.date('%Y-%m-%d_%H-%M')) if #buffer > 1 then data = table.concat(buffer, '\r\n') io.writefile(ftpfile, data) end log(data) RE: Read values in scheduled script store in ftp - admin - 04.10.2018 What do you mean by the actual time? You can get units like this, then use object.units when generating CSV: Code: query = 'SELECT address, datatype, name, units FROM objects WHERE disablelog=0' RE: Read values in scheduled script store in ftp - Rauschentofft - 11.01.2021 Could i get this script to work with a residential script? --Write data to a file in the local FTP value = event.getvalue() logdate = os.date('%Y.%m.%d %H:%M:%S', os.time()) csv = string.format('%q,%q,%q,%q\r\n', logdate, event.dst, grp.alias(event.dst), tostring(value)) log(csv) file, error = io.open("home/ftp/outdoor_temp1.txt","a") log(file,error) file:write(csv) file:close() RE: Read values in scheduled script store in ftp - Daniel - 11.01.2021 Yes only you need to change value = event.getvalue() to value = grp.getvalue('1/1/1') This will work but if you will be writing to FTP which is on SD card every 60s you will kill it quite fast. RE: Read values in scheduled script store in ftp - admin - 11.01.2021 The path is incorrect, it must start with a "/", so it should be /home/ftp/outdoor_temp1.txt Another issue is that you are constantly appending data to a file. If this is done very fast then the file can grow in size quickly and fill the whole partition. And as Daniel mentioned this can degrade the SD card due to constant writes. If you want to send data to external sources look into MQTT. If you want to log data externally then InfluxDB can be used (it has very simple integration with LM). RE: Read values in scheduled script store in ftp - Rauschentofft - 11.01.2021 I am going use scheduled script. Every 6th hour it should read the meters and store it ftp and then send an mail. Then i want to delete the file after the email is sent. I tried with this script and made the changes but i got this error: Error log: test 11.01.2021 17:16:00 User script:7: attempt to index global 'event' (a nil value) stack traceback: User script:7: in main chunk --Write data to a file in the local FTP value = grp.getvalue('16/1/1') logdate = os.date('%Y.%m.%d %H:%M:%S', os.time()) csv = string.format('%q,%q,%q,%q\r\n', logdate, event.dst, grp.alias(event.dst), tostring(value)) log(csv) file, error = io.open("/home/ftp/outdoor_temp1.txt","a") log(file,error) file:write(csv) file:close() RE: Read values in scheduled script store in ftp - admin - 12.01.2021 Replace event.dst in your script with '16/1/1', event variable is not defined in resident and scheduled scripts. If you want to send data as an attachment then you don't need to write it to file at all. See this example: https://forum.logicmachine.net/showthread.php?tid=394 If you need to write some temporary data to a file then use tmp storage (for example "/tmp/myfile.txt"). tmp storage uses RAM so it does not degrade the SD card. RE: Read values in scheduled script store in ftp - Rauschentofft - 12.01.2021 Instead of get past hour data, just read every adress with tag energy. Code: -- Mail Attachment RE: Read values in scheduled script store in ftp - Rauschentofft - 13.01.2021 I have the function now. It is possible to skip the adress in the csv file. I just want Date, Time, Name and Value 2021.01.13 18:38:17,"16/1/3","Mät 3","3400502" 2021.01.13 18:38:12,"16/1/2","Mät 2","200302" 2021.01.13 18:38:08,"16/1/1","Mät 1","10202" Br Code: -- get past hour data (3600 seconds) RE: Read values in scheduled script store in ftp - admin - 14.01.2021 Use this to generate CSV for a certain tag and a certain time: Code: tagname = 'log' RE: Read values in scheduled script store in ftp - Rauschentofft - 17.01.2021 HI. I still got the groupadress in the csv file. RE: Read values in scheduled script store in ftp - admin - 18.01.2021 Like this: Code: tagname = 'log' RE: Read values in scheduled script store in ftp - Rauschentofft - 03.02.2021 Hi. And if i want to email this csv file? RE: Read values in scheduled script store in ftp - admin - 03.02.2021 See this thread: https://forum.logicmachine.net/showthread.php?tid=394 RE: Read values in scheduled script store in ftp - Rauschentofft - 29.04.2021 Hi. In the csv file it looks like this: "date","address","name","value" "2021.02.02 05:00:07","1/6/17","N13A33","11491" "2021.02.02 05:00:07","1/6/16","N10A25","12894" Could i change it so it looks like this instead? date;name;value 2021-02-02;4400A1FS6B;591616,24 2021-02-02;4400A1FS6C;290030,96 RE: Read values in scheduled script store in ftp - admin - 30.04.2021 This should produce what you need. Are you sure that you don't need the time to be included? Code: tagname = 'log' |