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.
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
You can get units like this, then use object.units when generating CSV:
Code:
12345678
query = 'SELECT address, datatype, name, units FROM objects WHERE disablelog=0'for_, objectinipairs(db:getall(query)) doobjects[ tonumber(object.address) ] = {
datatype = tonumber(object.datatype),
name = tostring(object.nameor''),
units = tostring(object.unitsor''),
}
end
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.
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).
11.01.2021, 16:04 (This post was last modified: 11.01.2021, 16:17 by Rauschentofft.)
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
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.