---
name: "getUnitByIndicatorCode"
mode: "api"
versionRequirements:
  - { name: "@tuya-miniapp/cloud-api", version: "1.3.0" }
---

## getUnitByIndicatorCode

> [VERSION] @tuya-miniapp/cloud-api >= 1.3.0

> 💡 接口依赖云能力，需在[小程序开发者平台](https://platform.tuya.com/miniapp/)开发设置 - 云能力进行授权配置。使用本接口前，请确保设备所属产品已在[涂鸦 IoT 开发平台](https://iot.tuya.com/)开启**电量统计**高级能力。未启用该能力的产品无法采集电量/电费数据，接口将返回空数据或报错。如需使用，请创建带有电量/电费统计功能的产品，并在产品详情中启用相应高级能力。

### 描述

根据指标编码查询对应的计量单位（如 kWh、元）。可在查询能源数据前调用，以便在 UI 中正确标注数值含义。

### 参数

`Params`

| 参数 | 类型 | 必填 | 描述 |
| --- | --- | --- | --- |
| `params` | `GetUnitByIndicatorCodeParams` | 是 | 请求参数 |

### 返回值

类型: `Promise<string>`

指标计量单位字符串，如 kWh（用电量）、元（电费，随设备货币配置变化）

### 引用对象

##### `interface` GetUnitByIndicatorCodeParams

| 属性 | 类型 | 描述 |
| --- | --- | --- |
| `devId` | `string` | 设备 ID |
| `indicatorCode` | `"IndicatorCode.EleUsage" \\| "IndicatorCode.EleCost"` | 指标编码 |
| `dateType` | `"DateType.Hour" \\| "DateType.Day" \\| "DateType.Month"` | 时间维度 |
| `beginDate` | `string` | 开始时间，格式与 `dateType` 对应 |
| `endDate` | `string` | 结束时间，格式与 `dateType` 对应 |

##### `enum` IndicatorCode

| 枚举值 | 实际值 | 描述 |
| --- | --- | --- |
| `IndicatorCode.EleUsage` | `ele_usage` | 用电量 |
| `IndicatorCode.EleCost` | `ele_cost` | 电费 |

##### `enum` DateType

| 枚举值 | 实际值 | 描述 |
| --- | --- | --- |
| `DateType.Hour` | `hour` | 小时维度，日期格式为 `YYYYMMDDHH`，如 `2026010108` |
| `DateType.Day` | `day` | 天维度，日期格式为 `YYYYMMDD`，如 `20260101` |
| `DateType.Month` | `month` | 月维度，日期格式为 `YYYYMM`，如 `202601` |


### 示例代码

#### 请求示例

```javascript
import { getUnitByIndicatorCode, IndicatorCode, DateType } from '@tuya-miniapp/cloud-api';

getUnitByIndicatorCode({
  devId: 'vdevo123',
  indicatorCode: IndicatorCode.EleUsage,
  dateType: DateType.Day,
  beginDate: '20260101',
  endDate: '20260131',
})
  .then(unit => {
    console.log('指标单位:', unit); // 如 "kWh"
  })
  .catch(error => {
    console.error('获取指标单位失败:', error);
  });
```

#### 返回示例

```json
"kWh"
```


### 常见问题

#### 为什么调用云能力会出现失败的情况？

使用本接口前，请确保设备所属产品已在[涂鸦 IoT 开发平台](https://iot.tuya.com/)开启**电量统计**高级能力。未启用该能力的产品无法采集电量/电费数据，接口将返回空数据或报错。如需使用，请创建带有电量/电费统计功能的产品，并在产品详情中启用相应高级能力。同时，接口依赖云能力，需在[小程序开发者平台](https://platform.tuya.com/miniapp/)`开发设置` - `云能力`进行授权配置。
