Haiku Help on Climate Script

dgriffith

Member
I'm trying to collect some climate and security "snapshots" every 30 minutes and having trouble with this code. It used to work with the previous version and I think I got all the proper corrections for the new version syntax. Where is my stupid mistake(s)? :)


Code:
//Run on forced or automatic refresh (4AM)
function onRefreshConfig(forced) {
  controller.refreshAuxiliarySensors();
  controller.refreshZones();
  controller.refreshMessages();
}
 
 
//Climate Stat Collection
var isCollectingStats = false;
 
function onSyncStart(forced, timestamp) {
  if(!isCollectingStats) collectStats();
}
 
function collectStats() {
  if(controller.isConnected) { // Make sure we don't log nonsense if we aren't connected
    isCollectingStats = true;

    var line = controller.date + ',';

    //Get Outdoor Temp
    line += parseInt(controller.outdoorTemperatureSensor.valueDescription) + ',';
   
    //Get Outdoor Humidity
    line += parseInt(controller.outdoorHumiditySensor.valueDescription) + ',';
   
    //Get Indoor Temp
    line += parseInt(controller.auxiliarySensorWithNumber(1).valueDescription) + ',';
   
    //Get Indoor Humidity
    line += parseInt(controller.auxiliarySensorWithNumber(2).valueDescription) + ',';
   
    //Get Status of Windows Zones 5 - 15 (Windows 1 - 12 - no number 2 window)
    //Status reports 0 if closed and 1 if open
    line += controller.zoneWithNumber(5).status + ',';
    line += controller.zoneWithNumber(6).status + ',';
    line += controller.zoneWithNumber(7).status + ',';
    line += controller.zoneWithNumber(8).status + ',';
    line += controller.zoneWithNumber(9).status + ',';
    line += controller.zoneWithNumber(10).status + ',';
    line += controller.zoneWithNumber(11).status + ',';
    line += controller.zoneWithNumber(12).status + ',';
    line += controller.zoneWithNumber(13).status + ',';
    line += controller.zoneWithNumber(14).status + ',';
    line += controller.zoneWithNumber(15).status + ',';
		   
    //Get Status Skylights Zones 16 South 1,49 South 2,50 North 3,51 North 4
    line += controller.zoneWithNumber(16).status + ',';
    line += controller.zoneWithNumber(49).status + ',';
    line += controller.zoneWithNumber(50).status + ',';
    line += controller.zoneWithNumber(51).status + ',';
   
    //Get Status of Fans Unit 263(West Fan) and 264 (East Fan)
    line += controller.unitWithNumber(263).state + ',';
    line += controller.unitWithNumber(264).state + ',';
	   
    // The file gets stored in ~/Library/Application Support/com.nullriver.HaikuHelper2/
    //Could change this to an absolute path in webserver
    helper.logToExternalFile('../../../ClimateStats.csv', line); //Logs to user home dir
 
    setTimeout('collectStats()', 30*60*1000); // Every 30 minutes
  } else {
    isCollectingStats = false;
  }
}
 
I think its working fine, just that you are writing it to ~/Library/ not ~/ as you expect because you are using ../../../ but Haiku now places logs in a Logs subfolder of its document folder, which means you need to add one more ".." if you want it to be in your Home (where I'm guessing you are expecting it).
 
One more minor thing: Since you are logging units, you should probably add controller.refreshUnits() to onRefreshConfig()
 
Thank You, it was working perfectly but was writing into the Library directory.

Has the controller.date function changed? It is returning a formatted date not a unix timestamp.

Thanks again!
 
lupinglade said:
I think its working fine, just that you are writing it to ~/Library/ not ~/ as you expect because you are using ../../../ but Haiku now places logs in a Logs subfolder of its document folder, which means you need to add one more ".." if you want it to be in your Home (where I'm guessing you are expecting it).
Hi lupinglade,
I too am having problems with this function. 
It works fine when I use the example from the API documentation. However, I would like to share the folder with my PC's so I can use excel to examine. I'm not a big Mac user. I can't share the 'Logs' folder, because I can't navigate to it from the 'Settings/Share files' window. 
THUS, I would like to store the file in ~/Documents.
Now the path to my ClimateStats.csv file when I use "helper.logToExternalFile('ClimateStats.csv', line);" is...
~/Library/Containers/com.nullriver.HaikuHelper2/Data/Library/Application Support/com.nullriver.HaikuHelper2/Logs/ClimateStats.csv
This is a total of 8 'Parent' jumps to get to the Home directory. 
The API documentation states that the path is ...
~/Library/Containers/com.nullriver.HaikuHelper2/Library/Application Support/com.nullriver.HaikuHelper2/Logs/ClimateStats.csv - ie misses out the 'Data' folder between the first com.nullriver.HH2 folder and its Library subdirectory.
 
So, I have tried
1. '~/Documents/ClimateStats.csv'
2. '../../../../Documents/ClimateStats.csv' (your suggestion)
3. '../../../../../../Documents/ClimateStats.csv'
4. '../../../../../../../Documents/ClimateStats.csv' & even
5. '../../../../../../../../Documents/ClimateStats.csv'
 
The ONLY one that has worked is No 2 - yours. HOWEVER, it placed the file in 
~/Library/Containers/com.nullriver.HaikuHelper2/Data/Documents folder
and not as desired
~/Library/Documents.
 
You might say "just share the above folder instead, however, when I use the share files dialog, the 'Library' subdirectory of the Home folder is not visible. I can never see that folder unless I reverse navigate to it from the HH 'Show Logs Folder' menu item.
 
Any ideas what's going on and how I can fix my issue?
Much appreciated. 
 
lupinglade said:
In any open dialog you can drag the file to it or use clans+shift+g to type in a path.
I think you might have misunderstood me. I have copied the file to and from the desired directory, but I don't want to have to do this manually. I want the script to write the file directly to the desired location. 
eg helper.logToExternalFile('~/Documents/ClimateStats.csv', line);
Thanks
 
I was replying to this:
 
I can't share the 'Logs' folder, because I can't navigate to it from the 'Settings/Share files' window. 
 
Using shift+G, you can navigate to it and share it. There is no way to write to the documents folder directly currently.
 
Back
Top