You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Google Scripts获取Nest恒温器制热模式的设定温度?

如何通过Google Apps Script获取Nest恒温器的制热设定温度

可以实现,但Google Apps Script本身没有内置的Nest集成,需要借助Google Home Graph API来查询设备状态。以下是具体步骤和示例代码:

前提准备

  1. 在Google Cloud Console创建项目,启用「Home Graph API」
  2. 创建OAuth 2.0客户端ID,配置授权范围为https://www.googleapis.com/auth/homegraph
  3. 将OAuth凭据关联到你的Google Apps Script项目

实现步骤

1. 获取设备ID

先调用devices.list接口获取已关联的Nest恒温器ID,或者直接从Google Home应用的设备详情中查找(部分设备支持查看ID)。

2. 编写查询脚本

使用UrlFetchApp发送POST请求到Home Graph API,解析返回数据提取制热设定温度:

function getNestHeatingSetpoint() {
  const accessToken = getOAuthToken();
  const deviceId = "替换为你的Nest恒温器设备ID";

  const apiUrl = "https://homegraph.googleapis.com/v1/devices:query";
  const requestPayload = JSON.stringify({
    requestId: "nest-query-" + Date.now(),
    inputs: [
      {
        intent: "action.devices.QUERY",
        payload: { devices: [{ id: deviceId }] }
      }
    ]
  });

  const requestOptions = {
    method: "POST",
    contentType: "application/json",
    headers: { Authorization: `Bearer ${accessToken}` },
    payload: requestPayload
  };

  try {
    const response = UrlFetchApp.fetch(apiUrl, requestOptions);
    const responseData = JSON.parse(response.getContentText());
    const deviceStatus = responseData.payload.devices[deviceId];

    // 提取制热设定温度(区分环境温度:thermostatAmbientTemperature)
    const heatingSetpoint = deviceStatus.thermostatTemperatureSetpoint;
    const tempUnit = deviceStatus.thermostatTemperatureUnit;

    Logger.log(`制热设定温度:${heatingSetpoint} ${tempUnit}`);
    return heatingSetpoint;
  } catch (error) {
    Logger.log(`请求失败:${error.message}`);
    throw error;
  }
}

// OAuth授权辅助函数
function getOAuthToken() {
  const oauthService = OAuth2.createService("NestSmartHome")
    .setAuthorizationBaseUrl("https://accounts.google.com/o/oauth2/auth")
    .setTokenUrl("https://oauth2.googleapis.com/token")
    .setClientId("替换为你的OAuth客户端ID")
    .setClientSecret("替换为你的OAuth客户端密钥")
    .setCallbackFunction("authCallback")
    .setPropertyStore(PropertiesService.getUserProperties())
    .setScope("https://www.googleapis.com/auth/homegraph");

  if (oauthService.hasAccess()) {
    return oauthService.getAccessToken();
  } else {
    const authUrl = oauthService.getAuthorizationUrl();
    Logger.log(`请访问此链接完成授权:${authUrl}`);
    throw new Error("未完成API授权,请查看日志中的授权链接");
  }
}

// 授权回调处理
function authCallback(request) {
  const oauthService = OAuth2.createService("NestSmartHome");
  const isAuthorized = oauthService.handleCallback(request);
  return HtmlService.createHtmlOutput(isAuthorized ? "授权成功!可关闭此页面" : "授权失败,请重试");
}

关键说明

  • 脚本中thermostatTemperatureSetpoint对应制热设定温度,thermostatAmbientTemperature是环境实测温度,注意区分
  • 确保你的Nest设备已绑定到当前Google账号,且在Google Home应用中正常显示
  • 首次运行脚本时,需要按照日志中的链接完成OAuth授权

内容的提问来源于stack exchange,提问作者Permafrost

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 14:40:36