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

## getPeakValleyPrice

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

> 💡 接口依赖云能力，需在[小程序开发者平台](https://platform.tuya.com/miniapp/)开发设置 - 云能力进行授权配置。

### 描述

查询设备峰谷电价配置。返回指定设备当前的电价策略，包括电价类型、货币编码及完整电价配置对象。若设备未完成电价配置，priceType、currency 字段可能不存在。

### 参数

`Params`

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

### 返回值

类型: `Promise<GetPeakValleyPriceResult>`

峰谷电价配置，含 priceType（电价类型：0 常规/1 阶梯/2 峰谷）、currency（货币编码）、config（电价配置，含 normalPrice 和 peakValleyPrice 列表）

### 引用对象

##### `interface` GetPeakValleyPriceParams

| 属性 | 类型 | 描述 |
| --- | --- | --- |
| `devId` | `string` | 设备 ID |

##### `interface` GetPeakValleyPriceResult

| 属性 | 类型 | 描述 |
| --- | --- | --- |
| `priceType` | `"PriceType.Constant" \\| "PriceType.Stage" \\| "PriceType.Dynamic"` | 电价类型 |
| `currency` | `string` | 货币编码，如 `CNY`、`USD` |
| `config` | `PeakValleyPriceConfig` | 峰谷电价配置对象 |

##### `enum` PriceType

| 枚举值 | 实际值 | 描述 |
| --- | --- | --- |
| `PriceType.Constant` | `0` | 常规电价 |
| `PriceType.Stage` | `1` | 阶梯电价 |
| `PriceType.Dynamic` | `2` | 峰谷电价 |

##### `interface` PeakValleyPriceConfig

| 属性 | 类型 | 描述 |
| --- | --- | --- |
| `normalPrice` | `string` | 常规（平段）电价，单位为货币/kWh |
| `peakValleyPrice` | `PeakValleyPrice[]` | 峰谷时段电价列表，为空数组时表示仅使用常规电价 |

##### `type` PeakValleyPrice

| 属性 | 类型 | 描述 |
| --- | --- | --- |
| `price` | `string` | 该时段电价，单位为货币/kWh |
| `startTime` | `string` | 时段开始小时，两位字符串，如 `"08"` 表示 08:00 |
| `endTime` | `string` | 时段结束小时，两位字符串，如 `"12"` 表示 12:00 |


### 示例代码

#### 请求示例

```javascript
import { getPeakValleyPrice } from '@tuya-miniapp/cloud-api';

getPeakValleyPrice({ devId: 'vdevo123' })
  .then(result => {
    console.log('电价类型:', result.priceType);
    console.log('常规电价:', result.config.normalPrice);
    result.config.peakValleyPrice.forEach(item => {
      console.log(`${item.startTime}:00 - ${item.endTime}:00, 单价: ${item.price}`);
    });
  })
  .catch(error => {
    console.error('查询峰谷电价失败:', error);
  });
```

#### 返回示例

```json
{
  "priceType": 2,
  "currency": "CNY",
  "config": {
    "normalPrice": "0.56",
    "peakValleyPrice": [
      { "startTime": "08", "endTime": "12", "price": "0.80" },
      { "startTime": "18", "endTime": "22", "price": "0.80" },
      { "startTime": "22", "endTime": "24", "price": "0.30" }
    ]
  }
}
```


### 常见问题

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

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