<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[MMM-PIR - off delay]]></title><description><![CDATA[<p dir="auto">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.</p>
<p dir="auto">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.</p>
<p dir="auto">I’m not really understanding the node_helper.js script and wondered if someone could point me in the right direction.</p>
<ol>
<li>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?</li>
<li>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?</li>
<li>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.</li>
<li>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.</li>
</ol>
<p dir="auto">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.</p>
]]></description><link>https://forum.magicmirror.builders/topic/848/mmm-pir-off-delay</link><generator>RSS for Node</generator><lastBuildDate>Sat, 18 Jul 2026 02:42:01 GMT</lastBuildDate><atom:link href="https://forum.magicmirror.builders/topic/848.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 22 Oct 2016 08:30:33 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to MMM-PIR - off delay on Tue, 28 Feb 2017 07:27:19 GMT]]></title><description><![CDATA[<p dir="auto">Thank you for sharing <a class="plugin-mentions-user plugin-mentions-a" href="/user/jamesmm" aria-label="Profile: JamesMM">@<bdi>JamesMM</bdi></a> - it works perfectly!</p>
]]></description><link>https://forum.magicmirror.builders/post/16057</link><guid isPermaLink="true">https://forum.magicmirror.builders/post/16057</guid><dc:creator><![CDATA[troelsbugge]]></dc:creator><pubDate>Tue, 28 Feb 2017 07:27:19 GMT</pubDate></item><item><title><![CDATA[Reply to MMM-PIR - off delay on Mon, 31 Oct 2016 22:10:56 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jamesmm" aria-label="Profile: JamesMM">@<bdi>JamesMM</bdi></a> ok thx! i don’t really get where exactly your code above should be placed :( can you give me a hint pls?</p>
]]></description><link>https://forum.magicmirror.builders/post/7414</link><guid isPermaLink="true">https://forum.magicmirror.builders/post/7414</guid><dc:creator><![CDATA[bibi]]></dc:creator><pubDate>Mon, 31 Oct 2016 22:10:56 GMT</pubDate></item><item><title><![CDATA[Reply to MMM-PIR - off delay on Mon, 31 Oct 2016 22:02:02 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/lsfourwheeler" aria-label="Profile: lsfourwheeler">@<bdi>lsfourwheeler</bdi></a> yes passed it in. Assumed that was how it was supposed to be done.</p>
]]></description><link>https://forum.magicmirror.builders/post/7413</link><guid isPermaLink="true">https://forum.magicmirror.builders/post/7413</guid><dc:creator><![CDATA[JamesMM]]></dc:creator><pubDate>Mon, 31 Oct 2016 22:02:02 GMT</pubDate></item><item><title><![CDATA[Reply to MMM-PIR - off delay on Mon, 31 Oct 2016 21:59:28 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/bibi" aria-label="Profile: bibi">@<bdi>bibi</bdi></a> it does. Perfectly.</p>
]]></description><link>https://forum.magicmirror.builders/post/7412</link><guid isPermaLink="true">https://forum.magicmirror.builders/post/7412</guid><dc:creator><![CDATA[JamesMM]]></dc:creator><pubDate>Mon, 31 Oct 2016 21:59:28 GMT</pubDate></item><item><title><![CDATA[Reply to MMM-PIR - off delay on Mon, 31 Oct 2016 21:12:10 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/lsfourwheeler" aria-label="Profile: lsfourwheeler">@<bdi>lsfourwheeler</bdi></a> <a class="plugin-mentions-user plugin-mentions-a" href="/user/jamesmm" aria-label="Profile: JamesMM">@<bdi>JamesMM</bdi></a>  Actually, I see that you are referencing the Magic Mirror config for the delay setting. So where are you replacing the deactivateMonitor with timerID?</p>
]]></description><link>https://forum.magicmirror.builders/post/7406</link><guid isPermaLink="true">https://forum.magicmirror.builders/post/7406</guid><dc:creator><![CDATA[lsfourwheeler]]></dc:creator><pubDate>Mon, 31 Oct 2016 21:12:10 GMT</pubDate></item><item><title><![CDATA[Reply to MMM-PIR - off delay on Mon, 31 Oct 2016 20:50:38 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jamesmm" aria-label="Profile: JamesMM">@<bdi>JamesMM</bdi></a>  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.</p>
]]></description><link>https://forum.magicmirror.builders/post/7405</link><guid isPermaLink="true">https://forum.magicmirror.builders/post/7405</guid><dc:creator><![CDATA[lsfourwheeler]]></dc:creator><pubDate>Mon, 31 Oct 2016 20:50:38 GMT</pubDate></item><item><title><![CDATA[Reply to MMM-PIR - off delay on Sat, 29 Oct 2016 23:07:31 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jamesmm" aria-label="Profile: JamesMM">@<bdi>JamesMM</bdi></a> 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</p>
]]></description><link>https://forum.magicmirror.builders/post/7301</link><guid isPermaLink="true">https://forum.magicmirror.builders/post/7301</guid><dc:creator><![CDATA[bibi]]></dc:creator><pubDate>Sat, 29 Oct 2016 23:07:31 GMT</pubDate></item><item><title><![CDATA[Reply to MMM-PIR - off delay on Sat, 22 Oct 2016 10:10:57 GMT]]></title><description><![CDATA[<p dir="auto">Sorry I didn’t quote the code properly.</p>
]]></description><link>https://forum.magicmirror.builders/post/6697</link><guid isPermaLink="true">https://forum.magicmirror.builders/post/6697</guid><dc:creator><![CDATA[JamesMM]]></dc:creator><pubDate>Sat, 22 Oct 2016 10:10:57 GMT</pubDate></item><item><title><![CDATA[Reply to MMM-PIR - off delay on Sat, 22 Oct 2016 09:51:29 GMT]]></title><description><![CDATA[<p dir="auto">Thanks for your pointers.</p>
<p dir="auto">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.</p>
<p dir="auto">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.</p>
<p dir="auto">My working code below since I find it annoying when people don’t come back when they solve a problem:</p>
<p dir="auto">// Subclass socketNotificationReceived received.<br />
socketNotificationReceived: function(notification, payload) {<br />
if (notification === ‘CONFIG’ &amp;&amp; this.started == false) {<br />
const self = this;<br />
this.config = payload;<br />
self.timer = null;<br />
self.onState = 0;</p>
<pre><code>  //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 &amp;&amp; sudo chvt 6 &amp;&amp; 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);
      }
    }
</code></pre>
<p dir="auto">});</p>
<pre><code>  this.started = true;

} else if (notification === 'SCREEN_WAKEUP') {
  this.activateMonitor();
}
</code></pre>
<p dir="auto">}</p>
<p dir="auto">});</p>
]]></description><link>https://forum.magicmirror.builders/post/6695</link><guid isPermaLink="true">https://forum.magicmirror.builders/post/6695</guid><dc:creator><![CDATA[JamesMM]]></dc:creator><pubDate>Sat, 22 Oct 2016 09:51:29 GMT</pubDate></item><item><title><![CDATA[Reply to MMM-PIR - off delay on Sat, 22 Oct 2016 08:50:09 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jamesmm" aria-label="Profile: JamesMM">@<bdi>JamesMM</bdi></a></p>
<ol>
<li>
<p dir="auto">If the pir watcher detects a change it will run the specified callback from line 55. <a href="https://github.com/paviro/MMM-PIR-Sensor/blob/master/node_helper.js#L55" target="_blank" rel="noopener noreferrer nofollow ugc">https://github.com/paviro/MMM-PIR-Sensor/blob/master/node_helper.js#L55</a></p>
</li>
<li>
<p dir="auto">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.</p>
</li>
<li>
<p dir="auto">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.</p>
</li>
<li>
<p dir="auto">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 () =&gt; {…}, the advantage of this is you will stay in the same scope and wouldn’t need a redundant variable.</p>
</li>
</ol>
<p dir="auto">And I assume you want to use a timeout instead of an interval.</p>
<pre><code>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);
        }
});
</code></pre>
]]></description><link>https://forum.magicmirror.builders/post/6691</link><guid isPermaLink="true">https://forum.magicmirror.builders/post/6691</guid><dc:creator><![CDATA[strawberry 3.141]]></dc:creator><pubDate>Sat, 22 Oct 2016 08:50:09 GMT</pubDate></item></channel></rss>