I make a downtime timer. it operates similarly to stopwatch. but the problem is i cant display the timer in hour:minute:seconds . after 60sec it continues to 61 instead 1:01. can anyone help me?

downtime java
downtime

Hello and welcome to the forum.

Your question is not really Node-Red specific... but more of a "How program a thing in javascript"

There are many options out there that can easily be put in a function node.

https://www.google.com/search?q=javascript+timer+hours+minutes+seconds

And then there are many timer nodes available. Search for timer in the palette for one that might work for your needs.

I have a function I found online that I use to calculate downtime duration. I eliminated seconds from it because I didn't need it for my purposes.

In my case, I have a Pi running Node Red at each of our machines that reads PLC data and saves it to a MySQL db at our central status display. When a machine has an error, the Pi creates a timestamp using the function getDateTime() and saves that to the database. The central display sees that timestamp, gets the current date and time and calls the function timeDiffCalc(Now, Last_Update) to create a string to display the downtime.

I hope this helps.

Chris

function getDateTime() {
    var now     = new Date(); 
    var year    = now.getFullYear();
    var month   = now.getMonth()+1; 
    var day     = now.getDate();
    var hour    = now.getHours();
    var minute  = now.getMinutes();
    var second  = now.getSeconds(); 

    if(month.toString().length == 1) {
        month = '0'+month;
    }
    
    if(day.toString().length == 1) {
        day = '0'+day;
    }   

    if(hour.toString().length == 1) {
        hour = '0'+hour;
    }
    
    if(minute.toString().length == 1) {
        minute = '0'+minute;
    }
    
    if(second.toString().length == 1) {
        second = '0'+second;
    }   
    var dateTime = year + '-' + month + '-' + day + ' ' + hour + ':' + minute;   
    return dateTime;
}

function timeDiffCalc(Now, Last_Update) {
    let diffInMilliSeconds = Math.abs(Now - Last_Update) / 1000;

    // calculate days
    const days = Math.floor(diffInMilliSeconds / 86400);
    diffInMilliSeconds -= days * 86400;
    console.log('calculated days', days);

    // calculate hours
    const hours = Math.floor(diffInMilliSeconds / 3600) % 24;
    diffInMilliSeconds -= hours * 3600;
    console.log('calculated hours', hours);

    // calculate minutes
    const minutes = Math.floor(diffInMilliSeconds / 60) % 60;
    diffInMilliSeconds -= minutes * 60;
    console.log('minutes', minutes);

    let difference = '';
    if (days > 0) {
      difference += (days === 1) ? `${days} Day, ` : `${days} Days, `;
    }

    if (hours > 0) {
        difference += (hours === 0 || hours === 1) ? `${hours} Hour, ` : `${hours} Hours, `;
    }

    difference += (minutes === 0 || hours === 1) ? `${minutes} Minutes` : `${minutes} Minutes`; 

    return difference;
}

timeDiffCalc(new Date(Now), new Date(Last_Update))

thank you for the help!. I have already found the way to solve this. i just create a function and call it in the msg.payload

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.