Skip to content

Instantly share code, notes, and snippets.

@edesilets
Created November 23, 2022 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edesilets/0ba112fcab7761f049b0aab35938971c to your computer and use it in GitHub Desktop.
Save edesilets/0ba112fcab7761f049b0aab35938971c to your computer and use it in GitHub Desktop.
Just messing around trying to get total heating hours out of the google takeout nest summary.json file. What a pain in the ass.
<?php
class MonthlySummary extends Summary {
public function __toString(): string
{
return implode("\n", [
'-------------------------- MONTHLY REPORT --------------------------',
'Month of ' . $this->date . ' contained ' . $this->cycleCounts . ' Heating cycles.',
'For a total of ' . $this->heatingInHours() . " hours.",
"\n"
]);
}
}
class DailyReport extends Summary {
public array $cycleMessages = [];
public function __toString(): string
{
return implode("\n", [
$this->date . ' contained ' . $this->cycleCounts . ' Heating cycles.',
'For a total of ' . $this->heatingInHours() . " hours.",
"A cycle occurred during: \n" . implode("\n", $this->cycleMessages),
"\n"
]);
}
}
abstract class Summary {
protected const SECONDS_TO_HOURS = 3600;
protected const HOUSE_FIRST_FLOOR_BTU_HR = 30000;
public string $date = '';
public int $cycleCounts = 0;
public int $heatingSeconds = 0;
public function heatingInHours(): int {
return $this->heatingSeconds / self::SECONDS_TO_HOURS;
}
public function hoursToBTUs(): int {
return $this->heatingInHours() * self::HOUSE_FIRST_FLOOR_BTU_HR;
}
}
function pathsToSummaries() {
return [
"/Users/user/Downloads/Takeout/Nest/thermostats/asdfadf-LivingRoom/2022/11/2022-11-summary.json",
];
}
function summariesToAggregate(?string $pathString ): array {
$myfile = fopen($pathString, "r") or die("Unable to open file!");
$contents = fread($myfile,filesize($pathString));
fclose($myfile);
$summary = json_decode($contents,true);
return $summary;
}
/**
* This might be a fuckup. Cycles don't actually seem to be heating times...
*
* @param array $summary
* @return array
*/
function JSONToReports(array $summary): array {
$MonthlySummary = new MonthlySummary();
$dailyReports = [];
foreach ($summary AS $day => $daySummary) {
$DailyReport = new DailyReport();
$DailyReport->date = substr($day,0,10);
if (is_array($daySummary)) {
$cycles = $daySummary['cycles'];
$DailyReport->cycleCounts = count($cycles);
foreach ($cycles as $cycle) {
array_push($DailyReport->cycleMessages, $cycle['caption']['plainText']);
// Type of cycle and hours of run time.
$dayHeatTimeInSeconds = (int)(explode('s', $cycle['duration'])[0]);
$DailyReport->heatingSeconds += $dayHeatTimeInSeconds;
}
}
array_push($dailyReports, $DailyReport);
$MonthlySummary->heatingSeconds += $DailyReport->heatingSeconds;
$MonthlySummary->cycleCounts += $DailyReport->cycleCounts;
$MonthlySummary->date = substr($day,0,7);
}
return [$MonthlySummary, $dailyReports];
}
/**
* Attempt to access events to measure hours of heating
*
* @param array $summary
* @return array
*/
function heatReports(array $summary): array {
$MonthlySummary = new MonthlySummary();
$dailyReports = [];
foreach ($summary AS $day => $daySummary) {
$DailyReport = new DailyReport();
$DailyReport->date = substr($day,0,10);
if (is_array($daySummary)) {
foreach ($daySummary['events'] as $event) {
if ($event['eventType'] === 'EVENT_TYPE_HEAT'){
// Type of cycle and hours of run time.
$dayHeatTimeInSeconds = (int)(explode('s', $event['duration'])[0]);
$DailyReport->heatingSeconds += $dayHeatTimeInSeconds;
$DailyReport->cycleCounts ++;
}
}
}
array_push($dailyReports, $DailyReport);
$MonthlySummary->heatingSeconds += $DailyReport->heatingSeconds;
$MonthlySummary->cycleCounts += $DailyReport->cycleCounts;
$MonthlySummary->date = substr($day,0,7);
}
return [$MonthlySummary, $dailyReports];
}
function loadReports(): array {
$reports = [];
foreach(pathsToSummaries() as $pathString){
$summary = summariesToAggregate($pathString);
array_push($reports,JSONToReports($summary));
// array_push($reports,heatReports($summary));
}
return $reports;
}
function refreshAndGenerateReports(bool $displayMonthly = true) {
$reports = loadReports();
echo "Date,HeatingInHours,EstimatedBTUs\n";
foreach($reports as $yearReport) {
foreach($yearReport as $report) {
if ($report instanceof MonthlySummary && $displayMonthly) {
echo 'Month: ' . $report->date . ','. round(($report->heatingSeconds/3600)). ',' . $report->hoursToBTUs() ."\n";
} else if (!$displayMonthly){
foreach ($report as $daily) {
if ($daily instanceof DailyReport ) {
echo $daily->date . ','. round(($daily->heatingSeconds/3600)) . ','. $daily->hoursToBTUs() . "\n";
}
}
}
}
}
// echo "------------START---------\n";
// var_dump($reports);
// echo "----------END-----------\n";
// echo '---- On ' . $day . ' the heat ran: ' . (string)count($cycles) . " times. Total Hours: " . round($hours) . " ----\n";
// echo 'Day total of run time: ' . $dailyHeatUsageInSeconds . " in Seconds.\n";
// echo 'Day total of run time: ' . $minutes . " in Minutes.\n";
// echo 'Day total of run time: ' . round($hours) . " Hours.\n";
// CSV FORMAT
// echo "\"" . $day . "\",". (string)count($cycles) . ',' . round($hours) . "\n";
// Monthly average hours of heat
// $avgHoursPerDay = array_sum($dayTotalHours) / count($dayTotalHours);
// echo '---AVERAGE HOURS PER DAY OF RUNTIME -- ' . round($avgHoursPerDay) . "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment