Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 38 additions & 6 deletions collect.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,45 @@
function get_nest_data() {
$nest = new Nest();
$info = $nest->getDeviceInfo();

if (preg_match("/away/", $info->current_state->mode) || preg_match("/range/", $info->current_state->mode)) {
if ($info->current_state->temperature > $info->target->temperature[1]) {
//Hotter then upper temp
$targetTemp = $info->target->temperature[1];
}
else if ($info->current_state->temperature < $info->target->temperature[0]) {
//Colder then lower temp
$targetTemp = $info->target->temperature[0];
}
else {
if (($info->target->temperature[1] - $info->current_state->temperature) <
($info->current_state->temperature - $info->target->temperature[0]))
{
//Closer to upper temp
$targetTemp = $info->target->temperature[1];
}
else
{
//Closer to lower temp
$targetTemp = $info->target->temperature[0];
}
}
}
else {
$targetTemp = $info->target->temperature;
}

$data = array('heating' => ($info->current_state->heat == 1 ? 1 : 0),
'timestamp' => $info->network->last_connection,
'target_temp' => sprintf("%.02f", (preg_match("/away/", $info->current_state->mode) ?
$info->target->temperature[0] : $info->target->temperature)),
'current_temp' => sprintf("%.02f", $info->current_state->temperature),
'humidity' => $info->current_state->humidity
);
'cooling' => ($info->current_state->ac == 1 ? 1 : 0),
'fan' => ($info->current_state->fan == 1 ? 1 : 0),
'autoAway' => ($info->current_state->auto_away == 1 ? 1 : ($info->current_state->auto_away == -1 ? -1 : 0)),
'manualAway' => ($info->current_state->manual_away == 1 ? 1 : 0),
'leaf' => ($info->current_state->leaf == 1 ? 1 : 0),
'timestamp' => $info->network->last_connection,
'target_temp' => sprintf("%.02f", $targetTemp),
'current_temp' => sprintf("%.02f", $info->current_state->temperature),
'humidity' => $info->current_state->humidity
);
return $data;
}

Expand Down
53 changes: 53 additions & 0 deletions collectEnergy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

require 'inc/config.php';
require 'nest-api-master/nest.class.php';

define('USERNAME', $config['nest_user']);
define('PASSWORD', $config['nest_pass']);

date_default_timezone_set($config['local_tz']);

function get_nest_data() {
$nest = new Nest();
$info = $nest->getDeviceInfo();
$energy = $nest->getEnergyLatest();

//Change nulls to 0
foreach ($energy->days as &$day)
{
foreach ($day->events as &$events)
{
if ( empty($events -> continuation) )
{
$events -> continuation = 0;
}
if ( empty($events -> touched_by) )
{
$events -> touched_by = 0;
}
if ( empty($events -> touched_when) )
{
$events -> touched_when = 0;
}
if ( empty($events -> touched_timezone_offset) )
{
$events -> touched_timezone_offset = 0;
}
if ( empty($events -> touched_where) )
{
$events -> touched_where = 0;
}
}
unset($events);
}
unset($day);

return $energy;
}

function c_to_f($c) {
return ($c * 1.8) + 32;
}

?>
48 changes: 46 additions & 2 deletions dbsetup
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,56 @@ USE nest;
CREATE TABLE `data` (
`timestamp` timestamp NOT NULL,
`heating` tinyint unsigned NOT NULL,
`cooling` tinyint unsigned NOT NULL,
`fan` tinyint unsigned NOT NULL,
`autoAway` tinyint signed NOT NULL,
`manualAway` tinyint unsigned NOT NULL,
`leaf` tinyint unsigned NOT NULL,
`target` numeric(7,3) NOT NULL,
`current` numeric(7,3) NOT NULL,
`humidity` tinyint unsigned NOT NULL,
`updated` timestamp NOT NULL,
PRIMARY KEY (`timestamp`),
UNIQUE KEY `timestamp` (`timestamp`)
)
ENGINE=MyISAM DEFAULT CHARSET=latin1;
)ENGINE=MyISAM DEFAULT CHARSET=latin1;

CREATE TABLE `nest`.`cycles_data` (
`cycleNum` int(10) unsigned NOT NULL,
`cycleDate` datetime NOT NULL,
`start` int(10) unsigned NOT NULL,
`duration` int(10) unsigned NOT NULL,
`type` int(10) unsigned NOT NULL,
PRIMARY KEY (`cycleNum`,`cycleDate`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `nest`.`energy_data` (
`energyDate` datetime NOT NULL,
`device_timezone_offset` int(11) NOT NULL,
`total_heating_time` int(10) unsigned NOT NULL,
`total_cooling_time` int(10) unsigned NOT NULL,
`total_fan_cooling_time` int(10) unsigned NOT NULL,
`total_humidifier_time` int(10) unsigned NOT NULL,
`total_dehumidifier_time` int(10) unsigned NOT NULL,
`leafs` int(11) NOT NULL,
`whodunit` int(11) NOT NULL,
`recent_avg_used` int(10) unsigned NOT NULL,
`usage_over_avg` int(10) NOT NULL,
PRIMARY KEY (`energyDate`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `nest`.`events_data` (
`eventNum` int(10) unsigned NOT NULL,
`eventDate` datetime NOT NULL,
`start` int(10) unsigned NOT NULL,
`end` int(10) unsigned NOT NULL,
`type` int(10) unsigned NOT NULL,
`touched_by` int(10) unsigned NOT NULL,
`touched_when` int(10) unsigned NOT NULL,
`touched_timezone_offset` int(11) NOT NULL,
`touched_where` int(11) NOT NULL,
`heat_temp` int(11) NOT NULL,
`cool_temp` int(11) NOT NULL,
`continuation` int(11) NOT NULL,
`event_touched_by` int(11) NOT NULL,
PRIMARY KEY (`eventNum`,`eventDate`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
8 changes: 4 additions & 4 deletions fetch.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
define('DEFAULT_HRS', 72);

$hrs = DEFAULT_HRS;
if ($_GET["hrs"]) {
if (!empty($_GET["hrs"])) {
$hrs = $_GET["hrs"];
}

Expand All @@ -15,11 +15,11 @@
if ($stmt = $db->res->prepare("SELECT * from data where timestamp>=DATE_SUB(NOW(), INTERVAL ? HOUR) order by timestamp")) {
$stmt->bind_param("i", $hrs);
$stmt->execute();
$stmt->bind_result($timestamp, $heating, $target, $current, $humidity, $updated);
$stmt->bind_result($timestamp, $heating, $cooling, $fan, $autoAway, $manualAway, $leaf, $target, $current, $humidity, $updated);
header("Content-type: text/tab-separated-values");
print "timestamp\theating\ttarget\tcurrent\thumidity\tupdated\n";
print "timestamp\theating\tcooling\tfan\tautoAway\tmanualAway\tleaf\ttarget\tcurrent\thumidity\tupdated\n";
while ($stmt->fetch()) {
print implode("\t", array($timestamp, $heating, $target, $current, $humidity, $updated)) . "\n";
print implode("\t", array($timestamp, $heating, $cooling, $fan, $autoAway, $manualAway, $leaf, $target, $current, $humidity, $updated)) . "\n";
}
$stmt->close();
}
Expand Down
14 changes: 12 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@

// fetch the data
d3.tsv("fetch.php?hrs=" + hours, function(error, data) {
color.domain(d3.keys(data[0]).filter(function(key) { return (key == "current" || key == "target" || key == "heating"); }));
color.domain(d3.keys(data[0]).filter(function(key) { return (key == "current" || key == "target" || key == "heating"|| key == "cooling"|| key == "fan"|| key == "autoAway"|| key == "manualAway"|| key == "leaf"|| key == "humidity"); }));

data.forEach(function(d) {
d.date = parseDate(d.timestamp);
Expand All @@ -153,7 +153,17 @@
name: name,
values: data.map(function(d) {
if (name == "heating")
return { date: d.date, val: +d[name] + 60 };
return { date: d.date, val: +d[name] + 53 };
else if(name == "cooling")
return { date: d.date, val: +d[name] + 50 };
else if(name == "fan")
return { date: d.date, val: +d[name] + 47 };
else if(name == "autoAway")
return { date: d.date, val: +d[name] + 44 };
else if(name == "manualAway")
return { date: d.date, val: +d[name] + 41 };
else if(name == "leaf")
return { date: d.date, val: +d[name] + 38 };
else
return { date: d.date, val: +d[name] };
})
Expand Down
4 changes: 2 additions & 2 deletions insert.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
$db = new DB($config);
$data = get_nest_data();
if (!empty($data['timestamp'])) {
if ($stmt = $db->res->prepare("REPLACE INTO data (timestamp, heating, target, current, humidity, updated) VALUES (?,?,?,?,?,NOW())")) {
$stmt->bind_param("siddi", $data['timestamp'], $data['heating'], $data['target_temp'], $data['current_temp'], $data['humidity']);
if ($stmt = $db->res->prepare("REPLACE INTO data (timestamp, heating, cooling, fan, autoAway, manualAway, leaf, target, current, humidity, updated) VALUES (?,?,?,?,?,?,?,?,?,?,NOW())")) {
$stmt->bind_param("siiiiiiddi", $data['timestamp'], $data['heating'], $data['cooling'], $data['fan'], $data['autoAway'], $data['manualAway'], $data['leaf'], $data['target_temp'], $data['current_temp'], $data['humidity']);
$stmt->execute();
$stmt->close();
}
Expand Down
59 changes: 59 additions & 0 deletions insertEnergy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

require 'inc/config.php';
require 'inc/class.db.php';
require 'collectEnergy.php';

try {
$db = new DB($config);
$energy = get_nest_data();

foreach ($energy->days as $day)
{
foreach ($day->events as $key => $events)
{
if ($stmt = $db->res->prepare("REPLACE INTO events_data (eventNum, eventDate, start, end, type, touched_by, touched_when, touched_timezone_offset, touched_where, heat_temp, cool_temp, continuation, event_touched_by) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)"))
{
$stmt->bind_param("isiiiiiiiiiii", $key, $day->day, $events->start, $events->end, $events->type, $events->touched_by, $events->touched_when, $events-> touched_timezone_offset, $events->touched_where, $events->heat_temp, $events->cool_temp, $events->continuation, $events->event_touched_by);
$stmt->execute();
$stmt->close();
}
}

if ($stmt = $db->res->prepare("DELETE FROM events_data where eventNum > $key AND eventDate = '$day->day'"))
{
$stmt->execute();
$stmt->close();
}
unset($key);

foreach ($day->cycles as $key => $cycles)
{
if ($stmt = $db->res->prepare("REPLACE INTO cycles_data (cycleNum, cycleDate, start, duration, type) VALUES (?,?,?,?,?)"))
{
$stmt->bind_param("isiii", $key, $day->day, $cycles->start, $cycles->duration, $cycles->type);
$stmt->execute();
$stmt->close();
}
}

if ($stmt = $db->res->prepare("DELETE FROM cycles_data where cycleNum > $key AND cycleDate = '$day->day'"))
{
$stmt->execute();
$stmt->close();
}
unset($key);

if ($stmt = $db->res->prepare("REPLACE INTO energy_data (energyDate, device_timezone_offset, total_heating_time, total_cooling_time, total_fan_cooling_time, total_humidifier_time, total_dehumidifier_time, leafs, whodunit, recent_avg_used, usage_over_avg) VALUES (?,?,?,?,?,?,?,?,?,?,?)"))
{
$stmt->bind_param("siiiiiiiiii", $day->day, $day->device_timezone_offset, $day->total_heating_time, $day->total_cooling_time, $day->total_fan_cooling_time, $day->total_humidifier_time, $day->total_dehumidifier_time, $day->leafs, $day->whodunit, $day->recent_avg_used, $day->usage_over_avg);
$stmt->execute();
$stmt->close();
}
}
$db->close();
} catch (Exception $e) {
$errors[] = ("DB connection error! <code>" . $e->getMessage() . "</code>.");
}

?>
15 changes: 8 additions & 7 deletions test.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,28 @@

$status = $nest->getStatus();
print_r($status);
echo "\n<br>\n<br>";

$infos = $nest->getDeviceInfo();
print_r($infos);

echo "\n<br>\n<br>";
stuff_we_care_about($infos);

function stuff_we_care_about($info) {
echo "Heating : ";
printf("%s\n", ($info->current_state->heat == 1 ? 1 : 0));
printf("%s\n<br>", ($info->current_state->heat == 1 ? 1 : 0));
echo "Timestamp : ";
printf("%s\n", $info->network->last_connection);
printf("%s\n<br>", $info->network->last_connection);
echo "Target temperature : ";
if (preg_match("/away/", $info->current_state->mode)) {
printf("%.02f\n", $info->target->temperature[0]);
printf("%.02f\n<br>", $info->target->temperature[0]);
} else {
printf("%.02f\n", $info->target->temperature);
printf("%.02f\n<br>", $info->target->temperature);
}
echo "Current temperature : ";
printf("%.02f\n", $info->current_state->temperature);
printf("%.02f\n<br>", $info->current_state->temperature);
echo "Current humidity : ";
printf("%d\n", $info->current_state->humidity);
printf("%d\n<br>", $info->current_state->humidity);

}

Expand Down