Logic Machine Forum
Filtering info from loop - 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: Filtering info from loop (/showthread.php?tid=1498)



Filtering info from loop - FatMax - 24.07.2018

I'm trying to do scheduled scan of a tag weekly to ultimately send me an email with a report if something is wrong. However, I'm seeing that some info is left out. Logically I can see why, but I'm having some trouble fixing the logic for myself to see the solution...

First edition of script:

Code:
ecgFail = grp.tag('ecgFailure')

total = 0

for index, value in ipairs(ecgFail) do
 if value.data == true then
   total = total + 1
   name = value.name
 end
end
log('Found '..total..' ECG(s) with failure. Id´s: '..name)

Output in the log: 

Code:
* string: Found 2 ECG(s) with failure. Id´s: L5 - DALI ECG 3, Failure Status

As you can see I'm missing the name of the second ECG with failure. Any suggestions?


RE: Filtering info from loop - Daniel - 25.07.2018

The log for individual errors should be in the loop.


RE: Filtering info from loop - FatMax - 25.07.2018

(25.07.2018, 14:08)Daniel. Wrote: The log for individual errors should be in the loop.

Yeah, I figured that might be the solution. However, the end goal is to get them all neatly listed in an email. Wouldn't that just do the count wrong then? I understand I don't need the count, but its fun to learn something new in the process Wink


RE: Filtering info from loop - Daniel - 26.07.2018

Hi
Try something like that

Code:
ecgFail = grp.tag('ecgFailure')

total = 0
buffer = { 'Name' }
for index, value in ipairs(ecgFail) do
 if value.data == true then
   total = total + 1
   name = value.name
 end
 
buffer[ #buffer + 1 ] = table.concat({
 name,
}, ',')
end
csv = string.char(0xEF, 0xBB, 0xBF) .. table.concat(buffer, '\r\n')
res, err = mailattach('someone@example.com', 'Report', 'CSV file attached', 'report.csv', csv, 'text/csv')

log('Found '..total..' ECG(s) with failure. Id´s: '..name)

Use email function from here
https://forum.logicmachine.net/showthread.php?tid=394&pid=1994#pid1994

BR