Haiku Function within a Function / Nested Functions

Hey all, say I want to use a function within a function, the help docs don't seem to cover this, I have been scratching my head trying to figure it out. I want to take

Code:
function onZoneNotReady(zone) {
if(zone.number == 4) {// Match zone by number
  helper.sendNotification(controller, zone.bestDescription + ' Door Open / Not Ready!');
  }
}

but I only wanted those to show after a certain time, say 9pm. I would need to determine the current time

Code:
function onSyncStart(forced, timestamp) {
	    var date = new Date(timestamp);
	    var sunsetDate = new Date(controller.sunsetDate);
	   
	    if(date.getHours() >= 9 && date.getMinutes() >= 00) {
and
function onZoneNotReady(zone) {
if(zone.number == 4) {// Match zone by number
  helper.sendNotification(controller, zone.bestDescription + ' Door Open / Not Ready!');
  }
}

Think of it as a "forgot to close the Garage Door reminder." (And its after dark, and you live in the wrong part of the city, better go make sure your lawn mower is still there.... you get the idea) :)

So, are nested functions allowed, because I seem to be hitting a wall with this one?
 
It looks like you're going at this a little incorrectly. The onZoneNotReady() function is actually a callback that gets called by HaikuHelper, there can only be one such function and it has to be defined in the "root" scope to get called. Since your actual trigger for this event is the zone going not ready, there is no need to use the onSyncStart trigger which is meant to just be used as a shortcut for scheduling (or executing any code on sync start).

The correct way to do this would be to define the onZoneNotReady() callback function in the root scope and then do the zone number and date check there, sending the notification if all conditions are met.

PS. You may want to use the controller.isDark boolean property, that will tell you if its dark outside easily.
PPS. You may want to define a Message on the controller itself and add the logic there, this way you would also get a notification within the house, on the keypads & touch screens. HaikuHelper will automatically forward the message to your iOS devices provided its not sent by the controller "Without a Beep".

Here's one basic way of doing it, but don't be afraid to tweak it to your liking, ie. you can create your own functions that get called within the callbacks so that your code is more organized and easier to understand, etc.

Code:
function onZoneNotReady(zone) {
    if(zone.number == 4) {// Match zone by number
        var date = new Date(controller.date);

        // Between 9 PM and 7 AM (note 24 hr time)
        if(date.getHours() >= 21 && date.getHours() <= 7) helper.sendNotification(controller, zone.bestDescription + ' Door Open / Not Ready!');
    }
}

Here's the same with controller.isDark:

Code:
function onZoneNotReady(zone) {
    if(zone.number == 4 && controller.isDark) helper.sendNotification(controller, zone.bestDescription + ' Door Open / Not Ready!');
}

Here's the same with better organization:

Code:
/* Events */

function onZoneNotReady(zone) {
    sendZoneNotReadyNotification(zone);
    doSomethingElseWithZone(zone);
}

/* Functions */

function sendZoneNotReadyNotification(zone) {
    if(zone.number == 4 && controller.isDark) helper.sendNotification(controller, zone.bestDescription + ' Door Open / Not Ready!');
    // if(zone.number = 10 && other.logic) helper.sendOthernNotification...
    // if ...
    // ...

}

function doSomethingElseWithZone(zone) {
    // Just an example of organization
}

...
 
Oh sorry, just realized this is not quite what you want! The previous examples would send a notification when the zone goes not ready, but not if it was not ready to begin with at 9 PM.

Instead for what you want, you should use the onSyncStart() callback as well as the onZoneNotReady() callbacks together, but as separate triggers for the same notification function.

Code:
/* Events */

function onSyncStart(forced, timestamp) {
    // Use the controller.date property here instead of timestamp because you want it to match the 
    // check in the sendZoneNotReadyNotification() function, otherwise timestamp could end up being off
    // by up to a minute since it comes from the clock on the machine running HaikuHelper not the controller's clock
    var date = new Date(controller.date);

    // At 9:00PM only trigger sendZoneNotReadyNotification() to see if the zone is Not Ready to begin with
    if(date.getHours() == 21 && date.getMinutes == 0) {
        var zone = controller.zoneWithNumber(4);
        sendZoneNotReadyNotification(zone);
    }
}

function onZoneNotReady(zone) {
    sendZoneNotReadyNotification(zone);
}

/* Functions */

function sendZoneNotReadyNotification(zone) {
    var date = new Date(controller.date);

    // Between 9 PM and 7 AM, send a notification if zone 4 becomes not ready
    if(zone.number == 4 && zone.isNotReady && date.getHours() >= 21 && date.getHours() <= 7) {
        helper.sendNotification(controller, zone.bestDescription + ' Door Open / Not Ready!');
    }
}
 
Back
Top