function get_nest_data() { $nest = new Nest(); $info = $nest->getDeviceInfo(); $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); return $data; }
public function pushNestData($db) { define('USERNAME', $this->getApplication()->getSilexApplication()['config']['nest']['username']); define('PASSWORD', $this->getApplication()->getSilexApplication()['config']['nest']['password']); $nest = new \Nest(); $info = $nest->getDeviceInfo(); $temperature = $info->current_state->temperature; $humidity = $info->current_state->humidity; $state = $info->target->mode; $db->insert("temperature", ['fields' => array('value' => $temperature)]); $db->insert("humidity", ['fields' => array('value' => $humidity)]); $db->insert("state", ['fields' => array('value' => $state)]); }
// Here's how to use this class: $nest = new Nest($username, $password); echo "Location information:\n"; $locations = $nest->getUserLocations(); jlog($locations); echo "----------\n\n"; echo "Devices list (thermostats):\n"; $devices_serials = $nest->getDevices(); jlog($devices_serials); echo "----------\n\n"; echo "Devices list (Nest Protect):\n"; $protects_serials = $nest->getDevices(DEVICE_TYPE_PROTECT); jlog($protects_serials); echo "----------\n\n"; echo "Device information:\n"; $infos = $nest->getDeviceInfo($devices_serials[0]); jlog($infos); echo "----------\n\n"; echo "Current temperature:\n"; printf("%.02f degrees %s\n", $infos->current_state->temperature, $infos->scale); echo "----------\n\n"; echo "Setting target temperature...\n"; // Note: setting temperatures will use the units you set on the device. I'm using celsius on my device, so I'm using celsius here. $success = $nest->setTargetTemperature(26); var_dump($success); echo "Setting target temperatures (range)...\n"; $success = $nest->setTargetTemperatures(23.0, 26.0); var_dump($success); echo "Setting target temperature mode...\n"; $success = $nest->setTargetTemperatureMode(TARGET_TEMP_MODE_COOL, 26.0); // Available: TARGET_TEMP_MODE_COOL, TARGET_TEMP_MODE_HEAT, TARGET_TEMP_MODE_RANGE
$nest = new Nest(); function setHumidity($nest, $humidity) { $target = intval($humidity); if ($target > $GLOBALS['safelimit']) { echo "Error: Requested target exceeds safety limit.\n"; return 1; } elseif ($target < 0) { $target = 0; } echo "Setting humidity target to " . $target . "%\n"; $success = $nest->setHumidity($target); return $success; } if (count($argv) == 1) { $infos = $nest->getDeviceInfo(); echo "Current humidity level: " . $infos->current_state->humidity . "%\n"; } elseif ($argv[1] == "auto") { $locationinfo = $nest->getUserLocations(); $exttemp = round($locationinfo[0]->outside_temperature, 0); if ($exttemp >= 0) { $autotarget = $maxhumidity; } else { // Drop target humidity 5% for every 5degree C drop below 0 $autotarget = $maxhumidity - 5 * abs($exttemp / 5); } $success = setHumidity($nest, $autotarget); } elseif (is_numeric($argv[1])) { $success = setHumidity($nest, intval($argv[1])); } else { echo "Invalid humidity value entered. Must be either 'auto' or a value between 0 and " . $safelimit . ".\n";
// Google maps api URL // This is required to get the timezone offset from the current user's location $google_json = "https://maps.googleapis.com/maps/api/timezone/json?location=" . $user_lat . "," . $user_long . "×tamp=" . $timestamp; $google_time = json_decode(file_get_contents($google_json)); $dst_offset = $google_time->dstOffset; $raw_offset = $google_time->rawOffset; $timestamp_offset = ($dst_offset + $raw_offset) / 60 / 60; ////////////// $update_statement = $db_connect->prepare("\n\t\t\t\tUPDATE users\n\t\t\t\tSET nest_username = :nest_username,\n\t\t\t\t nest_password = :nest_password,\n\t\t\t\t user_location = :user_location,\t\t\t\t \n\t\t\t\t timestamp_offset = :timestamp_offset\n\t\t\t\tWHERE user_id = :user_id\n\t\t\t"); $update_statement->execute(array('nest_username' => $nest_username, 'nest_password' => $nest_password_encrypt, 'user_location' => $nest_location, 'timestamp_offset' => $timestamp_offset, 'user_id' => $user_id)); define('USERNAME', $nest_username); define('PASSWORD', $nest_password); $nest = new Nest(); $nest_devices = $nest->getDevices(); foreach ($nest_devices as $device) { $device_info = $nest->getDeviceInfo($device); $insert_statement = $db_connect->prepare("\n\t\t REPLACE INTO devices\n\t\t SET device_serial_number = :device_serial_number,\n\t\t user_id = :user_id,\n\t\t device_location = :device_location,\n\t\t device_name = :device_name\n\t\t "); $insert_statement->execute(array('device_serial_number' => $device, 'user_id' => $user_id, 'device_location' => $nest_location, 'device_name' => $device_info->name)); } $user_location = $nest_location; $message = isset($message) ? $message : ''; $success_message = $message . "Updated user preferences"; $tpl_success = new Template("../includes/templates/success.tpl"); $tpl_success->set("success_text", $success_message); echo $tpl_success->fetch(); } $tpl_head = new Template("../includes/templates/head.tpl"); $tpl_nav = new Template("../includes/templates/nav-user.tpl"); $tpl_foot = new Template("../includes/templates/foot.tpl"); $tpl_profile = new Template("../includes/templates/profile.tpl"); $tpl_head->set('title', "Nest Administration Tool: Settings");
// If the outside temperature is lower than the current inside dewpoint, reduce max humidity while ($dp - $outside > MAX_DEWPOINT_DELTA && $target > MIN_HUMIDITY) { $target -= 5; $dp = calculateDewpoint($inside, $target); $dpo = $dp - $outside; } // Respect MIN/MAX settings $target = min($target, MAX_HUMIDITY); $target = max($target, MIN_HUMIDITY); return $target; } $nest = new Nest(); try { // Retrieve data $locations = $nest->getUserLocations(); $thermostat = $nest->getDeviceInfo(THERMOSTAT_SERIAL); $insideTemp = $thermostat->current_state->temperature; $outsideTemp = $locations[0]->outside_temperature; $insideHumidity = $thermostat->current_state->humidity; $targetHumidityCurrent = $thermostat->target->humidity; // Determine heat/cool target temperatures $mode = $thermostat->target->mode; $cool = $heat = $thermostat->target->temperature; if ($mode == 'range') { $heat = $heat[0]; $cool = $cool[0]; } else { if ($mode == 'cool') { $heat = null; } else { if ($mode == 'heat') {
// Google maps api URL // This is required to get the timezone offset from the current user's location $google_json = "https://maps.googleapis.com/maps/api/timezone/json?location=" . $user_lat . "," . $user_long . "×tamp=" . $timestamp; $google_time = json_decode(file_get_contents($google_json)); $dst_offset = $google_time->dstOffset; $raw_offset = $google_time->rawOffset; $timestamp_offset = ( $dst_offset + $raw_offset ) / 60 / 60; //$local_time = $timestamp + $dst_offset + $raw_offset; */ $nest = new Nest($nest_username, $nest_password_decrypt); $nest_devices = $nest->getDevices(); foreach ($nest_devices as $device) { // Gather information from the Nest class object for storage $infos = $nest->getDeviceInfo($device); $energy = $nest->getEnergyLatest($device); $weather_nest = $nest->getWeather($user_location); // Gather the device information for storage $device_serial_number = $infos->serial_number; $nest_location = $infos->where; $device_name = $infos->name; $battery_level = $infos->current_state->battery_level; // Outside weather pulled from the nest class $outside_humidity = $weather_nest->outside_humidity; $outside_temperature = $weather_nest->outside_temperature; // Inside weather pulled from the nest class $temperature = $infos->current_state->temperature; $humidity = $infos->current_state->humidity; // Current running statistics for the graph $ac = $infos->current_state->ac == "" ? 0 : $infos->current_state->ac;