MagicMirror Forum
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • 3rd-Party-Modules
    • Donate
    • Discord
    • Register
    • Login
    A New Chapter for MagicMirror: The Community Takes the Lead
    Read the statement by Michael Teeuw here.

    Not updating at midnight...

    Scheduled Pinned Locked Moved Solved Troubleshooting
    24 Posts 3 Posters 3.2k Views 3 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S Offline
      sdetweil @BKeyport
      last edited by sdetweil

      @BKeyport the code sets the interval to midnight -now

      So unless this is started at midnight- it will update whenever

      So you need a set timeout for now til midnight, and then an interval for every midnight since this first midnight

      So updateDom
      Now
      Timeout Midnight tonight
      Start interval for next and following midnights

      Sam

      How to add modules

      learning how to use browser developers window for css changes

      BKeyportB 1 Reply Last reply Reply Quote 0
      • BKeyportB Online
        BKeyport Module Developer @sdetweil
        last edited by BKeyport

        I think you see it backwards.

        Look at the reset variable again.

        This line calculates the difference between the current time and midnight of the next day. It does this by creating a new Date object for the next day at 00:00:00.000 (midnight) and subtracting the current time from it.

        The variable timer is then set using the reset variable, so…

        new Date(year, month, nextday, 0, 0, 0, 0).getTime() - date.getTime()

        Assuming it’s currently 6:30pm on August 8th, 2024.

        2024/8/9 0:0:0.0 - 2024/8/8 18:30:0.0
        1723186800 - 1723167000 (Dropping the milliseconds if used)
        = 19800

        then the timer is set using setInterval() to “reset” or 19800 - which would be the next midnight.

        After the first one, it should be calculating so close to midnight, it’s an non-issue.

        The "E" in "Javascript" stands for "Easy"

        S 1 Reply Last reply Reply Quote 0
        • S Offline
          sdetweil @BKeyport
          last edited by sdetweil

          @BKeyport setInterval creates a repeating timer with ONE elapsed time

          6:30pm to midnight the next day is 24+5.5

          So every 29.5 hours it will fire.
          1st correct for tomorrow night,
          it will miss tonight’s midnight

          And then be off from them on, shifting 5.5 hours later each time

          24 hours of seconds is 86400

          If you think of it as morning, then it will fire every 5.5 hours. Still not 24

          After the first one, it should be calculating so close to midnight, it’s an non-issuee

          This part is incorrect. setInterval only looks at the value ONE time.

          Sorry, computer is stupid. It has one time event setTimeout, or interval setInterval. Both use the elapsed time only once, at creation

          Sam

          How to add modules

          learning how to use browser developers window for css changes

          BKeyportB 1 Reply Last reply Reply Quote 0
          • BKeyportB Online
            BKeyport Module Developer @sdetweil
            last edited by

            @sdetweil The problem is that it’s not firing at all - it was working as is… Now it’s not firing, first, last, middle, or any other time.

            The "E" in "Javascript" stands for "Easy"

            1 Reply Last reply Reply Quote 0
            • S Offline
              sdetweil @BKeyport
              last edited by

              @BKeyport make your own routine to be called

              this.update2()

              Prove it fires

              Sam

              How to add modules

              learning how to use browser developers window for css changes

              BKeyportB 1 Reply Last reply Reply Quote 0
              • BKeyportB Online
                BKeyport Module Developer @sdetweil
                last edited by BKeyport

                @sdetweil It’s people like you that drive people out of coding. This isn’t fun anymore.

                Is there anyone other than Sam that could explain why this don’t work anymore?

                The "E" in "Javascript" stands for "Easy"

                S 1 Reply Last reply Reply Quote 0
                • S Offline
                  sdetweil @BKeyport
                  last edited by

                  @BKeyport that’s very disappointing to hear.

                  I thought I was being helpful in giving ways to debug the problem.

                  I do not KNOW why it appears to be failing. None of the changes to the mm core appear to affect updateDom

                  Sam

                  How to add modules

                  learning how to use browser developers window for css changes

                  1 Reply Last reply Reply Quote 0
                  • M Offline
                    MMRIZE
                    last edited by MMRIZE

                    How about this? not tested.

                    start: function () {
                      this.dailyTimer = null
                      const moment = new Date()
                      const midnight = new Date(moment.getFullYear(), moment.getMonth(), moment.getDate() + 1, 0, 0, 0, 0)
                      const toMidnight = midnight - moment
                      setTimeout(() => {
                        this.doSomething()
                      }, toMidnight)
                    },
                    
                    
                    doSomething: function () {
                      clearTimeout(this.dailyTimer)
                      this.dailyTimer = null
                      // Do your job here.
                      this.dailyTimer = setTimeout(() => {
                        this.doSomething()
                      }, 24 * 60 * 60 * 1000)
                    }
                    
                    S 2 Replies Last reply Reply Quote 0
                    • S Offline
                      sdetweil @MMRIZE
                      last edited by sdetweil

                      @MMRIZE right, thats a way without interval timer

                      I added this to compliments with the cron entries to get the interval synched to the minute

                       let minute_sync_delay = 1;
                      		// loop thru all the configured when events
                      		for (let m of Object.keys(this.config.compliments)) {
                      			// if it is a cron entry
                      			if (this.isCronEntry(m)) {
                      				// we need to synch our interval cycle to the minute
                      				minute_sync_delay = (60 - (moment().second())) * 1000;
                      				break;
                      			}
                      		}
                      		// Schedule update timer. sync to the minute start (if needed), so minute based events happen on the minute start
                      		setTimeout(() => {
                      			setInterval(() => {
                      				this.updateDom(this.config.fadeSpeed);
                      			}, this.config.updateInterval);
                      		},
                      		minute_sync_delay);
                      

                      Sam

                      How to add modules

                      learning how to use browser developers window for css changes

                      1 Reply Last reply Reply Quote 0
                      • S Offline
                        sdetweil @MMRIZE
                        last edited by

                        @MMRIZE one thing

                        You can’t clear the timer if it has triggered, as the timer has expired. The handle is no longer valid

                        Sam

                        How to add modules

                        learning how to use browser developers window for css changes

                        BKeyportB 1 Reply Last reply Reply Quote 0
                        • BKeyportB Online
                          BKeyport Module Developer @sdetweil
                          last edited by

                          @MMRIZE @sdetweil

                          the reporter posted this to my github:

                          I am not a javascript expert, but have some experience in C++ and
                          microcontroller programming.
                          I have had some problems with other MagicMirror calendar modules not
                          updating the day properly and they also seem to implement workarounds and
                          have issues with the DOM updating.
                          I did a little looking into this and it seems there must be some generic
                          issue with the MagicMirror software allowing modules to implement timers
                          and date/time functions in realtime.
                          

                          And Sam - I was telling you that it previously was working and works outside of the MM ecosystem - or at least sets it up to work. that’s what got me pissed off - I had done a slew of debugging and it got down to “have you turned off and on again”

                          There’s lots of issues I’ve been having - most of them being internal MM functions, that I tend to work around by native JS code and using remote access via the Web server.

                          For example: Logging. Don’t seem to work for me. Gotta log via console, so I pull up the web server and Edge’s dev tools.

                          The "E" in "Javascript" stands for "Easy"

                          S 2 Replies Last reply Reply Quote 0
                          • S Offline
                            sdetweil @BKeyport
                            last edited by

                            @BKeyport logging

                            There are two logs

                            Node_helper goes to npm start console
                            Modulename.js goes to the browser log

                            There are a couple modules that will send the browser log records to the console
                            MMM-Logging, mmrize has an updated version, so do I.

                            I don’t know why it worked before, or why it’s not working now.

                            We added some checking for valid position names, instead of crashing

                            We added async to some mm functions, but
                            Timeout and interval are js functions.

                            Only way to debug the browser side is to use the developer window debugger (sources tab)

                            Sam

                            How to add modules

                            learning how to use browser developers window for css changes

                            1 Reply Last reply Reply Quote 0
                            • S Offline
                              sdetweil @BKeyport
                              last edited by

                              @BKeyport I’d be interested in the functions that you are having trouble with, as there are very few.

                              Sam

                              How to add modules

                              learning how to use browser developers window for css changes

                              BKeyportB 1 Reply Last reply Reply Quote 0
                              • BKeyportB Online
                                BKeyport Module Developer @sdetweil
                                last edited by

                                I think I got it figured out - Based on research elsewhere - if I’m correct: start: function() is only called upon load, correct?

                                ASSUMING that’s the case, this seems to work better:

                                start: function () {
                                    function scheduleMidnightUpdate() {
                                        const now = new Date();
                                        const nextMidnight = new Date(now);
                                
                                        // Set the time to midnight
                                        nextMidnight.setHours(24, 0, 0, 0);
                                
                                        // Calculate the time remaining until the next midnight
                                        const timeUntilMidnight = nextMidnight - now;
                                
                                        // Schedule the updateDom method to be called at midnight
                                        setTimeout(() => {
                                            this.updateDom();
                                
                                            // Reschedule the update for the next midnight
                                            scheduleMidnightUpdate.call(this);
                                        }, timeUntilMidnight);
                                    }
                                
                                    // Call the function to start the first schedule
                                    scheduleMidnightUpdate.call(this);
                                },
                                

                                Now, This seems to work on testing - but it will fail in testing if I change the clock after starting MM.

                                Is there anyway y’all can see to improve this, or am I good?

                                The "E" in "Javascript" stands for "Easy"

                                M 1 Reply Last reply Reply Quote 0
                                • M Offline
                                  MMRIZE @BKeyport
                                  last edited by

                                  @BKeyport
                                  Your DOM might not be rendered yet on “start” imo. I think manipulating DOM after “DOM_OBJECT_CREATED” notification would be safer.

                                  S 1 Reply Last reply Reply Quote 1
                                  • S Offline
                                    sdetweil @MMRIZE
                                    last edited by

                                    @MMRIZE

                                    Yes the documented flow is, as I recall on my phone,

                                    Init
                                    Getstyles
                                    Getscripts
                                    GetTemplates
                                    Start
                                    Notification received
                                    AllModulesStarted
                                    DOMobjectsCreated
                                    getDOM

                                    Sam

                                    How to add modules

                                    learning how to use browser developers window for css changes

                                    BKeyportB 1 Reply Last reply Reply Quote 0
                                    • BKeyportB Online
                                      BKeyport Module Developer @sdetweil
                                      last edited by

                                      @sdetweil @MMRIZE so, should I move the initial call somewhere else?

                                      The "E" in "Javascript" stands for "Easy"

                                      S 1 Reply Last reply Reply Quote 0
                                      • S Offline
                                        sdetweil @BKeyport
                                        last edited by

                                        @BKeyport after the notification

                                        From the doc

                                        DOM_OBJECTS_CREATED - All dom objects are created. The system is now ready to perform visual changes.

                                        Sam

                                        How to add modules

                                        learning how to use browser developers window for css changes

                                        BKeyportB 1 Reply Last reply Reply Quote 0
                                        • BKeyportB Online
                                          BKeyport Module Developer @sdetweil
                                          last edited by BKeyport

                                          @sdetweil said in Not updating at midnight...:

                                          DOM_OBJECTS_CREATED - All dom objects are created. The system is now ready to perform visual changes

                                          start: function () {
                                              function scheduleMidnightUpdate() {
                                                  const now = new Date();
                                                  const nextMidnight = new Date(now);
                                          
                                                  // Set the time to midnight
                                                  nextMidnight.setHours(24, 0, 0, 0);
                                          
                                                  // Calculate the time remaining until the next midnight
                                                  const timeUntilMidnight = nextMidnight - now;
                                          
                                                  // Schedule the updateDom method to be called at midnight
                                                  setTimeout(() => {
                                                      this.updateDom();
                                          
                                                      // Reschedule the update for the next midnight
                                                      scheduleMidnightUpdate.call(this);
                                                  }, timeUntilMidnight);
                                              }
                                          },
                                          
                                              notificationReceived: function (notification, payload, sender) {
                                                  if (notification === 'CALENDAR_EVENTS') {
                                                      this.storedEvents = JSON.parse(JSON.stringify(payload))
                                                      this.updateDom();
                                                  } else if (notification === 'DOM_OBJECTS_CREATED') {
                                                      // Call the function to start the first schedule
                                                        scheduleMidnightUpdate.call(this);  
                                                  }
                                          

                                          This is the only source I see in the docs of that - a notification. Is that correct?

                                          Thanks for the patience after the double misunderstanding.

                                          The "E" in "Javascript" stands for "Easy"

                                          S 2 Replies Last reply Reply Quote 0
                                          • S Offline
                                            sdetweil @BKeyport
                                            last edited by

                                            @BKeyport

                                            https://docs.magicmirror.builders/development/core-module-file.html

                                            IMG_0044.png

                                            you could have done all the initial event calculating in one function

                                            used interval for the midnights (as it’s a fixed number)

                                            Sam

                                            How to add modules

                                            learning how to use browser developers window for css changes

                                            S 1 Reply Last reply Reply Quote 0

                                            Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                                            Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                                            With your input, this post could be even better 💗

                                            Register Login
                                            • 1
                                            • 2
                                            • 1 / 2
                                            • First post
                                              Last post
                                            Enjoying MagicMirror? Please consider a donation!
                                            MagicMirror created by Michael Teeuw.
                                            Forum managed by Sam, technical setup by Karsten.
                                            This forum is using NodeBB as its core | Contributors
                                            Contact | Privacy Policy