05 · Make it yours

Until now the lamp's interface was the two buttons the HC3 gives every switch. This chapter adds your own control panel: a switch that turns the automatic mode on and off, and a label that says what the lamp is doing. You'll also give the lamp its first quickApp variable — a value you can change later from the HC3 app without touching the code.

The lamp with a control panel

Save this as sunset_lamp_ui.lua (or use the repo's copy at examples/sunset_lamp_ui.lua):

--%%name:SunsetLamp
--%%type:com.fibaro.binarySwitch
--%%var:auto=true
--%%var:wattage=60

--%%u:{label="status",text="Automatic mode"}
--%%u:{switch="automatic",text="Automatic",value="true",onReleased="toggleAuto"}

function QuickApp:onInit()
  self:debug("Sunset lamp ready,", self:getVariable("wattage"), "W")
  self:checkSun()
  setInterval(function() self:checkSun() end, 60 * 1000)
end

function QuickApp:turnOn()
  self:updateProperty("value", true)
  print("Lamp on")
end

function QuickApp:turnOff()
  self:updateProperty("value", false)
  print("Lamp off")
end

function QuickApp:toggleAuto(e)
  local auto = e.values and e.values[1] == "true"
  self:setVariable("auto", auto)
  self:updateView("status", "text", auto and "Automatic mode" or "Manual mode")
  if auto then self:checkSun() end
end

function QuickApp:checkSun()
  if not self:getVariable("auto") then return end -- manual mode: hands off
  local now = os.date("*t")
  local h, m = fibaro.getValue(1, "sunsetHour"):match("(%d+):(%d+)")
  local sunsetMin = tonumber(h) * 60 + tonumber(m)
  local nowMin = now.hour * 60 + now.min
  if nowMin >= sunsetMin and not self.properties.value then
    self:turnOn()
  elseif nowMin < sunsetMin and self.properties.value then
    self:turnOff()
  end
end

Your own UI: the --%%u lines

The two new header lines describe the panel:

--%%u:{label="status",text="Automatic mode"}
--%%u:{switch="automatic",text="Automatic",value="true",onReleased="toggleAuto"}

Each --%%u line is one row of your QuickApp's own UI:

That's the whole recipe for HC3 interfaces: rows of label, button, switch, slider, select — and the toolbox has multi, tabView, image, space, vertical, collapsible, colorPicker, colorTemperaturePicker and list when you need them — each with a name and the action to call. The HC3 app renders the same rows on the real controller later.

Reacting: toggleAuto

function QuickApp:toggleAuto(e)
  local auto = e.values and e.values[1] == "true"
  self:setVariable("auto", auto)
  self:updateView("status", "text", auto and "Automatic mode" or "Manual mode")
  if auto then self:checkSun() end
end

Two new tricks:

And the guard at the top of checkSun:

if not self:getVariable("auto") then return end

In manual mode the lamp leaves the light alone — the user has taken over. return means "stop this function right here".

Your own icon

A QA's tile icon is the deviceIcon property — an icon id from the controller's library. While you develop, preview a custom icon set straight from a folder: put the icons next to the QA in the upload naming (icon.png, icon0.png + icon100.png for a binarySwitch, …) and point at them:

--%%icons:icons

flua then uses the QA's own id as its deviceIcon and the web view renders and switches the folder's icons with value/state — no HC3, no upload (examples/local-icons.lua is the runnable demo). The directive is preview-only and never travels into a packed .fqa; production icons come from the controller (chapter 06).

QuickApp variables: --%%var

Both header lines starting with --%%var give the lamp variables — named values that live with the QuickApp:

--%%var:auto=true
--%%var:wattage=60

Why a variable and not a property? A QuickApp's properties are fixed by its type: a binarySwitch has value, and that's the schema — you can't invent an auto property for it. (self:updateProperty("value", …) from chapter 03 works because value is in the schema.) Variables, on the other hand, are free-form, and — the real reason auto is one — they survive restarts: when the HC3 restarts the QuickApp — or flua restarts it mid-run — the variables keep their values, so the user's switch choice isn't lost. The --%%var:auto=true line only sets the starting value, the first time. (One flua caveat: a fresh flua sunset_lamp_ui.lua starts the sim from scratch and re-applies the starting values — to persist variables across flua runs too, add --%%db:+.)

If a value does not need to survive a restart, don't give it a variable — a plain Lua variable in the file (or a field on self, like self.lastCheck = os.time()) is simpler and just as good. Variables earn their keep exactly when the value must outlive the QuickApp's run.

Run it and flip the switch

Press F5 with the Flua: Run Current File (UI) configuration, as in chapter 03, and open http://127.0.0.1:8090/.

🚀 flua 0.1.x (Lua 5.4, Python 3.x), mode offline (sim HC3, real network)
flua: UI viewer on http://127.0.0.1:8090/ — open it in a browser
[04.10.2026][21:00:00][DEBUG  ][SunsetLamp5000]: Sunset lamp ready, 60 W
[04.10.2026][21:00:00][DEBUG  ][SunsetLamp5000]: Lamp on

If the lamp didn't turn on: it's daytime where your virtual clock thinks it is (F5 starts at the real time, like chapter 04). Pin the sun with chapter 04's trick instead:

flua --api local --ui 8090 --start '2026/10/4 18:30:00' sunset_lamp_ui.lua

Open the viewer (chapter 03) and press Connect.

✅ You should see the panel from before — On/Off and the state label — and below it, your own rows: the status label and the Automatic switch, sitting ON.

Now flip Automatic off.

✅ You should see the status label change to Manual mode — your toggleAuto action ran, stored auto=false, and updated the label.

And because it's past sunset in the virtual world, flip the lamp off and on with Turn Off / Turn On — manual mode means the lamp won't fight you: checkSun returns immediately and leaves the light exactly where you put it. Flip Automatic back on and watch it take over again.

Press the red square (■) in VS Code's Run toolbar when you're done.

What did we just learn?

Next: the lamp has been living in a simulation this whole time. Time to move it into your real HC3.