Pomodoro Timer for Streamdeck

So I found this:

function millisToMinutesAndSeconds(millis) {
  var minutes = Math.floor(millis / 60000);
  var seconds = ((millis % 60000) / 1000).toFixed(0);
  return minutes + ":" + (seconds < 10 ? '0' : '') + seconds;
}

millisToMinutesAndSeconds(298999); // "4:59"
millisToMinutesAndSeconds(60999);  // "1:01"

And thought perhaps I could modify (in the widget area):

returnToBTT(timeAllowed - timePassed + ' s'); // returning the formatted value to BTT, so it can display it on the button

to

returnToBTT(millisToMinutesAndSeconds(timeAllowed - timePassed));

But that didn't work. Is that a function inside a function? Is that allowed? Any ideas from anyone?

To use a function inside a function do it like this:

let millisToMinutesAndSeconds = (millis)  => {
  var minutes = Math.floor(millis / 60000);
  var seconds = ((millis % 60000) / 1000).toFixed(0);
  return minutes + ":" + (seconds < 10 ? '0' : '') + seconds;
}

millisToMinutesAndSeconds(298999);

Many thanks Andreas. Is this what you mean? That doesn't work for me. And when I say it doesn't work, I mean to say, it shows '0:00' on the button. If I change the timeAllowed from 300 to 3000, the counter shows 0:03, which makes me think it's showing minutes rather than seconds. I tried deleting the /1000 in the 'let timePassed =' line, but that didn't help. In a few second, I should know if that's 0:03 is going to move to 0:02. No, I'm sure a minute has passed and it hasn't changed.

Edit: returnToBTT(millisToMinutesAndSeconds(298999));
Is displaying 4:59 as expected.

(async ()=> {

let timeAllowed = 300; //seconds to count down from
let startTime = await callBTT('get_number_variable', {variable_name:'timer_start_time'}); // the time the timer was started, this is set in the assigned action
let timePassed = Math.round((Date.now()-startTime)/1000); // converting from milliseconds to seconds and rounding

let millisToMinutesAndSeconds = (millis)  => {
  var minutes = Math.floor(millis / 60000);
  var seconds = ((millis % 60000) / 1000).toFixed(0);
  return minutes + ":" + (seconds < 10 ? '0' : '') + seconds;
}


returnToBTT(millisToMinutesAndSeconds(timeAllowed - timePassed)); // returning the formatted value to BTT, so it can display it on the button

// If the count down is finished, write some text 
  if (timeAllowed - timePassed < 0) returnToBTT("Time!")
  })();

Seems to work for me (just note, timeAllowed and timePassed need to be milliseconds for this function to work:

(async () => {
  let timeAllowed = 300; //seconds to count down from
  let startTime = await callBTT("get_number_variable", {
    variable_name: "timer_start_time",
  }); // the time the timer was started, this is set in the assigned action
  let timePassed = Math.round((Date.now() - startTime) / 1000); // converting from milliseconds to seconds and rounding

  let millisToMinutesAndSeconds = (millis) => {
    var minutes = Math.floor(millis / 60000);
    var seconds = ((millis % 60000) / 1000).toFixed(0);
    return minutes + ":" + (seconds < 10 ? "0" : "") + seconds;
  };

  if (timeAllowed - timePassed < 0) {
    // If the count down is finished, write some text
    returnToBTT("Time!");
  } else {
    returnToBTT(
      millisToMinutesAndSeconds(timeAllowed * 1000 - timePassed * 1000)
    ); // returning the formatted value to BTT, so it can display it on the button
  }
})();

1 Like

@Andreas_Hegenberg

It works! I had an idea it was a milli vs secs thing, and was trying to fix it at the let timePassed section.

Awesome. I know have a timer that counts down on the button. I'll see if I can add an alarm, a 2nd push to pause, and long hold to reset, then share.

Cheers!

2 Likes

Hey,
I came to this forum to ask something very similar. :slight_smile:
Were you able to figure this out?

I'm desperately looking for a solution to start and stop a countdown.

FWIW, if you're using BTT as a plugin rather than having it completely control the Stream Deck, there's a plugin called Tomato Timer that you could use instead. It's pretty configurable:

image

Thank you, I tried that before Andreas implemented Stream Deck support.
However, I ditched the original software as it was a resource hog and let BTT take over fully.

Maybe it would be possible to use the "Set/Assign Value to Variable" action:
Screenshot 2023-03-06 at 21.37.07

And store a "state" variable like "is_running", which then can be checked before the script is (re)started.