Hello dear community,
this is my first post in this forum, so please don't be too strict.
In my module IOT Smart Devices at the university I have to write data into a database with the help of a mqtt broker, which is collected with a smart socket.
This is actually only background knowledge and not too important I think.
My task is now to commutate the power data that the socket reads out and sends to the SQL database with a timestamp and to calculate a daily consumption and to create a bar chart with the daily consumption of the last 7 days from the irregular entries. Unfortunately, the smart socket has been switched off in the meantime, which is why we have large gaps in the data. The socket is now transmitting data to the SQL database again.
ChatGPT has helped me so far, but after 2 days of troubleshooting I'm running out of steam and am now trying to get help this way! Please help me to display the bar chart.
My flow consists of an Inject -> Function ( SQL query) -> My SQL (database) -> Function (data preparation) -> ui_chart (daily consumption of the last 7 days "bar chart")
function for SQL Query:
msg.topic = `SELECT DATE(timestamp) as Date, SUM(Power) as DailyPower
FROM Shelly_Data
WHERE timestamp >= NOW() - INTERVAL 7 DAY
GROUP BY DATE(timestamp)
ORDER BY DATE(timestamp) ASC`;
return msg;
function for data formatting:
irgnore the comments if you are not German. The plan is to get a value for every day of the last week. If there is no day with values, it should get the value zero.
// Funktion zur Formatierung der Daten für den ui_chart
let data = msg.payload;
// Debug-Ausgabe der Rohdaten
node.warn("Rohdaten: " + JSON.stringify(data));
// Erstellen eines Arrays für die letzten 7 Tage ab heute
let today = new Date();
let labels = [];
let seriesData = [];
// Füllen der Labels für die letzten 7 Tage
for (let i = -6; i <= 0; i++) {
let date = new Date(today);
date.setDate(date.getDate() + i);
labels.push(date.toISOString().split('T')[0]); // Format: YYYY-MM-DD
seriesData.push(0); // Initialisieren mit 0
}
// Zuordnen der tatsächlichen Daten zu den Labels
if (Array.isArray(data) && data.length > 0) {
data.forEach(item => {
let date = new Date(item.Date).toISOString().split('T')[0];
let index = labels.indexOf(date);
if (index > -1) {
seriesData[index] = parseFloat(item.DailyPower); // Sicherstellen, dass es eine Zahl ist
}
});
}
// Formatieren des Payloads für den ui_chart
msg.payload = {
labels: labels,
series: [seriesData]
};
// Debug-Ausgabe des formatierten Payloads
node.warn("Formatierter Payload: " + JSON.stringify(msg.payload));
// Rückgabe des msg Objekts
return msg;