Device Data Statistics

Last Updated on : 2025-11-24 07:28:53download

Overview

A specialized statistics API suite is designed for chart data visualization, providing device data statistics interfaces across four different time granularities to help you quickly implement:

  • Energy consumption charts: Show device energy consumption, temperature, humidity, and more.
  • Trend analytics: Analyze data change trends.
  • Usage statistics: Track device usage duration and frequency.
  • Data comparison: Compare data across different time periods.

To utilize the statistics capabilities, you must first submit a ticket to subscribe to the product data statistics service.

Core advantages

  • Four time granularities: Offer 15-minute, hourly, daily, and monthly intervals to meet various chart requirements.
  • Flexible statistical types: Support 7 statistical types, including sum, maximum, minimum, and average values.
  • Intelligent data imputation: Automatically fill missing data points to ensure chart continuity.
  • Out-of-the-box: Designed with a singleton pattern, ready to use without initialization.

Core features

Data statistics across four time granularities

Interface Time granularity Typical use case Data points
get15MinData 15 minutes Real-time monitoring, detailed analytics 96 points/day
getHourData Hourly Intra-day trends, hourly comparison 24 points/day
getDayData Daily Weekly/Monthly reports Custom range
getMonthData Monthly Annual reports, long-term trends Custom range

Seven statistical types (StatType enum)

Statistical type Enum value Description Scenario
SUM sum Summation Total energy consumption, usage statistics
MAX max Maximum value Peak analytics, maximum temperature
MIN min Minimum value Valley analytics, minimum temperature
AVG avg Average value Average power, average temperature
MINUX minux Differential value Increment calculation
COUNT count Count Switch cycles, trigger events
RECENT recent Most recent value Real-time status (hourly only)

Recommendations

// Use sum for energy consumption statistics
type = StatType.SUM.value

// Use avg for temperature analytics
type = StatType.AVG.value

// Use max for peak power analytics
type = StatType.MAX.value

API description

get15MinData — 15-minute statistics

Feature description

Get device data statistics at 15-minute intervals within a single day, suitable for real-time monitoring charts and detailed data analytics.

Data characteristics

  • 96 data points per day (24 hours × 4)
  • Time format: YYYYMMDDhhmm. For example, 202411130000 and 202411130015.
  • Ideal for: Line charts and bar charts.
fun get15MinData(
    devId: String,              // Device ID
    dpId: String,               // Data point ID (integer)
    date: String,               // Date: YYYYMMDD
    type: String = "sum",       // Statistical type (default: sum)
    auto: Int = 0,              // Data imputation strategy
    keepScalaPoint: Boolean = false,  // Retain decimal points
    callback: IThingDataCallback<String?> )

Parameter details

Parameter Type Required Default value Description
devId String Yes - The device ID obtained from the device object.
dpId String Yes - The ID of the specified data point.
date String Yes - The query date in a format like 20241113.
type String No sum The statistical type. It is recommended to use StatType enum.
auto Int No 0
  • 0: Fill with 0.
  • 1: Fill with previous value. 
  • 2: Fill with #.
keepScalaPoint Boolean No false Specifies whether to retain decimal points.
callback IThingDataCallback Yes - The callback interface.

Example

Scenario: Draw a line chart of today’s electricity consumption at a 15-minute interval.

// Kotlin example
class PowerConsumptionChart : Fragment() {

    private fun loadChartData() {
        val today = SimpleDateFormat("yyyyMMdd", Locale.getDefault())
            .format(Date())

        ThingSmartBusinessRequest.get15MinData(
            devId = deviceId,
            dpId = 101,  // Assume 101 is the power consumption DP
            date = today,
            type = StatType.SUM.value,  // Using enum is safer
            auto = 0,    // Fill with 0 for missing data
            keepScalaPoint = true,  // Retain decimal places
            callback = object : IThingDataCallback<String?> {
                override fun onSuccess(result: String?) {
                    parseAndDrawChart(result)
                }
                override fun onError(errorCode: String?, errorMessage: String?) {
                    showError("Loading failed: $errorMessage")
                }
            }
        )
    }

    private fun parseAndDrawChart(json: String?) {
        try {
            val data = JSONObject(json)
            val result = data.getJSONObject("result")

            // Parse data points
            val entries = mutableListOf<Entry>()
            result.keys().forEach { timeKey ->
                val value = result.getString(timeKey).toFloat()
                entries.add(Entry(timeKey, value))
            }

            // Draw chart (using libraries like MPAndroidChart)
            drawLineChart(entries)

        } catch (e: Exception) {
            Log.e(TAG, "Data parsing failed", e)
        }
    }
}

Example of returned data

{
  "result": {
    "202411130000": "12.34",
    "202411130015": "11.89",
    "202411130030": "13.45",
    "202411130045": "12.01",
    "202411130100": "14.23",
    "202411130115": "13.67",
    ...
    "202411132345": "10.12"
  },
  "t": 1699862400000,
  "success": true,
  "status": "ok"
}

Data description

  • A total of 96 time points (15 minutes × 96 = 24 hours)
  • Key: Timestamp (YYYYMMDDhhmm)
  • Value: Statistical value (string format)

getHourData — hourly statistics

Feature description

Get device data statistics aggregated by hour within a single day. This is the most commonly used chart data interface.

Data characteristics

  • 24 data points per day.
  • Time format: YYYYMMDDhh. For example, 2024111300 and 2024111301.
  • Ideal for: Intraday trend charts and bar comparison charts.
fun getHourData(
    devId: String,              // Device ID
    dpId: String,               // Data point ID
    date: String,               // Date: YYYYMMDD
    auto: Int = 0,              // Data imputation strategy
    type: String = "sum",       // Statistical type
    keepScalaPoint: Boolean = false,
    callback: IThingDataCallback<String?>
)

Example

Scenario: Draw today’s 24-hour temperature curve.

class TemperatureChart : Fragment() {

    private fun load24HourTemperature() {
        ThingSmartBusinessRequest.getHourData(
            devId = deviceId,
            dpId = "102",  
            date = "20241113",
            type = StatType.AVG.value,  // Use the average temperature value
            auto = 1,  // Fill the gaps with the previous value
            callback = object : IThingDataCallback<String?> {
                override fun onSuccess(result: String?) {
                    drawTemperatureChart(result)
                }

                override fun onError(errorCode: String?, errorMessage: String?) {
                    Toast.makeText(context, "Loading failed", Toast.LENGTH_SHORT).show()
                }
            }
        )
    }
}

Example of returned data

{
  "result": {
    "2024111300": "22.5",
    "2024111301": "22.3",
    "2024111302": "22.1",
    ...
    "2024111323": "21.8"
  },
  "t": 1699862400000,
  "success": true,
  "status": "ok"
}

getDayData — daily statistics

Feature description

Get device data statistics aggregated by day within a specified date range, suitable for weekly and monthly reports.

Data characteristics

  • Supports querying across any number of days.
  • Time format: YYYYMMDD. For example, 20241101 and 20241102.
  • Ideal for: Weekly trend charts and monthly comparison charts.
fun getDayData(
    devId: String,
    dpId: String,  
    type: String = "sum",
    startDay: String,           // Start date: YYYYMMDD
    endDay: String,             // End date: YYYYMMDD
    auto: Int? = null,
    keepScalaPoint: Boolean = false,
    callback: IThingDataCallback<String?>
)

Example

Scenario: Draw a bar chart of this week’s 7-day electricity consumption.

class WeeklyConsumption : Fragment() {

    private fun loadWeeklyData() {
        val calendar = Calendar.getInstance()
        val endDay = SimpleDateFormat("yyyyMMdd").format(calendar.time)

        calendar.add(Calendar.DAY_OF_YEAR, -6)
        val startDay = SimpleDateFormat("yyyyMMdd").format(calendar.time)

        ThingSmartBusinessRequest.getDayData(
            devId = deviceId,
            dpId = 101,
            type = StatType.SUM.value,
            startDay = startDay,  // 7 days ago
            endDay = endDay,      // Today
            auto = 0,
            callback = object : IThingDataCallback<String?> {
                override fun onSuccess(result: String?) {
                    drawWeeklyBarChart(result)
                }

                override fun onError(errorCode: String?, errorMessage: String?) {
                    Log.e(TAG, "Failed to load weekly data: $errorMessage")
                }
            }
        )
    }
}

Example of returned data

{
  "result": {
    "result": {
      "20241107": "45.67",
      "20241108": "52.34",
      "20241109": "48.90",
      "20241110": "51.23",
      "20241111": "49.56",
      "20241112": "53.78",
      "20241113": "47.89"
    },
    "min": "20241113"
  },
  "t": 1699862400000,
  "success": true,
  "status": "ok"
}

getMonthData — monthly statistics

Feature description

Get device data statistics aggregated by month within a specified month range, suitable for annual reports and long-term trend analytics.

Data characteristics

  • Supports querying across any month range.
  • Time format: YYYYMM. For example, 202401 and 202402.
  • Ideal for: Annual trend charts and monthly comparison charts.
fun getMonthData(
    devId: String,
    dpId: String,
    type: String = "sum",
    startMonth: String,         // Start month: YYYYMM
    endMonth: String,           // End month: YYYYMM
    auto: Int? = null,
    keepScalaPoint: Boolean = false,
    callback: IThingDataCallback<String?>
)

Example

Scenario: Draw a comparison chart of this year’s 12-month electricity consumption.

class YearlyConsumption : Fragment() {

    private fun loadYearlyData() {
        val currentYear = Calendar.getInstance().get(Calendar.YEAR)

        ThingSmartBusinessRequest.getMonthData(
            devId = deviceId,
            dpId = 101,
            type = StatType.SUM.value,
            startMonth = "${currentYear}01",  // January
            endMonth = "${currentYear}12",    // December
            callback = object : IThingDataCallback<String?> {
                override fun onSuccess(result: String?) {
                    drawYearlyChart(result)
                }

                override fun onError(errorCode: String?, errorMessage: String?) {
                    showError("Failed to load annual data")
                }
            }
        )
    }
}

Example of returned data

{
  "result": {
    "result": {
      "202401": "1234.56",
      "202402": "1156.78",
      "202403": "1289.90",
      "202404": "1345.67",
      "202405": "1298.45",
      "202406": "1423.89",
      "202407": "1567.23",
      "202408": "1489.01",
      "202409": "1376.54",
      "202410": "1298.76",
      "202411": "1234.00",
      "202412": "0"
    },
    "min": "202412"
  },
  "t": 1699862400000,
  "success": true,
  "status": "ok"
}