> ## Documentation Index
> Fetch the complete documentation index at: https://docs.hyperwisor.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Script Examples

> Common scripting patterns built from the Script API.

Small, composable patterns using the [Script API](/script/overview) globals.

## React to a widget change

```js theme={null}
widget.on('brightnessSlider', 'change', (value) => {
  widget.setText('brightnessLabel', `Brightness: ${value}%`);
});
```

## Send a command to a device

```js theme={null}
widget.on('fanSwitch', 'toggle', (isOn) => {
  ws.send(context.deviceId, { fan: isOn ? 1 : 0 });
});
```

## React to incoming device data

```js theme={null}
const off = ws.onMessage((msg) => {
  if (typeof msg.temperature === 'number') {
    widget.setValue('tempGauge', msg.temperature);
  }
});
```

## Remember a user's choice

```js theme={null}
const saved = storage.get('view');
if (saved) widget.setValue('viewSelector', saved);

widget.on('viewSelector', 'change', (v) => storage.set('view', v));
```

## Render the latest database record

```js theme={null}
const rows = await db.query('readings', {}, {
  limit: 1, orderBy: 'created_at', ascending: false
});
if (rows.length) {
  widget.setText('lastReading', `${rows[0].temperature} °C`);
}
```

## Responsive UI

```js theme={null}
if (context.device.isMobile) {
  widget.hide('detailPanel');
}
```

## Periodic refresh

```js theme={null}
const id = setInterval(async () => {
  const rows = await db.query('status', {}, { limit: 1 });
  if (rows.length) widget.setText('statusLabel', rows[0].state);
}, 5000);
```

<Card title="API overview" icon="code" href="/script/overview">
  All the globals these examples use.
</Card>
