Homey Community

Countdown to New Year


#1

Hi there,

I want to make a countdown flow to new year.
To do this I created a timer named Nieuwjaaraftellen

I created a flow that starts at 23.59 (since you can not start aflow at 23.59.50) that sets Nieuwjaaraftellen to 60

If Nieuwjaaraftellen >11 then the actual countdown starts

The flow works great but…
If I compare it with the computer time, Homey’new year begins a few seconds later.

Does anyone have a solution?

Thanks in advance.

Hans


#2

Correct the time on your computer :joy::rofl:


#3

Let the countdown start a few seconds earlier. You stil got one night to test🤣


#4

I can also make ten flows and start them manually the last ten seconds.
But that is not what I had in mind.

Has anyone else has a suggestion?


#5

IIRC the time on Homey is synched with timeservers when you do a reset (holding upside down) with keeping your settings…


#6

Of course I was not serious, although what is exact your problem?

I checked my computer and it was ~15 seconds before Homey…
Both Synchronize with internet servers.
Force syncing my computer solved it as Homey probably was already almost correct!
After that a visual check both where within ~ 1 second.

I guess a restart of Homey should be enough to force Homey to check the correct internet time as there is no way to get into Homeys OS to force it. (not sure but I think reset/recovery will also do but is somewhat to much.)

Problem is, what do you think is the correct time ???

look at the broadcasting of World Cup football you see several seconds (~5-10 ?) difference in viewing TV broadcast signals received if hear the goals in the neighborhood …
(Satellite vs Digital cable vs DVB-T vs Analog signal)

Or are you looking at the railway station Clock’s?
that have a second-hand going round in ~55 seconds to wait at 59 Seconds for the Minute signal to release and start their round again?
These railway stations where the cause of the need to synchronize time around the country anyway…

Anyway if you like the Internet synchronized time of Homey you could use a HomeyScript for it to do the trick just after the second… I tried to synchronize exactly on the seconds but that is a problem
It is not easy to count exactly at each second, more than 95% of the time it is 1 - 40 ms to late, but sometimes just to early counting for example 5 two times and missing 4 :cry:

For My Homey it works best to try to count on ~50-100 ms after each second. (to prevent missing seconds.)
I think I can live with that :wink:

You can schedule it every hour just 2 minutes before the hour from 11 o’ cock in the morning (for The Netherlands) as it will be New-Year somewhere in the world every Hour :wink:

Here is the HomeyScript,
just what I could think of and maybe not optimized…

// CountDown Script 
/*  Count down to a set Time, 
    Schedule/start this script ~ 2 minutes before the time
    it synschronizes on the Second + ~50 ms and counts from 9 to 1 in the last Seconds of the minute.  

*/
let messageToSpeak = 'Happy Newyear!'; 
// let messageToSpeak = 'Gelukkig Nieuwjaar!'; 
let countMin = 59; // The minute we count down the seconds back to 1. Often countMin = 59
let stopMin =  (countMin+1)%60; // the minut we stop ;-) 
let maxRuntime = 4; 
let sys = await  Homey.system.getInfo();
var loopDelay = 1;
let extraDelay = 50; // to prevent sometimes starting just before the next second ;-) 
var theTime = new Date(sys.date);
var secondsToGo = 60-theTime.getSeconds();
var miliseconds = theTime.getMilliseconds();
var theMin  = theTime.getMinutes();

console.log(theTime, theTime.getMilliseconds());
log ('Count during ', countMin, ' until ', stopMin);
log (sys.date, miliseconds, loopDelay);
var loopDelay = Math.floor((loopDelay + miliseconds+ 2)/2);
log (sys.date, miliseconds, loopDelay);

async function now (arg) {
    sys = await Homey.system.getInfo();
    theTime = new Date(sys.date);
    secondsToGo = 60-theTime.getSeconds();
    miliseconds = theTime.getMilliseconds();
    if (miliseconds < 200)  { 
        loopDelay = Math.floor((loopDelay + miliseconds)/2);
        newDelay =  999 - loopDelay;
    } else {
        newDelay = miliseconds < 800 ? 1000-miliseconds-loopDelay : 2000-miliseconds;
    }
    theMin  = theTime.getMinutes();
    log (arg, sys.date, loopDelay, newDelay);
    if ((theMin === countMin) && (secondsToGo < 10) ) {
        log('SecondsToGo: ', secondsToGo );
        say(secondsToGo.toString());
    } 
    if (theMin != stopMin && (countMin >= theMin) && ((countMin - theMin)<maxRuntime)) {
         _.delay( now, newDelay+extraDelay, 'CountingDown: ' ) 
    } else {
         log('stop at ', theTime, theMin,  stopMin );  // break
         if (theMin === stopMin ) {
            say(messageToSpeak);
            log(messageToSpeak);
         }
    }
}
 _.delay( now, (1000-miliseconds), 'CountingDown: ' ) 

See here the result.

2018-12-30T22:58:14.818Z 818
Count during  59  until  0
2018-12-30T22:58:14.818Z 818 1
2018-12-30T22:58:14.818Z 818 410

----------------
Script returned:
 undefined
CountingDown:  2018-12-30T22:58:15.058Z 234 765
CountingDown:  2018-12-30T22:58:**15.908Z 234 1092**
CountingDown:  2018-12-30T22:58:17.093Z 163 836
CountingDown:  2018-12-30T22:58:18.093Z 128 871
CountingDown:  2018-12-30T22:58:19.050Z 89 910
CountingDown:  2018-12-30T22:58:20.062Z 75 924
CountingDown:  2018-12-30T22:58:21.085Z 80 919
CountingDown:  2018-12-30T22:58:22.093Z 86 913
CountingDown:  2018-12-30T22:58:23.096Z 91 908
CountingDown:  2018-12-30T22:58:24.101Z 96 903
CountingDown:  2018-12-30T22:58:25.094Z 95 904
CountingDown:  2018-12-30T22:58:26.108Z 101 898
CountingDown:  2018-12-30T22:58:27.100Z 100 899
CountingDown:  2018-12-30T22:58:28.093Z 96 903
CountingDown:  2018-12-30T22:58:29.091Z 93 906
CountingDown:  2018-12-30T22:58:30.097Z 95 904
CountingDown:  2018-12-30T22:58:31.114Z 104 895
CountingDown:  2018-12-30T22:58:32.106Z 105 894
CountingDown:  2018-12-30T22:58:33.180Z 142 857
CountingDown:  2018-12-30T22:58:34.268Z 142 590
CountingDown:  2018-12-30T22:58:**34.945Z 142 1055**
CountingDown:  2018-12-30T22:58:36.092Z 117 882
[...]
CountingDown:  2018-12-30T22:59:44.113Z 102 897
CountingDown:  2018-12-30T22:59:45.106Z 104 895
CountingDown:  2018-12-30T22:59:46.167Z 135 864
CountingDown:  2018-12-30T22:59:47.117Z 126 873
CountingDown:  2018-12-30T22:59:48.080Z 103 896
CountingDown:  2018-12-30T22:59:49.076Z 89 910
CountingDown:  2018-12-30T22:59:50.077Z 83 916
CountingDown:  2018-12-30T22:59:51.086Z 84 915
SecondsToGo:  9
CountingDown:  2018-12-30T22:59:52.093Z 88 911
SecondsToGo:  8
CountingDown:  2018-12-30T22:59:53.099Z 93 906
SecondsToGo:  7
CountingDown:  2018-12-30T22:59:54.296Z 93 611
SecondsToGo:  6
CountingDown:  2018-12-30T22:59:55.151Z 122 877
SecondsToGo:  5
CountingDown:  2018-12-30T22:59:56.128Z 125 874
SecondsToGo:  4
CountingDown:  2018-12-30T22:59:57.168Z 146 853
SecondsToGo:  3
CountingDown:  2018-12-30T22:59:58.154Z 150 849
SecondsToGo:  2
CountingDown:  2018-12-30T22:59:59.100Z 125 874
SecondsToGo:  1
CountingDown:  2018-12-30T23:00:00.079Z 102 897
stop at  2018-12-30T23:00:00.079Z 0 0
Happy Newyear!

#7

Hi Dijker,

Thank you for taking the time for your reply.
Rebooting Homey synced the time between the computer and Homey indeed within 1-2 seconds. Close enough.
The main delay in my flow seems to occur when I let Homey say 10, 9, 8…
I will experiment with your script today since I am not that familiar with Javascript.


#8

Hi Geurt,

It works like a charm
Thank you very much!
Happy New Year!!!