Hello everyone
I have a problem with my script. I'm getting errors sleepStart and SleepEnd. I guess in date format ? "const diffMs = sleepEnd - sleepStart;" Can someone help me ?
// Überprüfen, ob die Konvertierung erfolgreich war
if (isNaN(sleepStart.getTime()) || isNaN(sleepEnd.getTime())) {
node.error("Ungültige Datumsangaben für 'sleepStart' oder 'sleepEnd'.");
return null;
}
// Berechne die Differenz in Millisekunden und konvertiere in Stunden und Minuten
const diffMs = sleepEnd - sleepStart;
const diffHours = Math.floor(diffMs / (1000 * 60 * 60)); // Ganze Stunden
const diffMinutes = Math.floor((diffMs % (1000 * 60 * 60)) / (1000 * 60)); // Verbleibende Minuten
// Ausgabe der Differenz als z. B. "8:45"
msg.payload = `${diffHours}:${diffMinutes.toString().padStart(2, '0')}`;
// Rückgabe der Nachricht mit der berechneten Schlafdauer
return msg;
There’s a great page in the docs (Working with messages : Node-RED) that will explain how to use the debug panel to find the right path/value for any data item.
Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.
To the issue
const diffMs = sleepEnd - sleepStart;
sleepEnd and sleepStart are still time objects, you have not create variables that contain the getime() integers. Try creating two new vars using sleepEnd.getTime(), and use these vars for your calculation.