> For the complete documentation index, see [llms.txt](https://dev.ubidots.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dev.ubidots.com/dashboards-and-widgets/custom-metric/dashboard-capabilities/widget-data.md).

# Widget data (onInfo / onData)

Read a Custom Metric widget's variables and values with useWidgetEvents — onInfo descriptors and onData values, joined by variableId

The widget receives its data through `useWidgetEvents()`. `onInfo` carries the descriptors: what to render and how it looks. `onData` carries the values. Join them by `variableId`.

## GET

`useWidgetEvents()` returns:

```js
{ onInfo, onData, onReady, onError, emitWidgetEvent, onWidgetEvent, onAnyWidgetEvent, widgetId }
```

Every `on*` subscriber returns an unsubscribe function. Return it from the `useEffect`.

### `onInfo(cb)`: the descriptors

It fires on load and whenever the widget's settings or devices change.

```js
{
  templateId,
  variables: [{ id, label, name, unit, icon, color, aggregationMethod, span }],
  devices: [{ id, label, name }] // the devices that OWN the configured variables
}
```

### `onData(cb)`: the values

The payload has the same shape on the initial load and on every real-time tick.

```js
{
  start, // ISO 8601 string (e.g. "2025-01-01T00:00:00.000Z") or undefined
  end, // ISO 8601 string or undefined
  results: [{ variableId, deviceId, value, timestamp }]
  // timestamp -> epoch ms (number); value -> number or null
}
```

{% hint style="warning" %}
`start` and `end` are ISO strings, but each `results[].timestamp` is in epoch ms. `onData` results carry only ids and the value: look up labels, units and device names in the `onInfo` descriptors by id.
{% endhint %}

## How to implement: render the configured variables

```jsx
import React, { useEffect, useState } from 'react';
import { useWidgetEvents } from '@ubidots/react-html-canvas';

export default function Widget() {
  const { onInfo, onData, onError } = useWidgetEvents();
  const [variables, setVariables] = useState([]);
  const [values, setValues] = useState({});

  useEffect(() => onInfo((p) => setVariables(p?.variables ?? [])), [onInfo]);
  useEffect(() => onData((p) => setValues((prev) => {
    const next = { ...prev };
    for (const r of p?.results ?? []) next[r.variableId] = r;
    return next;
  })), [onData]);
  useEffect(() => onError(() => {/* show a friendly message, never crash */}), [onError]);

  if (!variables.length) return <div>Loading…</div>;
  return variables.map((v) => {
    const cell = values[v.id];
    return <div key={v.id}>{v.label ?? v.name}: {cell?.value ?? '—'} {v.unit ?? ''}</div>;
  });
}
```

Render adaptively: derive scales, ranges and colors from the values you receive, and recompute them on each `onData` tick so real-time updates are reflected automatically.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://dev.ubidots.com/dashboards-and-widgets/custom-metric/dashboard-capabilities/widget-data.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
