I've Been Playing Around With Google Charts

Not sure if I will take this anywhere, but thought I would play around with a small HTML file that could be hosted on the HE hub to display a numeric attribute value over time in a Line Chart. You just need to have the device available in a Maker API instance, populate the variables at the top of the readInDeviceData function and upload the html file to the HE hub local file storage. Not the way I would want to have this setup in the end, but something to get me started in playing around with this bit of code...

image

<script type="text/javascript" src="https://code.jquery.com/jquery-3.6.0.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script type="text/javascript" src="https://github.com/evanplaice/jquery-csv/blob/main/src/jquery.csv.js"></script>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>

<script>

function readInDeviceData() {

	var heHubIp = "";
	var makerAppNo = "";
	var makerToken = "";
	var deviceId = "";
	var deviceAttribute = "";
	
	var deviceData = new google.visualization.DataTable();
	deviceData.addColumn('date', 'DateTime');
    deviceData.addColumn('number', deviceAttribute);
	
	$.ajax({
		  type: "GET",
		  dataType: "json",
		  url: "http://" + heHubIp + "/apps/api/" + makerAppNo + "/devices/" + deviceId + "/events?access_token=" + makerToken,
		  async: false,
		  success: function (fileData) { 
			
			for (x=0; x< fileData.length; x++) {
				if (fileData[x].name = deviceAttribute) { deviceData.addRow([new Date(fileData[x].date), Number(fileData[x].value)]); }
			}
			
		  }
		  }).fail(function(jqXHR, textStatus, errorThrown) {
					console.error(jqXHR, textStatus, errorThrown);
					console.error(jqXHR.responseJSON);
					if (errorThrown == 'Not Found') {
													  alert("Warning: The device data could not be read");
													  return null;
													}
					else {
						   alert("Error reading the device data: " + errorThrown);
						   return null;
						 }
				});	
	return deviceData;
}

function drawChart() {


var data = readInDeviceData();

// Set Options
var options = {
  title: 'Temperature',
  //hAxis: {title: 'Time'},
  vAxis: {title: 'Value'},
  legend: 'none'
};
// Draw Chart
var chart = new google.visualization.LineChart(document.getElementById('myChart'));
chart.draw(data, options);
}
</script>



<div id="myChart" style="max-width:700px; height:400px"></div>

<script>
google.charts.load('current',{packages:['corechart']});
google.charts.setOnLoadCallback(drawChart);
</script>
1 Like

I love the idea of monitoring a device's events longitudinally for all sorts of use cases. And I'd probably use it if you developed it further because I don't want to take the time to learn Grafana/Influx. Plus, ever since Hubigraph was orphaned....

Having said that, I hear people in these parts talking about how the Hubitat architecture is not well suited to managing/visualizing data series. Or something like that. Your take?

1 Like

I'm certainly in the camp that advocates to offload the data recording and analysis to an external system like influxdb and Grafana. That said I can also see the use in having a lightweight option available for small datasets and simple use-cases. My main interest was more in developing the code, but may end up including it somewhere in one of my projects.

2 Likes

Third line of your script won’t load… \ instead of =

Thanks, not sure what happened there....

This is exactly what I am wanting to chart how long my mower is mowing per day. I'd be interested to know more about this. Particularly, what the fileData variable needs to look like for this to work

FileData will just be the results from having called the Maker API events url for the device. If you install maker API, add the mower device, then update the variables towards the top of the file, add this file to the local storage, you can the access it from there. You can setup an iFrame device and add that to a dashboard. Simple....:slightly_smiling_face:

Once you add the mower to maker API, the Maker API app screen has the url's there for you to copy and test, replacing the device id with that of the mower. That will show you the format of the fileData output.

I want to play with it some more to read a similarly formatted file for longer time-frames,

1 Like

I'd also add you may want to look at Bryan's latest creation, I haven't had a chance yet, but I suspect it will also give you what you need.

2 Likes