Time delay (not widget) with Node.js and Raspberry Pi

Yes this is another timer/delay question, and another garage door opener…

However, all of the timer, delays and simple timer posts I’ve read have been for microcontrollers using sketches and C++. This project is using a Raspberry Pi and Node.js.

The project uses a magnetic switch for determining the status of the garage door (open/closed). The mobile device app has a labled value indicating the status of the door. The project also uses a 5v relay to power the garage door opener, and the mobile device app has a button to power the relay.

What I’m attempting to do: Have Blynk immediately notify the mobile device by push notification and email when the door is opened. Furthermore, I am attemting to have Blynk notify the moble device when the door has been open for 10 minutes, as a reminder.

The code below works and runs well except for notifying if the door has been open for 10 mins.

var Blynk = require('blynk-library'); 
var Gpio = require('onoff').Gpio;
var reed = new Gpio(17, 'in', 'both');
var relay = new Gpio(18, 'out');
var MinutesToWait=10;


var AUTH = 'AUTH TOKEN'; 

var blynk = new Blynk.Blynk(AUTH);

var v0 = new blynk.VirtualPin(0); 

v0.on('write', function(param) {

 if (param[0] == '1') { 
  relay.writeSync(1);
 } else {
  relay.writeSync(0);
 }

 console.log('V0:', param[0]);

});

var virtualPin = 1;
reed.watch(function(err,value){

if(value==0){
blynk.virtualWrite(virtualPin,"Closed");
console.log('Door Closed');
};

if(value==1){
blynk.virtualWrite(virtualPin,"Open");
console.log('Door Open');
blynk.notify("The door just opened!");
blynk.email("email@email.com", "Door Open", "Door Open");
//Everything above here works like a charm. Below does not. Conditions don't trigger
startCountingIdle();
};

//timing block
var timeEl = 0;
var timeOut = null;
function startCountingIdle(){
  if(timeOut !== null){
    clearTimeout(timeOut);
    timeOut = null;
  }else{
    time();
  }
}

function time()
{
  timeOut = setTimeout(function()
  {
    timeEl = timeEl + 1;

    if(timeEl==MinutesToWait*60 && value==1){
         	blynk.virtualWrite(virtualPin,"Open");
			console.log('Door Open for 10 min');
			blynk.notify("The door has been open for 10 min");
			blynk.email("email@email.com", "Door Open for 10 min", "Door Open for 10 min");
			timeEl=0;
    }
    else{
    	time();
    }
    
    
  }, 1000);
}


});

I’ve tested the above code in a node js ide and forced value ==1, and the second notification fires off after 10 min. I believe the hardware if functioning properly, because when opening the door(moving the magnet) the first notifications are sent, and the mobile device app correctly indicates door is open.

Any help with getting the second, time dependent, notifications to fire off is greatly appreciated. Thank you.

I suspect your solution will be a matter of finding a suitable timer library (or whatever the RPi equivalent is) as I don’t think SimpleTimer/BlynkTimer is supported in JavaScript??

I have this line in my test script that counts seconds, perhaps you can experiment with other commands like new Date().getMinutes() to see if you can use that for a countdown?

var v9 = new blynk.VirtualPin(9);  // Setup Display Widget on V9 with variable 'v9'

v9.on('read', function() {
  v9.write(new Date().getSeconds());  // This sends the seconds 0-59 to the Display Widget on V9 - Set to 1sec reading rate.
});

I also did a quick Google search and dug up a few options…

https://albert-gonzalez.github.io/easytimer.js

https://husa.github.io/timer.js/

I just did a few other tests with the new Date().getMinutes() and new Date().getHours() commands, and it turns out they are reporting the time of day… so using that you should be able to get some sort of Flag set and a clock check/comparison, to count down 10 minutes, and if no Flag reset, send a notification.