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.

    Auto-disable module

    Scheduled Pinned Locked Moved Development
    12 Posts 4 Posters 8.2k Views 2 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.
    • R Offline
      ronny3050 Module Developer
      last edited by ronny3050

      First send a notification from your main module.js to node_helper.js to ask it to look for the time of day.
      Something like in your main module.js:

      start: function(){
              this.sendSocketNotification('LookForTime');
      }
      

      In your node_helper.js, you could check for the time. Once you see that it is night, you can send a socket notification to your main module.js file like:

      socketNotificationReceived: function(notification, payload){
               if(notification === 'LookForTime'){
                        // Keep checking for time. Once night,
                       if(night){
                                  this.sendSocketNotification('NIGHT');
                       }
             }
      }
      

      In your main module.js:

      socketNotificationReceived: function(notification, payload){
            if(notification === 'NIGHT'){
                  this.hide();
            }
      }
      
      yo-lessY 1 Reply Last reply Reply Quote 0
      • yo-lessY Offline
        yo-less Module Developer @ronny3050
        last edited by

        @ronny3050 Impressive response, thank you!

        R 1 Reply Last reply Reply Quote 1
        • R Offline
          ronny3050 Module Developer @yo-less
          last edited by

          @yo-less No problem! If you have any issues, I’ll be glad to answer them on chat :)

          1 Reply Last reply Reply Quote 0
          • KirAsh4K Offline
            KirAsh4 Moderator
            last edited by

            No need to use node helper for that. All modules can use 'moment.js' as it’s already in the code (for the time display, calendar, and various timers.) So write your code in the main module’s js file.

            moment.now() gives you the current time.

            Take a gander at my calendar_monthly module where I keep track of time for the refreshes. The calendar DOM only refreshes once a day, at midnight. However, because browsers tend to get “stuck” every so often during the day, I also keep track of the hours. What that means is, there is a “heartbeat” every hour to stay accurate to the actual time. If the browser got stuck and the clock delayed by a few seconds, the heartbeat will fix that every 60 minutes. Then, only when it reaches midnight, will it actually reload the DOM.

            In the code, look at the 'start' function where I set the time till next midnight. Then I fire off a scheduleUpdate() which sets a delay to the next hour. When that timer expires, a couple of things happen:

            1. it checks whether the current time is past the midnight set earlier. If so,
            • it will reload the DOM (which refreshes the calendar display),
            • it will calculate the next midnight
            • it will reset the timer to the next hour
            • it will loop back to scheduleUpdate()
              OR
            1. if the current time is LESS than midnight,
            • it will reset the timer to the next hour
            • it will loop back to scheduleUpdate()
              … and the DOM never gets touched.

            This way I don’t reload the calendar unnecessarily throughout the day. It only happens once, but the timer fires off every hour, just to keep track of time. If I didn’t do that, at least on my rpi, I noticed the browser’s time will drift by several minutes in a 24 hour period.

            A Life? Cool! Where can I download one of those from?

            R 1 Reply Last reply Reply Quote 0
            • R Offline
              ronny3050 Module Developer @KirAsh4
              last edited by

              @KirAsh4’s solution is much neater and elegant. He never ceases to amaze me! :D

              1 Reply Last reply Reply Quote 0
              • yo-lessY Offline
                yo-less Module Developer
                last edited by

                @KirAsh4 - It’s amazing for me to see how helpful and how full of good advice people on this forum are. Thank your for that!

                I’ve got my module up and running now and will go through troubleshooting it. As soon as I feel that it’s working as it should, I will try to implement the feature to automatically disable it if necessary, I will study your code then as well. Thanks for pointing me in a good direction!

                1 Reply Last reply Reply Quote 0
                • MichMichM Offline
                  MichMich Admin @yo-less
                  last edited by

                  This post is deleted!
                  1 Reply Last reply Reply Quote 0
                  • 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