Taps and handlers
Make parts of a widget do something when they are tapped, held or changed, or let whoever uses the widget choose what a tap does.
A widget that only shows values is a good start. A widget that does something when you tap it is a control panel. There are two ways to make a part respond. You can fix what it does yourself with a handler, or you can mark it and let each tile’s owner choose.
Handlers
A handler is a short line attached to an element by an attribute whose name says when it runs:
| Attribute | Runs when |
|---|---|
data-hd-on-click | The element is tapped or clicked, or Enter or Space is pressed on a button. |
data-hd-on-dblclick | It is tapped twice quickly. |
data-hd-on-longpress | A finger or the mouse stays on it for half a second. |
data-hd-on-change | A control inside it, such as hd-slider or hd-toggle, changes. |
data-hd-on-unlock | The right PIN is typed on an hd-keypad. |
<button data-hd-on-click="toggle('onoff')">Switch</button><button data-hd-on-longpress="openDevice()">Details</button>The line is written in the same small language as expressions, but it calls actions. It is read by a parser and carried out by Tiledash. It is never run as JavaScript, so it can only do what the actions below allow.
Inline onclick attributes are always removed.
Only these five data-hd-on- names exist, and the editor tells you if a handler can’t be read.
Actions
An action is something a handler can do. Most of them take the device’s reading by its id, in quotes:
| Group | Actions |
|---|---|
| Devices | toggle, set, increment |
| Flows and moods | startFlow, setMood |
| Variables | setVariable, toggleVariable, addToVariable |
| Web requests | sendRequest |
| Open | openHistory, openDevice, openCamera, navigate, openDashboard |
| This widget | setState, toggleState, runTap, openPopup, closePopup |
| Ask and tell | confirm, toast |
<button data-hd-on-click="toggle('onoff')">Switch</button><button data-hd-on-click="set('dim', 0.5)">Half</button><button data-hd-on-click="increment('target_temperature', 0.5)">Warmer</button><button data-hd-on-click="openHistory('measure_power', '24h')">Power</button>The device defaults to the tile’s own device.
To act on another device, give its id as the last argument.
Devices come in through settings: a device setting called charger gives settings.charger.id.
<button data-hd-on-click="set('evcharger_charging', false, settings.charger.id)">Stop</button>openDevice() shows the device’s controls and readings over the page as a popup; openDevice(settings.lock.id, 'side') as a side panel, and 'page' on the Devices page.
On a wall panel or a share link it always opens over the page.
A Homey Logic variable is changed by its id: setVariable(id, value) with a value of its type, toggleVariable(id) for a yes/no one, and addToVariable(id, amount) for a number (a negative amount takes away).
A value setting that reads a variable gives its id as settings.<name>.variableId.
Changing variables needs a permission of its own; without it a toast says to log in again and allow it.
<button data-hd-on-click="toggleVariable(settings.guests.variableId)">Guests</button><button data-hd-on-click="addToVariable(settings.setpoint.variableId, 0.5)">Warmer</button>A web request the tile’s owner set on a tap is sent with sendRequest(key), its key read from taps (taps.doorTap.key, or an entry’s tap.key).
Your widget never sees the address, and only requests the owner stored on what the page shows are sent, from the browser, without reading the reply.
Most widgets don’t need it: a part marked with a tap setting sends it by itself.
The actions reference lists every action with its arguments, and the Actions panel in the editor offers each as a snippet to put at the cursor.
Checked before they run
Whatever a handler asks for is checked before it reaches Homey:
- The device, flow or mood must be on this Homey.
- The reading must exist on the device, be changeable, and the device must be available.
- The value must fit: on or off for a switch, a number between the reading’s minimum and maximum (rounded to its step), or one of the choices of an enum.
A refused action changes nothing and says why in a message. A change that is allowed shows on the tile at once, and goes back if Homey refuses it.
Several steps, and asking first
Separate steps with a semicolon. A handler can run up to eight actions each time.
<button data-hd-on-click="startFlow(settings.boost.id); toast('Boost started')">Boost</button>confirm asks a yes or no question.
Joined to the next action with &&, that action runs only after a yes:
<button data-hd-on-click="confirm('Open the garage door?') && set('garagedoor_closed', false)"> Open the garage</button>A message passed to confirm or toast can be up to 200 characters.
What a handler can read
A handler can read these names and nothing else: device, cap, settings, devices, state, pending, item, flow, mood and, for a change handler, event.
event.value is the new value of the control that changed.
<hd-slider value="{{ cap.dim.value }}" data-hd-on-change="set('dim', event.value)"></hd-slider>Handlers don’t see now, prices, energy or other names of the template.
If a handler needs a value from the template, put it in the handler as text, with the quote filter so that it stays one piece of text:
{% for id in settings.shown %} <button data-hd-on-click="openHistory({{ id | quote }})">{{ cap[id].title }}</button>{% endfor %}A handler can be 500 characters long, with up to 120 parts.
Which handler runs
A tap goes to the innermost element that has a handler.
A control on the way (a button, a slider, a toggle, a dial, a field, or an element with a role such as button or tab) takes the tap for itself, and the handlers of the parts around it don’t run, so a slider inside a tappable tile doesn’t also trigger the tile.
The same goes for holding it and tapping it twice: a control keeps all of its touches.
A held press runs data-hd-on-longpress and swallows the click that follows.
While it is held, a ring fills round the finger, so the person holding knows something is coming.
A press that moves more than a few pixels is a scroll and not a long press.
With a mouse, a right-click holds too; with the keyboard, the context-menu key or Shift+F10 holds the focused part.
A double click fires two clicks first.
A part whose owner chose a double tap is different: there a tap waits a quarter of a second for a second one, so two quick taps run only the double tap.
So does an element with a data-hd-on-dblclick handler inside a tappable tile or part, such as a value with its own double tap: a tap on it runs its data-hd-on-click, or the tap of the part round it, once the quarter second has passed.
Tapping something else meanwhile runs the waiting tap at once.
Keeping state
A widget can remember small things for itself, such as which part is open.
setState stores a value under a name and draws the widget again, and toggleState flips a name between true and false.
The template reads them as state.<name>.
<button data-hd-on-click="toggleState('details')">Details</button>{% if state.details %} <p>Room: {{ cap.measure_temperature.value | temp: 1 }}</p>{% endif %}A name starts with a letter and continues with letters, digits and underscores, up to 32 characters.
A value is text, a number, true, false or null, and a widget can keep up to 32 of them.
State belongs to one tile on one screen and survives a reload of that screen.
Give setState a time in milliseconds, from 100 to 60,000, and the value goes back to null after that long and is not remembered:
<button data-hd-on-click="setState('flash', true, 2000)">Highlight for two seconds</button>Showing that something is on its way
While a command to the tile’s device is waiting for an answer, it is in pending, by reading.
pending.target_temperature is empty most of the time and holds { value } while a change is on its way, so a tile can say so.
This thermostat tile uses all of this: two buttons that change the target by half a degree, a tap on the number that toggles a detail line, and a word that says Sending while a change waits. The preview’s taps don’t reach a real Homey.
{% assign target = cap.target_temperature %}<div class="steps"> <button class="step" aria-label="Cooler" data-hd-on-click="increment('target_temperature', -0.5)">−</button> <button class="value" data-hd-on-click="toggleState('details')" aria-expanded="{% if state.details %}true{% else %}false{% endif %}"> <span class="big">{{ target.value | decimals: 1 }}°</span> <span class="hint">{% if pending.target_temperature %}Sending…{% else %}Target{% endif %}</span> </button> <button class="step" aria-label="Warmer" data-hd-on-click="increment('target_temperature', 0.5)">+</button></div>{% if state.details %} <p class="detail">Room: {{ cap.measure_temperature.value | temp: 1 }}</p>{% endif %}A popup of your own
A small tile has room for little, so it can open a popup with more.
Write the popup in the same template, inside a <hd-popup id="…"> block beside the tile’s markup, and open it with openPopup('id'):
<hd-popup id="scenes"> <hd-slider bind="{{ cap.dim }}" label="Brightness"></hd-slider> <button data-hd-on-click="set('dim', 0.4); set('onoff', true)">Evening</button> <button data-hd-on-click="set('dim', 0.1); set('onoff', true)">Night</button> <button data-hd-on-click="closePopup()">Done</button></hd-popup><button data-hd-on-click="openPopup('scenes')">{{ device.name }}</button>The tile never shows a <hd-popup> block, and a popup shows only its own.
Both are drawn from the one template, so the popup has the tile’s device, settings and style, and state is shared: a setState in the popup changes the tile too.
While the popup is drawn, popup holds its id; on the tile it is empty.
openPopup('scenes', 'side') opens it as a side panel instead, which comes up from the bottom on a phone.
Without an id, openPopup() opens the block called main.
An id starts with a letter and has at most 40 letters, digits, - or _; a template can have several popups, but not one inside another.
closePopup(), the close button, a tap outside it or Esc closes it.
The popup takes the tile’s title, or its device’s name, and works on dashboards, wall panels and share links.
A script opens one the same way, with tiledash.openPopup('scenes') and tiledash.closePopup().
The script itself doesn’t run in the popup: only the template’s block is drawn there.
Taps the tile’s owner chooses
Sometimes you don’t know what a tap should do, because it depends on the home. Mark the part instead, and each tile’s owner picks what it does in the tile’s settings.
First declare a setting of the kind A tap in the editor’s Settings tab, or in its JSON view:
{ "type": "tap", "name": "lampTap", "label": "The lamp", "default": { "type": "toggle" }, "hold": { "type": "device" }}Then mark the part with the setting’s name:
<div class="lamp" data-hd-tap="lampTap">…</div>On a tile’s Tap tab, the part now appears as The lamp, with what tapping, holding and tapping it twice do.
Each has the same picker: nothing, switch it, show its history over a range, open the device (or another one), open its camera, open a dashboard as a page, a popup, a side panel or full screen, start a flow, set a mood, set a Logic variable (flip a yes/no one, set one, or add to or take from a number, asking first if you like), or send a web request (a GET or a POST to an address the owner types).
Tap is always shown; Hold and Double tap show once they do something, else the owner adds them.
Until the owner picks something, your default (the tap), hold and double apply; left out, they do nothing.
A default can be nothing, switch, history, open the device or the camera, or what your widget does itself, because those work on any Homey.
A dashboard, a flow or a mood belongs to one Homey, so only the tile’s owner can choose one.
What your widget does itself
Some parts already do something of their own: a lock that locks and unlocks, a button that starts its flow.
Keep that as a data-hd-on-click on the part, and name it in the setting’s widgetAction:
{ "type": "tap", "name": "lockTap", "label": "The lock", "widgetAction": "Lock or unlock", "default": { "type": "widget" }, "hold": { "type": "device" }}<button class="lock" data-hd-tap="lockTap" data-hd-on-click="set('locked', true)">…</button>The picker offers it as Lock or unlock, so the owner can leave it on the tap, move it to a hold, or put something else there. Your handler runs only when that action is chosen.
A part marked data-hd-longpress instead is only held, with one action; parts marked data-hd-tap have a hold of their own now, so new widgets don’t need it.
A tap setting can say more about the part:
{ "type": "tap", "name": "powerTap", "label": "Tapping the power", "capability": "measure_power", "of": "charger", "default": { "type": "history", "range": "24h" }, "allow": ["none", "history", "device"]}capabilityis the reading the part shows, which switch and history use.ofis the name of a device setting whose device it belongs to. Left out, it is the tile’s own device.allowlimits what the owner can pick. Nothing is always allowed.
A part marked data-hd-tap that does something becomes a button for the keyboard and for screen readers, unless it already is one or has its own role.
A part whose tap is set to nothing stays inert, and a tap on it goes to whatever is around it.
Inside a loop, name the reading each part is for with data-hd-capability:
{% for id in settings.shown %} <div class="reading" data-hd-tap="readingTap" data-hd-capability="{{ id }}">{{ cap[id].text }}</div>{% endfor %}taps.lampTap is in the template only while one of its gestures does something, so you can show a hint only when it is useful.
It reads as the tap’s action (taps.lampTap.type), with taps.lampTap.hold and taps.lampTap.double beside it:
{% if taps.lampTap.type %}<span class="hint">Tap to switch</span>{% endif %}{% if taps.lampTap.hold %}<span class="hint">Hold for more</span>{% endif %}A tap that opens or runs something named has its name and icon too: the dashboard, device, flow, mood or variable it points at, or the name the owner gave a web request (with a globe as its icon).
The Button uses them as its label and icon when it has none of its own:
<hd-icon name="{{ settings.icon | default: taps.tileTap.icon }}"></hd-icon>{{ settings.label | default: taps.tileTap.name }}A part is either fixed by you or chosen by its owner, never both.
A part with data-hd-tap and a data-hd-on-longpress or data-hd-on-dblclick is an error when you save, and so is one with data-hd-on-click unless its setting offers widget, and a marker that names no tap setting.
A handler that wants to do what a tap setting says can call runTap('lampTap').
Controls that change a reading
The hd-slider, hd-toggle, hd-dial, hd-stepper, hd-segmented and hd-color-wheel controls can be bound to a reading.
bind takes the whole reading, without .value:
<hd-slider bind="{{ cap.dim }}" label="Brightness"></hd-slider><hd-toggle bind="{{ settings.lamp.cap.onoff }}" label="Switch"></hd-toggle><hd-stepper bind="{{ settings.thermostat.cap.target_temperature }}"></hd-stepper><hd-segmented bind="{{ settings.thermostat.cap.thermostat_mode }}"></hd-segmented>hd-stepper puts − and + beside the value. Held, a button keeps stepping, and the steps go to Homey as one change once you let go.
hd-segmented shows a few choices side by side. Bound to a reading with choices, such as a thermostat’s mode, it takes them from the reading; otherwise options lists them: options="eco:Eco,comfort:Comfort".
A bound control shows the reading’s value, takes its minimum, maximum and step from the reading, and sends changes back. It shows a change at once, goes back if Homey refuses it, and checks the range. A control for a device that is unavailable shows its last value, disabled.
Writing a whole reading into an attribute, {{ cap.dim }}, renders a live reference to it, so any attribute that takes a value can follow any device.
The components reference says which attributes do.
When a change should do something else, use the explicit form.
value says what the control shows, and data-hd-on-change decides what a change does:
<hd-slider value="{{ cap.dim.value }}" data-hd-on-change="set('dim', event.value)"></hd-slider>A slider that is the whole tile
With fill, a slider fills the box it sits in and draws your content on top.
Its value is a wash that rises from the bottom, or grows from the left in a wide tile, with a grip on its edge.
It is dragged anywhere, by how far you move, so touching it never makes it jump:
<div class="tile"> <hd-slider fill bind="{{ settings.lamp.cap.dim }}" tone="lit"> <hd-icon name="device.light"></hd-icon> <b>{{ settings.lamp.name }}</b> </hd-slider></div>Give the box around it position: relative and rounded corners, and the slider follows them.
A tap without dragging slides it to the spot you tapped.
With tap="tile", a tap is the tile’s own instead, for example switching the lamp, and only a drag changes the value.
direction="up" or direction="right" picks the way it grows.
Its colours are CSS properties, set in the widget’s style: any colour or gradient.
hd-slider { --hd-slider-fill: linear-gradient(to top, #7c3aed88, #f9731688, #fde04799); --hd-slider-tone: #fde047; /* the edge and its grip */}hd-stepper { --hd-stepper-button: var(--hd-cool); --hd-stepper-button-text: white;}A gradient is laid across the whole tile and uncovered as the value grows.
--hd-slider-track colours the rest.
A keypad
hd-keypad asks for your account’s PIN, set under Settings › Keypads.
The digits go to Tiledash’s server and nowhere else: your template never sees them.
When the PIN is right, data-hd-on-unlock runs:
<hd-keypad label="Disarm" data-hd-on-unlock="set('homealarm_state', 'disarmed', settings.alarm.id)"></hd-keypad>A wrong PIN shakes, and after five wrong tries in a row every keypad of your account waits five minutes.
shuffle puts the digits in a new order each time, so smudges and onlookers give nothing away.
pins lets other PINs unlock it too, by id and comma-separated. Your own PIN always does. Leave the ids to the tile’s owner with a pins setting:
<hd-keypad label="Open" pins="{{ settings.who }}" data-hd-on-unlock="…"></hd-keypad>A light’s colour
hd-color-wheel sets a light’s colour on a wheel, or its white on a disk from cool to warm.
Bind it to the light’s colour, and it reads the light’s saturation, white and mode itself:
<hd-color-wheel bind="{{ settings.light.cap.light_hue }}"></hd-color-wheel>For a light with whites only, bind light_temperature instead.
A light with both shows Colour and White above the wheel.
The colour goes to Homey when you let go.
With live, it also goes while you drag, a few times a second, so the lamp follows your finger.
A light’s colour is also in its popup: openDevice() opens the light with a colour wheel that changes it as you drag.
That way a small tile can have a colour button and leave the wheel to the popup:
<button data-hd-on-click="openDevice()">{{ device.name }}</button>Below the wheel are your favourite colours, the same on every dashboard: up to 15.
Tap one to set it, + keeps the colour shown, and holding one (or right-clicking it) offers to remove it.
On a paired screen or a share link they are shown but can’t be changed.
favourites="none" leaves them out, for a small tile.
Trying it out
The editor lists every handler call in a log under the preview: when it ran, which part, what it ran and what happened. By default, taps in the preview don’t reach your Homey. Turn on Send taps to my Homey when you preview with one of your own devices and want them to.