Hi Everyone,
I am new to this.
Just after some advice or direction on how to do this.
What I'm trying to achieve - I have a flow that runs on a time trigger -> extracts data from a database -> formats that data from the database -> emails out a report to users of that data. So to email out a daily report of automated doors battery levels, so that i know when they need to be replaced.
trigger (daily time trigger) -> SQL Database extraction -> function to format data from SQL Extraction-> email out a report
The sql database pumps out an array object to the function, I didn't want to hard code anything because the size of the array can change daily due to the database SQL query results so I need to somehow declare the variables for formatting from the sql query (array object).
I am trying to declare variables from each array object every time the flow runs.
i decided i could try and achieve it through a forloop to go through each element but not sure on how to do this.
im not a coder so please be gentle
see image and code example below. (EDITED, added the email format function)
SQL Extraction query is simple:
SELECT TOP (20) [id_lock]
,[name]
,[Description]
,[Battery]
FROM [BATTERY_RW].[dbo].[BP_Locks] where Battery < 11 order by Battery asc
Here is an example of data extraction from SQL query that I'm working with.
[{"id_lock":1271,"name":"Front Door 1","Description":"Front Door Battery Lock","Battery":10},{"id_lock":1249,"name":"Side Door 2","Description":"Side Door Battery Lock","Battery":3},{"id_lock":1177,"name":"Back Door 3","Description":"Back Door Battery Lock","Battery":5}]
then this is the function that formats the data:
var counter = msg.payload.length;
msg.topic = "CRITICAL - Low Door Battery Report";
msg.from = "Node-Red-Door-Batteries@Reports.com";
for (var i=0; i < counter; i++)
{
// don't need this anymore msg.SD[i] = ("Battery Lock Doors Door - " + msg.payload[i]["name"] + " - Battery Level: " + msg.payload[i]["Battery"] + "%");
msg.SDB[i] = ("CRITICAL - Battery Level: " + msg.payload[i]["Battery"] + "%");
msg.SDO[i] = (msg.payload[i]["name"]);
return msg; //testing whats returned
}
I pass variables -> msg.SD1,msg.SD2,msg.SDB1, msg.SDB2, msg.SDO1, msg.SDO2 etc into the email part but i don't know how to declare SD1, SD2 etc based on the array element
sorry forgot to add the html email formatting function -
html email
<html>
<head>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><title>Salto Doors 20 List</title>
</head>
<body>
<p>
Battery Door - {{{SDO1}}}
</p>
<p>
Battery Level - {{{SDB1}}}
</p>
<p>
Battery Door 2 - {{{SDO2}}}
</p>
<p>
Battery Level - {{{SDB2}}}
</p>
</body>
</html>
thank you for your help in advance!
hope that makes sense!
cheers
Ed