Skip to content

A chart of recorded power

Load recorded history, then append live readings using the tiledash JavaScript API.

Reviewed demonstration

The chart script in the demo-house widget editor

Prerequisites and limitations

Needs a selected power meter, recorded history and a script-capable browser. History resolution and retention follow the account’s plan. The screenshot is from the demo house; this documentation does not execute the script.

Complete source

Download package (.zip)

The package uses the placeholder your-handle. Replace it with your Community handle before publishing.

tiledash.json

{  "id": "@your-handle/history-chart",  "title": "A chart of recorded power",  "summary": "A reviewed Tiledash script example.",  "version": "1.0.0",  "licence": "MIT",  "tags": [],  "kind": "widget",  "widget": {    "kind": "free",    "match": {      "requires": [],      "optional": []    },    "size": {      "default": [        6,        8      ],      "min": [        3,        4      ],      "max": [        12,        24      ]    },    "settings": [      {        "name": "meter",        "label": "Meter",        "required": true,        "group": "content",        "type": "device",        "requires": {          "all": [            "measure_power"          ],          "any": []        }      }    ],    "script": {      "libraries": [        {          "url": "https://cdn.jsdelivr.net/npm/chart.js@4.4.1/dist/chart.umd.js"        }      ],      "connect": [],      "images": [],      "actions": [],      "context": [        "settings",        "history"      ]    }  }}

template.liquid

{%- 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>

style.css

.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;}

client.js

// 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();});

How it works

The script requests recorded points once, then listens for permitted context changes. A data-script element preserves the chart when the template updates.

Script lifecycle and browser support · Permissions · API reference