Tutorial
Draw a live chart with a script
A widget of your own that draws a meter's power over the last 24 hours with Chart.js, from your recorded history, and adds each new reading as it comes in.
- Takes
- About 10 minutes
- Steps
- 7
- First
- Make your own widget
What you end up with

Start an empty widget
In the sidebar, choose Workshop, then New → Widget and Start empty. Name it Power chart and choose Start editing. The editor opens.


Add its setting
The widget has one setting: the meter it draws, any device that measures power. Open the Settings tab, choose JSON, and paste over what’s there:
[ { "type": "device", "name": "meter", "label": "Meter", "requires": { "all": ["measure_power"] } }]The editor says the setting is valid, and the tab reads Settings · 1.


Add the template and style
The template shows the meter’s name and its power now, and leaves an empty part for the chart.
data-scriptmarks that part as the script’s: the template leaves what the script draws in it alone. On the Template tab, paste:{%- assign meter = settings.meter -%}<div class="tile"> <p class="name">{{ meter.name }}</p> <p class="now">{{ meter.cap.measure_power.value | round }} W</p> <div class="chart" data-script></div></div>

The style puts the chart under the number and lets it fill the rest of the tile. On the Style tab, paste:
.tile { display: flex; flex-direction: column; height: 100%;}p { margin: 0;}.name { font: 600 13px var(--hd-font-ui); color: var(--hd-text-muted);}.now { font: var(--hd-number-weight) 24px / 1.2 var(--hd-font-display); color: var(--hd-text);}.chart { position: relative; flex: 1; min-height: 0;}.chart canvas { position: absolute; inset: 0;}The editor says No problems with any demo, and the preview shows a made-up meter with its power. A made-up meter has no history to draw, so for Meter above the preview, choose your own, such as P1 meter.


Add the script
The script draws the chart. Open the Script tab and choose Add a script.


The script editor opens with a small example. Paste over it:
// The meter's power over the last 24 hours, with a point added each time it changes.const DAY = 24 * 60 * 60 * 1000;const part = tiledash.root.querySelector('.chart');const canvas = document.createElement('canvas');part.append(canvas);// The theme's colours and font, from its CSS variables.const theme = (name) => getComputedStyle(part).getPropertyValue(name);Chart.defaults.font.family = theme('--hd-font-ui');Chart.defaults.color = theme('--hd-text-muted');// The theme's accent, see-through for the area under the line.function accent(alpha) { const pixel = document.createElement('canvas').getContext('2d'); pixel.fillStyle = theme('--hd-accent'); pixel.fillRect(0, 0, 1, 1); const [r, g, b] = pixel.getImageData(0, 0, 1, 1).data; return `rgba(${r}, ${g}, ${b}, ${alpha})`;}const hour = (time) => new Date(time).toLocaleTimeString(tiledash.language, { hour: '2-digit', minute: '2-digit' });const chart = new Chart(canvas, { type: 'line', data: { datasets: [ { data: [], fill: 'start', tension: 0.35, pointRadius: 0, borderColor: accent(1), backgroundColor: accent(0.25), }, ], }, options: { maintainAspectRatio: false, animation: false, parsing: false, plugins: { legend: { display: false } }, scales: { x: { type: 'linear', ticks: { stepSize: DAY / 4, maxRotation: 0, callback: hour }, grid: { display: false }, }, y: { ticks: { maxTicksLimit: 3 } }, }, },});let meter = null;let points = null;tiledash.on('context', async ({ settings }) => { const power = settings?.meter?.cap?.measure_power?.value; if (typeof power !== 'number') return; // A new meter: start with its recorded history. if (settings.meter.id !== meter) { meter = settings.meter.id; points = tiledash .history({ device: meter, capability: 'measure_power', from: Date.now() - DAY }) .then((history) => (history?.points ?? []).map(([x, y]) => ({ x, y }))) .catch(() => []); } const shown = await points; shown.push({ x: Date.now(), y: power }); // The last 24 hours along the bottom, ending now. Object.assign(chart.options.scales.x, { min: Date.now() - DAY, max: Date.now() }); chart.data.datasets[0].data = shown; chart.update();});It makes a canvas in the chart part and draws it with Chart.js, in the theme’s accent colour. It asks for the meter’s recorded history of the last 24 hours once, then adds a point each time the power changes.


Allow Chart.js and the history
A script may do nothing until you allow it. Open the Permissions tab, choose Add a library and type the address of Chart.js:
https://cdn.jsdelivr.net/npm/[email protected]/dist/chart.umd.min.jsWhat it may do now says it loads code from cdn.jsdelivr.net.


Scroll down to What it is given. Untick This device, as the widget has no device of its own, and tick Its settings, including what you type in them and The recorded history of the devices and readings it is set to.


Save it
Choose Save at the bottom. It says Saved. The preview runs a script only with the permissions you saved, so the chart shows now: your meter’s last 24 hours.




Add it to a dashboard
Open your dashboard and choose Add a widget. On the My own tab, tick Power chart and choose Add widget. The tile’s settings open by themselves, as it needs a meter: for Meter, choose your energy meter, such as P1 meter, and choose Save.




Your chart is on your dashboard
Choose Done at the top right. The tile draws your meter’s last 24 hours, and a new point each time its power changes.


Next: everything a script can do, and what it may see, is in Scripts. To make the chart your own, add buttons for other periods, or read recorded history of another reading.