• Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
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.

MMM-PIR - off delay

Scheduled Pinned Locked Moved Development
11 Posts 5 Posters 8.1k Views 6 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.
  • J Offline
    JamesMM
    last edited by Oct 22, 2016, 8:30 AM

    I’ve been using the PIR sensor module and if I want to change my PIR on delay in hardware its a pain to take a really neat installation down. Instead I wanted to set the delay to the minimum and then have a configurable offDelay of say 30 seconds.

    I added offDelay:30000 to the config and instead of calling deactivateMonitor I used timerID = setInterval(deactivateMonitor,self.config.offDelay). If detecting on again before this runs I called clearInterval(); It works but isn’t quite right because if the PIR becomes 0 and then 1 again before the timer is activated the monitor goes off and on again.

    I’m not really understanding the node_helper.js script and wondered if someone could point me in the right direction.

    1. the socketreceivednotification gets called on startup with config set, this sets up the pirwatcher. - but does the pir watcher block or when its state changes it simply calls itself again?
    2. im watching .pm2/log/ errors and output and only really see when something is catastrophically wrong with my code. Is there a more convenient way to pipe something out from the node_helper?
    3. I am persisting state information by adding it to the config because I thought that gets passed back in the “main loop”. I looked at some other modules for a hint but couldn’t really find the answer.
    4. the activate or deactivate functions cannot see config if they are called async so I guess I’ve fallen into the typical trap of not really getting the self. this. thing in this context.

    So I’m obviously missing something bit fundamental, I write C normally and frequently misinterpret inline JS functions. Its also hard to concentrate on this in the evening with a 2 year old swinging on my head.

    S 1 Reply Last reply Oct 22, 2016, 8:45 AM Reply Quote 0
    • S Offline
      strawberry 3.141 Project Sponsor Module Developer @JamesMM
      last edited by strawberry 3.141 Oct 22, 2016, 8:50 AM Oct 22, 2016, 8:45 AM

      @JamesMM

      1. If the pir watcher detects a change it will run the specified callback from line 55. https://github.com/paviro/MMM-PIR-Sensor/blob/master/node_helper.js#L55

      2. If you want to debug the node_helper put console.log(“message”) at the position you want and you will see the message in the terminal.

      3. If you change the config in the node_helper, you have to send the changes also to the main module by sending a socketnotification, as you did from the main loop to the node_helper.

      4. If you define a callback with function(){…} you switch the scope, so the reference of this is the callback and not longer the node_helper itself, that’s why the author saved this into the variable self upfront. I would personally define a callback as big arrow function () => {…}, the advantage of this is you will stay in the same scope and wouldn’t need a redundant variable.

      And I assume you want to use a timeout instead of an interval.

      this.pir.watch(function(err, value) {
              if (value == 1) {
                clearTimeout(self.timer);
                self.sendSocketNotification("USER_PRESENCE", true);
                if (self.config.powerSaving){
                  self.activateMonitor();
                }
               }
              else if (value == 0) {
                self.timer = setTimeout(function(){
                    self.sendSocketNotification("USER_PRESENCE", false);
                    if (self.config.powerSaving){
                      self.deactivateMonitor();
                    }
                }, self.config.offDelay);
              }
      });
      

      Please create a github issue if you need help, so I can keep track

      1 Reply Last reply Reply Quote 1
      • J Offline
        JamesMM
        last edited by Oct 22, 2016, 9:51 AM

        Thanks for your pointers.

        So clearTimeout will throw an error because self.timer is not defined so I defined it as null at the top, and didn’t bother checking if it was null or not before clearing it.

        Once this was done we were in the same boat as before with a pir detection of 1 before the timeout has fired resulting in the mirror turning off and on again which is annoying. This is fixed by adding an onstate variable and simply not executing the activatemirror if its already activated. I just moved cleartimeout outside of that check otherwise it won’t work.

        My working code below since I find it annoying when people don’t come back when they solve a problem:

        // Subclass socketNotificationReceived received.
        socketNotificationReceived: function(notification, payload) {
        if (notification === ‘CONFIG’ && this.started == false) {
        const self = this;
        this.config = payload;
        self.timer = null;
        self.onState = 0;

          //Log.info('PIR: ' + this.name);
        
          //Setup pins
          this.pir = new Gpio(this.config.sensorPIN, 'in', 'both');
        
          if (this.config.relayPIN) {
            this.relay = new Gpio(this.config.relayPIN, 'out');
            this.relay.writeSync(this.config.relayOnState);
            exec("/opt/vc/bin/tvservice --preferred && sudo chvt 6 && sudo chvt 7", null);
          }
        
          //Detected movement
          this.pir.watch(function(err, value) {
            if (value == 1) {
             clearTimeout(self.timer);
            if(self.onState != 1){
              self.sendSocketNotification("USER_PRESENCE", true);
              if (self.config.powerSaving){
                self.activateMonitor();
                self.onState = 1;
              }
              }
             }
            else if (value == 0) {
              if(self.onState != 0){
               self.timer = setTimeout(function(){
                  self.sendSocketNotification("USER_PRESENCE", false);
                  if (self.config.powerSaving){
                    self.deactivateMonitor();
                    self.onState = 0;
                  }
              }, self.config.offDelay);
              }
            }
        

        });

          this.started = true;
        
        } else if (notification === 'SCREEN_WAKEUP') {
          this.activateMonitor();
        }
        

        }

        });

        1 Reply Last reply Reply Quote 2
        • J Offline
          JamesMM
          last edited by Oct 22, 2016, 10:10 AM

          Sorry I didn’t quote the code properly.

          B 1 Reply Last reply Oct 29, 2016, 11:07 PM Reply Quote 0
          • B Offline
            bibi @JamesMM
            last edited by Oct 29, 2016, 11:07 PM

            @JamesMM hi! i also have pain to setup the PIR in hardware way and i would prefer your way. Is the code stated above working? thx

            J 1 Reply Last reply Oct 31, 2016, 9:59 PM Reply Quote 0
            • L Offline
              lsfourwheeler
              last edited by Oct 31, 2016, 8:50 PM

              @JamesMM Where is the “Config” section where you added the delay configuration? I’ve changed my node_helper.js file but I can’t find the configuration file you referenced.

              L 1 Reply Last reply Oct 31, 2016, 9:12 PM Reply Quote 0
              • L Offline
                lsfourwheeler @lsfourwheeler
                last edited by Oct 31, 2016, 9:12 PM

                @lsfourwheeler @JamesMM Actually, I see that you are referencing the Magic Mirror config for the delay setting. So where are you replacing the deactivateMonitor with timerID?

                J 1 Reply Last reply Oct 31, 2016, 10:02 PM Reply Quote 0
                • J Offline
                  JamesMM @bibi
                  last edited by Oct 31, 2016, 9:59 PM

                  @bibi it does. Perfectly.

                  B 1 Reply Last reply Oct 31, 2016, 10:10 PM Reply Quote 0
                  • J Offline
                    JamesMM @lsfourwheeler
                    last edited by Oct 31, 2016, 10:02 PM

                    @lsfourwheeler yes passed it in. Assumed that was how it was supposed to be done.

                    1 Reply Last reply Reply Quote 0
                    • B Offline
                      bibi @JamesMM
                      last edited by Oct 31, 2016, 10:10 PM

                      @JamesMM ok thx! i don’t really get where exactly your code above should be placed :( can you give me a hint pls?

                      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