Haiku Script Execution question

scantrel

Member
I'm trying to run a script in HaikuHelper that will email me and my wife whenever our doors are opened during our work hours. I entered the following script below, clicked the "Run Script" button and saw the following in the log: 
 
2015-06-16 18:01:57 Our House: Initializing scripting environment…
2015-06-16 18:01:57 Our House: Applying controller script…
 
but when the doors open between the appointed times (8am to 5pm), nothing happens. Tried restarting HaikuHelper and I see the same lines about applying the script when the program starts, but it doesn't seem to be working. So, I'm guessing this is a scripting error! Any ideas are greatly appreciated.
 

function onZoneNotReady(zone)
{
     if (zone.number == 1)
     { 
          var date = new Date();
          if(date.getHours() >=17 && date.getHours() <=8)
               {
                    helper.sendMail (controller, '[email protected]','Front Door Notice','Front Door was opened.');
                    helper.sendMail (controller, '[email protected]','Front Door Notice','Front Door was opened.');
               }
     }
 
     if (zone.number == 2)
     { 
          var date = new Date();
          if(date.getHours() >=17 && date.getHours() <=8)
               {
                    helper.sendMail (controller, '[email protected]','Garage Door Notice','Door to Garage was opened.');
                    helper.sendMail (controller, '[email protected]','Garage Door Notice','Door to Garage was opened.');
               }
     }
 
      if (zone.number == 3)
     { 
          var date = new Date();
          if(date.getHours() >=17 && date.getHours() <=8)
               {
                    helper.sendMail (controller, '[email protected]','Back Door Notice','Back Door was opened.');
                    helper.sendMail (controller, '[email protected]','Back Door Notice','Back Door was opened.');
               }
     }
 
}

 
 
Your getHours comparisons should use OR (||) not AND (&&) as the value can't be true for both comparisons.
 
Actually, I'm thinking this through again, and not sure why the AND condition wouldn't be correct.
 
If the event takes place at say, 1PM (13:00), isn't that less than 17 and greater than 8? So that should resolve as true.
 
Alternatively, let's take 6AM as an example...that's less than 17 so that condition would be true, but it's not greater than 8 so the OR comparison would still be true and would generate the message. 
 
No problem :) You can also cut down the repeated code a lot by refactoring a bit:
Code:
function onZoneNotReady(zone) {
     var date = new Date();
     var hour = date.getHours();

     switch(zone.number) {
          case 1: case 2: case 3:
               if(hour >= 8 && hour <= 17) {
                    var recipients = ['[email protected]', '[email protected]'];
                    var subject = '{0} Notice'.format(zone.bestDescription);
                    var body = '{0} was opened.'.format(zone.bestDescription);
                    sendEmailToMultipleRecipients(recipients, subject, body);
               }
               break
     }
}

function sendEmailToMultipleRecipients(recipients, subject, body) {
     for(i = 0; i < recipients.length; i++) {
          var recipient = recipients[i];
          helper.sendMail(controller, recipient, subject, body);
     }
}
You can set the description for the zones if you want to use more custom/verbose strings using the Object Editor in HaikuHelper.

That format() function can take multiple values, ie. '{0} status is {1}'.format(zone.bestDescription, zone.statusDescription) will work too.
 
Back
Top