Haiku basic zone logging script not working

btrvalik

Member
This is my first script.. when I tell it to run the script, it seems to run, I get my restart message, but I never seem to get zone events.  Do I need to initialize something?
 
var boilerOnD = new Date();
 
helper.logToExternalFile('BoilerStats.csv',"Boiler Status Restart " + boilerOnD.toLocaleString());
 
 
function sleep(delay) {
        var start = new Date().getTime();
        while (new Date().getTime() < start + delay);
      }
 
function onZoneNotReady (zone) {
 
if (zone.number == 5) { // Driveway Sensor
helper.sendNotification(controller, zone.bestDescription + 'Driveway Tripped' );
}
if (zone.number == 6) { // Boiler Demand
var boilerStatus = "boiler on,";
boilerOnD = new Date();
 
boilerStatus += boilerOnD.toLocaleTimeString() + ",";
var thermostats = controller.thermostats;
for (i in thermostats) {
if( i == 1){
boilerStatus += thermostats.outdoorTemperatureDescription + ",";
}
boilerStatus += thermostats.statusDescription + ",";
boilerStatus += thermostats.temperatureDescription + ",";
}
helper.logToExternalFile('BoilerStats.cvs',boilerStatus);
}
}
 
function onZoneReady (zone) {
if (zone.number == 6) { // Boiler Demand
var boilerStatus = "boiler off,";
var boilerOffD = new Date();
 
 
var duration = (boilerOffD - boilerOnD)/60000;
 
 
boilerStatus += boilerOffD.toLocaleTimeString() + ",";
var thermostats = controller.thermostats;
for (i in thermostats) {
if( i == 1){
boilerStatus += thermostats.outdoorTemperatureDescription + ",";
}
boilerStatus += thermostats.statusDescription + ",";
boilerStatus += thermostats.temperatureDescription + ",";
boilerStatus += duration.toString() + ",";
}
helper.logToExternalFile('BoilerStats.csv',boilerStatus);
}
}
 
Well, one issue I see right off the bat is that you should not use a sleep() function like you have defined, use timers instead. Your approach will cause the entire app to halt/stall.

Now for the actual problem, I think the main cause is that you are not looping through the thermostats correctly. Here's an updated script that should do the trick. I haven't tested it, so let me know if you have any issues.
Code:
var boilerStatusChangeDate = new Date();

function onZoneNotReady (zone) {
	switch(zone.number) {
		case 5: // Driveway Sensor
			helper.sendNotification(controller, "Driveway Tripped: {0}".format(zone.bestDescription));
			break;

		case 6: // Boiler Demand
			logBoilerStatus(zone);
			break;
	}
}

function onZoneReady (zone) {
	switch(zone.number) {
		case 6: // Boiler Demand
			logBoilerStatus(zone);
			break;
	}
}

function logBoilerStatus(zone) {
	var status = "Boiler {0}".format(zone.isNotReady ? "ON" : "OFF");
	var date = new Date();
	var dateDiff = (boilerStatusChangeDate-date)/60000;
	var outdoorTemp = controller.thermostats[0].outdoorTemperatureDescription;

	var thermostatStatuses;
	for (i in controller.thermostats) {
		var thermostat = controller.thermostats[i];
		thermostatStatuses += "{0}, {1}, ".format(thermostat.statusDescription, thermostat.temperatureDescription);
	}

	var line = "{0}, {1}, {2}, {3}, {4}".format(status, date.toLocaleTimeString(), dateDiff, outdoorTemp, thermostatStatuses)
	helper.logToExternalFile('BoilerStats.cvs', line);

	boilerStatusChangeDate = date;
}
If you don't want to log the time that the boiler is off (for easier column totaling), just change the dateDiff definition to:
Code:
var dateDiff = zone.isReady ? ((boilerStatusChangeDate-date)/60000) : 0;
 
Back
Top