Scripts
A template draws with Liquid and CSS, and that covers most widgets.
For what it can’t do, such as a chart library, a canvas or an outside service, a widget can have a script: its own JavaScript, in a file called client.js.
Use a script only for that part. Everything else stays in the template, which also draws wherever a script doesn’t run.
Where it runs
A script runs in a sealed-off frame on its own website, tiledashusercontent.com, not in the Tiledash app.
From there it can’t reach the app, your Homey, your account, cookies or other tiles.
It sees only what its widget’s permissions give it, and talks to the tile through tiledash.
Scripts run on dashboards, and in the widget editor’s preview of a widget of yours. They don’t run:
- on an e-ink screen, the lite pages or a picture the server draws;
- on a Community item’s page or in the install dialog: nothing runs before you install it;
- in a browser that can’t keep the frame apart from the page, such as Safari or anything on an iPhone or iPad, unless the page holds nothing worth reading. A shared link or paired screen whose dashboard shows no people, Timeline, calendars, cameras or floor plans still runs them.
Where a script doesn’t run, the tile draws the same template in the app, without the script. Make it worth seeing there too.
The template, the style and the script
A widget with a script is a frame over its whole tile. The frame draws the widget’s template and style, as the tile would, and then starts the script. So the markup goes in the template and the CSS in the style, and the script works on them.
The template draws in the frame with only what the script is given (see What it is given).
A part it isn’t given is empty there, so {{ device.name }} needs device.
The script finds its elements in tiledash.root, the tile’s own root, not in document:
<div class="name">{{ device.name }}</div><div class="wheel" data-script></div>const wheel = tiledash.root.querySelector('.wheel');When a reading changes, the template draws again and only what changed is updated.
An element with data-script belongs to the script: it, and whatever the script put in it, stays as it is.
Build what a library draws inside one. Anything the script adds elsewhere is gone after the next change.
tiledash.on('render', …) runs after every time the template is drawn.
Don’t write <hd-frame> in a widget with a script: the widget is its frame.
Components in the frame
The components that draw from the tile’s own readings work in the frame too: icons, values, toggles, sliders, rings, gauges, bulbs, batteries, weather icons, price bars and the like.
Each is loaded the first time the tile has one, so a tile without any loads none.
To work on one the script added itself, wait for it with await customElements.whenDefined('hd-gauge').
Charts (hd-chart) and history (hd-history) draw in the frame when the script asks for history (see below).
Components that need more than the readings show an empty box in the frame: sparklines, maps, floor plans, cameras, pictures, slideshows, 3D models, the keypad and widgets in widgets.
Draw those in the script, or leave them to a widget without one.
Taps
In the frame, the template’s tap handlers (data-hd-on-…) don’t run, and a control bound with bind shows its value but changes nothing.
The script listens instead, to a click or a control’s hd-change event, and calls tiledash.call:
tiledash.root .querySelector('hd-toggle') .addEventListener('hd-change', () => tiledash.call('toggle'));Where the script doesn’t run, the same template draws in the app with everything the tile knows, and its handlers work as usual.
Talking to the tile
The frame has one object, tiledash. The widget editor’s Syntax panel has every part of it, and its Script tab completes them as you type.
| Part | What it does |
|---|---|
tiledash.on(event, handler) | Runs handler at once and on every change. event is context (what the tile knows), size, theme or render (the template was drawn again). |
tiledash.root | The tile’s root, with the template’s elements and the widget’s style. |
tiledash.call(action, …args) | Does an action the widget may do, on the tile’s own device and what its settings picked. Returns a promise. |
tiledash.setState(key, value) | Sets a value the template reads as state.<key>, and draws the template again. |
tiledash.storage | Small values kept for this tile on this screen: get, set, remove and keys. |
tiledash.history(request) | Recorded history of a reading the tile is set to, with history declared. See Recorded history. |
tiledash.canAct | Whether this page may run actions at all. A link that may only look can’t, so hide your buttons when it’s false. |
tiledash.language | The viewer’s language, such as nl or en. |
// The template has <span class="power" data-script></span>.const label = tiledash.root.querySelector('.power');tiledash.on('context', (context) => { label.textContent = context.cap?.measure_power?.text ?? '';});The theme’s colours and fonts are set on the frame’s page as CSS variables, such as --hd-accent and --hd-text, so the style and the script can match the tile.
Recorded history
tiledash.history gives the readings Tiledash recorded, so a chart can start with the last hours or days instead of empty.
const history = await tiledash.history({ capability: 'measure_power', from: Date.now() - 6 * 60 * 60 * 1000, // the last 6 hours});// history.points is [[time, value], …]; null when the reading isn't recorded.deviceis the tile’s own device unless you name another;tois now unless you set it.resolutionisauto(about a thousand points),raw,1m,5m,15m,1hor1d;aggregateisavg,min,max,last, ordeltafor meters that only count up.- It reaches back as far as your plan keeps;
limitedFromsays where that starts. - Read it once when the script starts, then add the live readings
contextbrings. A frame may read 10 times in 10 seconds.
Actions
An action runs only right after a press inside the frame: a click or a key on the script’s own button.
A script can’t switch anything by itself, and a press elsewhere on the dashboard doesn’t count.
Before a script changes a lock, an alarm or a garage door, the app asks you first, whatever the script showed.
What a script says with toast or confirm is shown with its widget’s name, never as the app’s own words.
What it may do: script
A widget’s script is allowed nothing until its manifest says so.
The editor’s Permissions tab sets this; in a package folder it is the script part of tiledash.json.
"script": { "libraries": [{ "url": "https://cdn.jsdelivr.net/npm/[email protected]/dist/chart.umd.js" }], "connect": ["api.example.com"], "images": ["images.example.com"], "actions": ["toggle"], "context": ["device", "cap", "settings"]}| Field | What it is |
|---|---|
libraries | Outside code loaded before client.js, in this order. At most 8. |
connect | Hosts the script may reach itself, with fetch, a websocket or a picture. At most 10. |
images | Hosts the template’s pictures come from (hd-image src, hd-slideshow, hd-house-side). At most 10. |
actions | The actions tiledash.call may do: toggle, set, increment, startFlow, setMood, openHistory, openDevice, openCamera, openDashboard, confirm and toast. |
context | The parts of what the tile knows that the script is given. |
A host is a full public name in lower case, such as api.example.com: no https://, port, path or wildcard, and nothing on your home network.
Every address written in client.js must be on one of these hosts, or the check refuses it.
Libraries
A library is a full https address, served as JavaScript that any site may load.
On jsDelivr, unpkg and cdnjs it must name an exact version, such as [email protected], so it can’t change under you.
Each is at most 1 MB.
When you save or publish, Tiledash fetches each library once and keeps its hash. The frame then loads only those exact bytes. Tiledash also asks public safety databases which package and version it is and whether anything is known about it:
- a library known as malware is refused;
- a known security problem is shown to anyone who installs the widget;
- a library on another host than jsDelivr, unpkg or cdnjs is shown as Unknown source.
What it is given
| Part | What the script gets |
|---|---|
device | The tile’s device: its id, name, class, zone and whether it is available. |
cap | The device’s readings, as the template has them in cap. |
settings | What the person picked in the tile’s settings, and the values they set. See below. |
state | The tile’s state, as setState and the template set it. |
weather | The weather, as the template has it. |
prices | The electricity prices. |
energy | The house’s energy. |
shape | The tile’s shape, as the template has it. |
history | Recorded history on request (tiledash.history), of the readings it is set to. |
With settings, the script gets what the person picks, and nothing those choices don’t point at:
- devices, one or a list, with their readings, and flows, moods and dashboards. A list set to Every device that fits is every device that fits, also ones added later, and the script may act on each;
- a room, with the rooms inside it, its devices and their readings, and its totals;
- a person: their name, initials, and whether they are home or asleep;
- calendars and their events. Left on All my calendars, that is every calendar, also ones added later;
- the house’s energy and values, a floor plan’s rooms, models;
- numbers, text, choices, colours, icons and so on.
A PIN setting and the dashboard’s tiles stay the template’s.
A script is never given people, rooms or calendars the tile wasn’t given, the Timeline, cameras, other devices, taps, the dashboard or your account.
It can act only on the tile’s own device and on the devices, flows, moods and dashboards its settings picked, including a picked room’s devices.
With history it may read the recorded history of the tile’s own device, of a device set in one of its device settings (not a list on Every device that fits), and of a reading picked in a capability, value or readings setting (only that reading). Never a room’s devices or the Homey’s energy and prices.
Ask only for what the script needs.
Whatever it is given can leave the house through connect, and that includes what someone types in its settings, such as a key for an outside service.
Who accepts what
Your own widget. What you set in the Permissions tab takes effect when you save. When an AI assistant adds to it, the extra waits until you accept it in the editor; the script runs without it until then.
From the Community. A widget that runs code shows Runs code on its card and page, with what its code may do. Installing it asks you to tick that you understand what it loads and may do. An update whose script may do more waits for you, even with automatic updates on, and so does a script an assistant installed for you: the tile says so, and Review takes you there. Nothing from outside in the Community’s filters hides every widget that runs code.
Try it
Draw a live chart with a script builds a widget with Chart.js and recorded history, step by step.
Price curve by Tiledash, in the Community, draws today’s prices on a canvas with a few lines of script. Its template draws them as bars inside a data-script part, which the script swaps for its canvas, so the bars are what you see where the script doesn’t run. Install it and make a copy to start from.