Skip to content

Instantly share code, notes, and snippets.

@jrbenito
Last active December 10, 2015 05:38
Show Gist options
  • Save jrbenito/4389251 to your computer and use it in GitHub Desktop.
Save jrbenito/4389251 to your computer and use it in GitHub Desktop.
Google Apps Scripts to access Flukso.net data. This can be used in a Google Spreadsheet.
/**
* Access Flukso API server returning a JSON
*
*/
function getJSON(sensor, token, query) {
var sheetname = "test";
//var aUrl = "http://api.flukso.net/sensor/c1411c6b4f9910bbbab09f145f8533b9?version=1.0&token=d8a8ab8893ea73f768b66b45234b5c3a&interval=month&unit=watt";
var api="1.0";
var aUrl = "http://api.flukso.net/sensor/"+sensor+"?version="+api+"&token="+token+query;
var headers = {
"Accept": "application/json",
};
var options = {
"method": "get",
"headers": headers,
};
var response = UrlFetchApp.fetch(aUrl); // get feed
return JSON.parse(response.getContentText());
}
/**
* Get information from Flukso, day resolution
*/
function getKWhMonth(sensor,token, start, end) {
// Prepare query for API server
var days = end - start;
start = convertDate(start);
end = convertDate(end);
var query = "&start=" + start +"&end=" + end +"&resolution=day&unit=watt";
var data=getJSON(sensor, token, query);
var sum = 0;
for (var i = 0; i < data.length; i++) {
// data[x] = [ Unix date, Watt]
// so, use the second parameter [1]
if (typeof data[i][1] == "number")
sum = sum + data[i][1];
}
// Average KWh
return ((sum *24)/1000);
}
/**
* Get information from Flukso, hour resolution
*/
function getKWhDay(sensor,token, start, end) {
// Prepare query for API server
//var days = end - start;
start = convertDate(start);
end = convertDate(end);
var query = "&start=" + start +"&end=" + end +"&resolution=hour&unit=watt";
var data=getJSON(sensor, token, query);
var sum = 0;
for (var i = 0; i < data.length; i++) {
// data[x] = [ Unix date, Watt]
// so, use the second parameter [1]
if (typeof data[i][1] == "number")
sum = sum + data[i][1];
}
// Average KWh
return (sum/1000);
}
/**
* Converts a given date number to Unix timestamp
* (seconds from Jan-1-1970)
* Accounts for Timezone Offset
*/
function convertDate(dateNumber) {
var ret = Date.parse(dateNumber);
var dat = new Date(ret);
var offset = dat.getTimezoneOffset() * 60; // seconds
// return seconds instead of milliseconds
return (ret /1000) - offset;
}
/**
* Calculate coast of consumption based on
*
* 1) KWh coast and company consuption reading
*
* or
*
* 2) Previous KWh coast and flukso actual reading
*/
function custo(companyRead,fluksoRead,custoActual,custoPrevious) {
var ret = 0;
if (((companyRead != "") && (typeof companyRead == "number")) &&
((custoActual != "") && (typeof custoActual == "number"))) {
ret = companyRead * custoActual;
} else {
if (((fluksoRead != "") && (typeof fluksoRead == "number")) &&
((custoActual != "") && (typeof custoActual == "number"))) {
ret = fluksoRead * custoActual;
} else {
if (((fluksoRead != "") && (typeof fluksoRead == "number")) )//&&
// ((custoPrevious != "") && (typeof custoPrevious == "number")))
ret = fluksoRead * custoPrevious;
}
}
return (ret>0) ? ret : "";
}
/**
* Adds a custom menu to the active spreadsheet, containing a single menu item
* for invoking the readRows() function specified above.
* The onOpen() function, when defined, is automatically invoked whenever the
* spreadsheet is opened.
* For more information on using the Spreadsheet API, see
* https://developers.google.com/apps-script/service_spreadsheet
*/
/*function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "getJson",
functionName : "getJson"
}];
sheet.addMenu("Script Center Menu", entries);
};*/
@jrbenito
Copy link
Author

I used this snippet to build a spreadsheet that grabs information from Flukso.net (who holds my Fluksometer data) and compare it to my electricity monthly bill. The template of this spreadsheet is available at: https://drive.google.com/previewtemplate?id=0Anjfupsi3t5DdEd4eVh1RVV5cktiTTM3SFlJa0ZqY0E&mode=public

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment