Devices
List the dashboard's devices and change the selected device from a Custom Metric widget — dashboardDevices, useUbidotsSelectedDevice, setDashboardDevice
GET
All devices on the dashboard, to list them or build a picker:
useUbidots().state.dashboardDevicesreturnsDevice[] | null. It arrives through thev2:dashboard:devices:selfevent.The selected device(s):
useUbidotsSelectedDevice()returnsDevice | null.useUbidotsSelectedDevices()returnsDevice[] | null.
The devices that own this widget's configured variables, handy for labels and names when you render: the
devices: [{ id, label, name }]of theonInfopayload.
A Device has the shape { id, label, name, ... }.
EMIT (useUbidotsActions())
Change the selected device:
setDashboardDevice(deviceId).Select several at once:
setDashboardMultipleDevices([deviceId, ...]).
How to implement: a device picker
import { useUbidots, useUbidotsActions } from '@ubidots/react-html-canvas';
function DevicePicker() {
const { state } = useUbidots();
const devices = state.dashboardDevices ?? []; // the full dashboard list
const { setDashboardDevice } = useUbidotsActions();
return (
<select onChange={(e) => setDashboardDevice(e.target.value)}>
{devices.map((d) => (
<option key={d.id} value={d.id}>{d.label ?? d.name}</option>
))}
</select>
);
}Last updated
Was this helpful?