This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Dynamic table of logs
#1
Good afternoon,

Perhaps you can help me with a task that I have to accomplish. I'm working with the latest version of HL (2.0.0).

I have to perform an alarm control, and create a log with the last 30 or 40 alarms generated by the system; This system is a solar inverter that communicates via modbus with the HL. This communication is correct.

I attach an image of the book of modbus variables of the manufacturer, where you can observe the operation, as well as the variables already visible in HL.

I have configured the HL so that I ask the modbus interface every 60 seconds, so every 60 seconds I get new information in a loop, the object 1/1/7 is the index -> modbus adress 3001. Every time I ask A modbus read, the rest of variables change to their corresponding register.

The date of the alarm gives me in seconds since 1970 ... I'm not very clear how to do this, when I want to initialize a variable of time in 1970, the system changes it to 2070 ... I'm a little lost.

The ideal would be to visualize by alarms rows, and that these could be on written ones ...
And if I could already send the whole log by email once a week, for example, it would be extraordinary.

I hope someone can advise me a little, you would be grateful.

Thank you very much in advance

Alberto.

Attached Files Thumbnail(s)
       
Reply
#2
Hi,

Use this as event based script attached to object 1/1/8:
Code:
if event.getvalue() == 1 then -- active
 alert('There is a alarm with code ' .. grp.getvalue('1/1/9') .. ' at ' .. os.date("%c", grp.getvalue('1/1/6'))
end
It will generate a alert inside the alert tab, and that will be used in the alert app, can be downloaded from the appstore in FW 2.0.0.

There you will get a audio alert and a list with all available alerts.

To mail the alerts use this script as scheduled once a week: (make sure to enable 'less secure apps' in your gmail account to be able to mail and set DNS and default gateway on NIC)
Code:
--Gmail (smtp) username !IMPORTANT!
user = 'YOUR EMAIL ADRESS'

--Gmail (smtp) password !IMPORTANT!
password = 'YOUR PASSWORD'

--Sender for e-mail
from = '<' .. user .. '>'
alias_from = 'YOUR ALIAS'

--Recipient for e-mail
to = '<receiver@domain.com>'
alias_to = 'receiver'

--Subject for e-mail
subjectpart1 = 'Alerts'
subjectpart2 = 'automaticly send by homeLYnk'

--Message on bottom of email (will only be showed when client don't understand attachment)
epilogue = 'End of message'

-- Get all alerts from DB
alerts_table = db:getall('SELECT * FROM alerts')

-- csv buffer
buffer = {}
-- format csv row
csv = string.format('%q,%q,%q', "ID", "ALERT", "ADDED TO LIST")
-- add to buffer
table.insert(buffer, csv)
-- add empty line to buffer
table.insert(buffer, "")

-- Loop through alerts_table
for _, alerts in ipairs(alerts_table) do

 -- format csv row
 csv = string.format('%q,%q,%q', alerts.id, alerts.alert, os.date("%x %X", alerts.alerttime))
 -- add to buffer
 table.insert(buffer, csv)
end

--Create table to include mail settings
local settings = {
   from = from,
   rcpt = to,
   user = user,
   password = password,
   server = 'smtp.gmail.com',
   port = 465,
   secure = 'sslv23',
}

--Create attachment inside FTP server
src = 'AlertExport '.. os.date('%Y-%m-%d %H#%M#%S') .. '.csv'
dst = '/home/ftp/' .. src
io.writefile(dst, buffer)

--Create subject
subject = subjectpart1 .. ": " .. src .. " " .. subjectpart2

--Load required modules to send email with attachment
local smtp = require("socket.smtp")
local mime = require("mime")
local ltn12 = require("ltn12")

--Create e-mail header
settings.source = smtp.message{
headers = {
         from = '' .. alias_from .. ' ' .. from .. '',
         to = '' .. alias_to .. ' ' .. to .. '',
         subject = subject
},

--Load attachment inside body    
body = {
preamble = "",
[1] = {  
       headers = {
          ["content-type"] = 'text/plain',
          ["content-disposition"] = 'attachment; filename="'..src..'"',
          ["content-description"] = '.. src ..',
          ["content-transfer-encoding"] = "BASE64",
       },
       body = ltn12.source.chain(
         ltn12.source.file(io.open(dst, "rb")),
         ltn12.filter.chain(
         mime.encode("base64"),
         mime.wrap()
       )
     )
   },
     epilogue = epilogue
 }
}

--Send the email
r, e = smtp.send(settings)

--Create alert when sending gives an error with error message
if (e) then
 log (e)
 log (r)
 alert("Could not send email: ", e, "\n")
end

--Delete created file from ftp folder inside HL
os.remove(dst)
Reply
#3
(20.06.2017, 20:11)Erwin van der Zwart Wrote: Hi,

Use this as event based script attached to object 1/1/8:
Code:
if event.getvalue() == 1 then -- active
 alert('There is a alarm with code ' .. grp.getvalue('1/1/9') .. ' at ' .. os.date("%c", grp.getvalue('1/1/6'))
end
It will generate a alert inside the alert tab, and that will be used in the alert app, can be downloaded from the appstore in FW 2.0.0.

There you will get a audio alert and a list with all available alerts.

To mail the alerts use this script as scheduled once a week: (make sure to enable 'less secure apps' in your gmail account to be able to mail and set DNS and default gateway on NIC)
Code:
--Gmail (smtp) username !IMPORTANT!
user = 'YOUR EMAIL ADRESS'

--Gmail (smtp) password !IMPORTANT!
password = 'YOUR PASSWORD'

--Sender for e-mail
from = '<' .. user .. '>'
alias_from = 'YOUR ALIAS'

--Recipient for e-mail
to = '<receiver@domain.com>'
alias_to = 'receiver'

--Subject for e-mail
subjectpart1 = 'Alerts'
subjectpart2 = 'automaticly send by homeLYnk'

--Message on bottom of email (will only be showed when client don't understand attachment)
epilogue = 'End of message'

-- Get all alerts from DB
alerts_table = db:getall('SELECT * FROM alerts')

-- csv buffer
buffer = {}
-- format csv row
csv = string.format('%q,%q,%q', "ID", "ALERT", "ADDED TO LIST")
-- add to buffer
table.insert(buffer, csv)
-- add empty line to buffer
table.insert(buffer, "")

-- Loop through alerts_table
for _, alerts in ipairs(alerts_table) do

 -- format csv row
 csv = string.format('%q,%q,%q', alerts.id, alerts.alert, os.date("%x %X", alerts.alerttime))
 -- add to buffer
 table.insert(buffer, csv)
end

--Create table to include mail settings
local settings = {
   from = from,
   rcpt = to,
   user = user,
   password = password,
   server = 'smtp.gmail.com',
   port = 465,
   secure = 'sslv23',
}

--Create attachment inside FTP server
src = 'AlertExport '.. os.date('%Y-%m-%d %H#%M#%S') .. '.csv'
dst = '/home/ftp/' .. src
io.writefile(dst, buffer)

--Create subject
subject = subjectpart1 .. ": " .. src .. " " .. subjectpart2

--Load required modules to send email with attachment
local smtp = require("socket.smtp")
local mime = require("mime")
local ltn12 = require("ltn12")

--Create e-mail header
settings.source = smtp.message{
headers = {
         from = '' .. alias_from .. ' ' .. from .. '',
         to = '' .. alias_to .. ' ' .. to .. '',
         subject = subject
},

--Load attachment inside body    
body = {
preamble = "",
[1] = {  
       headers = {
          ["content-type"] = 'text/plain',
          ["content-disposition"] = 'attachment; filename="'..src..'"',
          ["content-description"] = '.. src ..',
          ["content-transfer-encoding"] = "BASE64",
       },
       body = ltn12.source.chain(
         ltn12.source.file(io.open(dst, "rb")),
         ltn12.filter.chain(
         mime.encode("base64"),
         mime.wrap()
       )
     )
   },
     epilogue = epilogue
 }
}

--Send the email
r, e = smtp.send(settings)

--Create alert when sending gives an error with error message
if (e) then
 log (e)
 log (r)
 alert("Could not send email: ", e, "\n")
end

--Delete created file from ftp folder inside HL
os.remove(dst)

Many thanks!
I'm going to implement it.

Best regards
Reply


Forum Jump: