getUnitByIndicatorCode(params: GetUnitByIndicatorCodeParams)

Requires:
@tuya-miniapp/cloud-api≥1.3.0
Additional notesRead the full notes
This API relies on cloud capabilities. You must grant authorization in Development Settings - Cloud Capabilities on the Mini App Developer Platform. Before using this API, ensure the device’s product has enabled the **Energy Statistics** advanced capability on the Tuya IoT Development Platform. Products without this capability cannot collect energy consumption/cost data; the API will return empty data or an error. To use this feature, create a product with energy consumption/cost statistics and enable the corresponding advanced capability in the product details.

Description

Query the measurement unit by metric code (e.g., kWh, yuan). Call this before querying energy data so the UI can label values correctly.

Parameters

ParameterTypeRequiredDescription
YesRequest parameters

Return Value

Type: Promise<string>
Metric unit string, e.g., kWh (consumption) or currency (cost; varies with the device’s currency configuration)

Referenced Types

enumIndicatorCode
ValueActual ValueDescription
IndicatorCode.EleUsageele_usageElectricity consumption
IndicatorCode.EleCostele_costElectricity bill
enumDateType
ValueActual ValueDescription
DateType.HourhourHour-level granularity; date format is `YYYYMMDDHH`, e.g., `2026010108`
DateType.DaydayDaily granularity, date format `YYYYMMDD`, e.g., `20260101`
DateType.MonthmonthMonthly granularity, date format `YYYYMM`, e.g., `202601`

Valid Values

IndicatorCode
ValueDescriptionSince
ele_usageElectricity consumption-
ele_costElectricity bill-
DateType
ValueDescriptionSince
hourHour-level granularity; date format is `YYYYMMDDHH`, e.g., `2026010108`-
dayDaily granularity, date format `YYYYMMDD`, e.g., `20260101`-
monthMonthly granularity, date format `YYYYMM`, e.g., `202601`-

Examples

Request example

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('Indicator unit:', unit); // e.g., "kWh"
  })
  .catch(error => {
    console.error('Failed to get indicator unit:', error);
  });

Response example

"kWh"

FAQ

Why does the call to cloud capabilities fail?

Before using this API, please ensure that the product to which the device belongs has enabled the Electricity Statistics advanced capability on the Tuya IoT Development Platform. Products without this capability enabled cannot collect electricity consumption/cost data, and the API will return empty data or an error. If you need to use it, please create a product with electricity consumption/cost statistics functions and enable the corresponding advanced capabilities in the product details. At the same time, the API depends on cloud capabilities, and authorization configuration is required in Development Settings - Cloud Capabilities on the Mini Program Developer Platform.