So I wanted to get MMM-BeeStat working, since its out of date and the module has not been updated to the latest api calls. I appear to have gotten the API configured right, but I am unsure if I am actually pulling the data down right and how to parse the data. This is partly due to the fact I am unsure how the data was presented in the past so I cannot really compare before and after json results from the call and partly due to the fact I am a complete NOOB when it comes to javascript.
If I need to reformat this, or add/remove info or provide more context - please let me know
'use strict';
Module.register("MMM-Beestat", {
hist: [],
// Default module config.
defaults: {
updateInterval: 360 * 60 * 1000, // every 6 hours
url: 'https://api.beestat.io/?api_key=',
api_key: "", //request it from beestat
thermostat_id: 0, //via &resource=ecobee_thermostat&method=read_id
time_period: "month",
time_count: 3,
width: 300,
height: 200,
fadeSpeed: 2000,
chart_title: "Last 3 months runtime"
},
getStyles: function() {
return ["MMM-Beestat.css"];
},
getScripts: function() {
return ["modules/" + this.name + "/node_modules/chart.js/dist/Chart.bundle.min.js"];
},
start: function() {
this.loaded_history = false;
this.getData();
this.scheduleUpdate();
},
// Override dom generator.
getDom: function() {
const outerWrapper = document.createElement("beestat");
const demandWrapper = document.createElement("div");
const chartWrapper = document.createElement("div");
chartWrapper.setAttribute("style", "position: relative; display: inline-block;");
if (!this.loaded_history) {
outerWrapper.innerHTML = this.translate("LOADING");
outerWrapper.className = "dimmed light small";
return outerWrapper;
}
demandWrapper.className = 'medium bright';
if (this.hist) {
// Create chart canvas
const chartCanvas = document.createElement("canvas");
var arrHeat = [];
var arrCool = [];
var arrLabels = []; //later set to blanks so the graph plots the points
for (var i = 0; i < this.hist.data.length; i++) {
var heatRuntime = 0;
var coolRuntime = 0;
//heat
if (this.hist.data[i].auxiliary_heat_1 > 0) {
heatRuntime += this.hist.data[i].sum.auxiliary_heat_1 / 3600;
}
if (this.hist.data[i].auxiliary_heat_2 > 0) {
heatRuntime += this.hist.data[i].sum.auxiliary_heat_2 / 3600;
}
if (this.hist.data[i].auxiliary_heat_3 > 0) {
heatRuntime += this.hist.data[i].sum.auxiliary_heat_3 / 3600;
}
if (this.hist.data[i].compressor_heat_1 > 0) {
heatRuntime += this.hist.data[i].sum.compressor_heat_1 / 3600;
}
if (this.hist.data[i].compressor_heat_2 > 0) {
heatRuntime += this.hist.data[i].sum.compressor_heat_2 / 3600;
}
//cool
if (this.hist.data[i].compressor_cool_1 > 0) {
coolRuntime += this.hist.data[i].sum.compressor_cool_1 / 3600;
}
if (this.hist.data[i].compressor_cool_2 > 0) {
coolRuntime += this.hist.data[i].sum.compressor_cool_2 / 3600;
}
if (heatRuntime > 0) {
arrHeat.push(heatRuntime);
} else {
arrHeat.push(0);
}
if (coolRuntime > 0) {
arrCool.push(coolRuntime);
} else {
arrCool.push(0);
}
arrLabels.push('');
}
var chartconfig = {
type: 'bar',
data: {
labels: arrLabels,
datasets: [{
backgroundColor: "#fd9644",
data: arrHeat
},
{
backgroundColor: "#45aaf2",
data: arrCool
}]
},
options: {
scales: { xAxes: [{ stacked: true }], yAxes: [{ stacked: true }] },
elements: { point: { radius: 0 } },
legend: { display: false },
title: { display: true, text: this.config.chart_title, padding: 5 }
}
};
this.chart = new Chart(chartCanvas.getContext("2d"), chartconfig);
chartCanvas.width = this.config.width;
chartCanvas.height = this.config.height;
chartCanvas.setAttribute("style", "width: " + this.config.width + "; height: " + this.config.height+";");
// Append chart
chartWrapper.appendChild(chartCanvas);
outerWrapper.appendChild(demandWrapper);
outerWrapper.appendChild(chartWrapper);
}
return outerWrapper;
},
scheduleUpdate: function(delay) {
var nextLoad = this.config.updateInterval;
if (typeof delay !== "undefined" && delay >= 0) {
nextLoad = delay;
}
var self = this;
setInterval(function() {
self.getData();
}, nextLoad);
},
getData: function () {
var url = this.config.url + this.config.api_key + '&resource=runtime_thermostat_summary&method=read_id&arguments={"attributes":{"thermostat_id":'+this.thermostat_id+',"date":{"value":"-'+this.time_count+''+this.time_period+'","operator":">"}}}';
this.sendSocketNotification('beestat_runtime', url);
},
socketNotificationReceived: function(notification, payload) {
if (notification === "beestat_runtime") {
this.hist = payload;
this.loaded_history = true;
}
//display only when data is loaded
if (this.loaded_history) {
this.updateDom(this.config.fadeSpeed);
}
},
});
Sample results from postman:
"success": true,
"data": {
"29249277": {
"runtime_thermostat_summary_id": 29249277,
"user_id": 18261,
"thermostat_id": XXXXXX,
"date": "2022-03-07",
"count": 124,
"sum_compressor_cool_1": 0,
"sum_compressor_cool_2": 0,
"sum_compressor_heat_1": 0,
"sum_compressor_heat_2": 0,
"sum_auxiliary_heat_1": 3600,
"sum_auxiliary_heat_2": 1335,
"sum_fan": 8070,
"sum_humidifier": 15,
"sum_dehumidifier": 0,
"sum_ventilator": 0,
"sum_economizer": 0,
"sum_degree_days": 0,
"avg_outdoor_temperature": 33.5,
"avg_outdoor_humidity": 83,
"min_outdoor_temperature": 31.4,
"max_outdoor_temperature": 36.3,
"avg_indoor_temperature": 68.4,
"avg_indoor_humidity": 38,
"deleted": false
},
"29249278": {
"runtime_thermostat_summary_id": 29249278,
"user_id": 18261,
"thermostat_id": XXXXXX,
"date": "2022-03-08",
"count": 287,
"sum_compressor_cool_1": 0,
"sum_compressor_cool_2": 0,
"sum_compressor_heat_1": 0,
"sum_compressor_heat_2": 0,
"sum_auxiliary_heat_1": 11250,
"sum_auxiliary_heat_2": 0,
"sum_fan": 18150,
"sum_humidifier": 5010,
"sum_dehumidifier": 0,
"sum_ventilator": 0,
"sum_economizer": 0,
"sum_degree_days": 0,
"avg_outdoor_temperature": 32,
"avg_outdoor_humidity": 75,
"min_outdoor_temperature": 27.1,
"max_outdoor_temperature": 36.5,
"avg_indoor_temperature": 68.2,
"avg_indoor_humidity": 36,
"deleted": false
},