• Professional advice
  • Order before 16.30 uur shipped today
  • Always reachable (+31 73 762 0 762)
  • Monday open late

Intro to FIBARO LUA-code

Introduction in LUA programming for the FIBARO Home Center 2 controller

In this short manual you will learn the basics of LUA programming for the Home Center 2 by FIBARO. You will learn the basics of this programming language, the structure of the language and you will make some basic commands.


1.1 Creating a new LUA scene

The version 3.x and higher interface enables a functionality which is LUA language scene programming. 
To create a new scene press 'Add scene'. 

Now you can choose whether you want to create a scene based on 'grafical blocks' or based on LUA coding. Grafical blocks are not in the scope of this manual. Click here for a short video about graphical blocks.

De LUA code for the new scene can be entered in the textfield as shown in the figure below.

LUA coding is a line based programming language. Like other languages the commands are written per line. Each LUA scene starts with a header.

1 -- [ [
2 % % properties
3 % % globals
4 - -] ]

The header is absolutely essential for any scene. Triggers are defined in the '%% properties' heading. The global variables are defined in the '%% globals' heading. Both of these mechanisms will be described in detail later in this manual. 


1.2 Devices

To understand LUA programming it's crucial to know what a device exactly is. Below you will find, what kind of devices are included in LUA. 

Figure 1: Different types of devices in a network


When adding a Z-Wave module to the Home Center 2, it is presented as a specific device type:

  • binary_light – Relay switches, two-state devices, for example FIBARO Relay Switch module
  • dimmable lights – for example Dimmer module oraz external Z-Wave sirens
  • blind – Roller Shutters conntrollers, for example FIBARO Roller Shutter 
  • virtual_device – Virtual Devices created by the user
  • thermostat_setpoint – thermostatic valves, for example Danfoss Living Connect
  • motion_sensor – motion sensors, binary sensors
  • door_sensor – door and window sensors, for example FIBARO Door/Window Sensor
  • smoke_sensor, smoke binary sensors
  • water_sensor, Flood sensor, for example FIBARO Flood Sensor
  • ip_camera – IP cameras 


As shown in figure 2 the devices list also includes HC_user. It is one of the most important changes in the 3.x interface - users have become devices. With this approach, you can program the advanced stage on the individual characteristics of users. This procedure was designed to unify the functionality of Home Center 2 used in scenes. 
In the FIBARO Home Center 2 interface all available devices are characterized by:
  • deviceID – unique device number in Home Center 2 database
  • type – type of the device (for example binary_light or smoke_sensor)
  • properties – (for example Value, dead) - illustrates what the current state of the device is. The device properties are read-only information such as "motion sensor is breached", the "on / off switch is on" and "the shutter is open".
  • actions – (for example turnON(), turnOFF()) - it represents what actual actions can be performed on the device. For example if we have a Relay Switch module, we can turn it ON (turnON() action) or turn it OFF (turnOFF() action). Dimmable lights can be turned on, for an exact lever (for example for 50%), which is represented by action setValue(). 
Hint: If you are not sure what actual actions your devices can execute you can also preview and use them from the Home Center 2 interface (Fig. 2). 


 Figure 2 - Preview of decive properties and  actions


2. Scenes

Using the LUA language allows us to programm any control algorithm. In the next chapters you will find examples and tutorials how to start your first Smart Homes control algorythms. 


Script 1

First, we will create the simplest possible scene, which will be simple switching on device.

To turn ON the device fibaroLcall function is used. This is the most elementary function in Home Center 2 scenes. This function sends a request to perform an action to the device.

The function takes at least two arguments:

  • deviceID - the mentioned device ID
  • actionName - actions you can perform on the device (eg turnOn,
  • TurnOff, SetValue)  

Suppose you have a system on module Relay Switch with 2 deviceID to turn this ON / OFF 'and the need to write the following script 

1 -- [ [
2 % % properties
3 % % globals
4 -- ] ]
6 fibaro:call ( 2 , ' TurnON ' ); -- ON  function activate

Our first simple program consists of a header and a standard scene script. In this code to the device of DeviceID number 2 is send instrucction to turn ON.
To run the scene click START button. 

Save the scene by pressing 'save'

This is the place in the code that will be skipped during the execution of the script - the headers are a special case - this place does not apply to comments. Comments are very useful when writing and analysis of the code, especially when you read it later or another person analyze it. It is good to use comments.

To run the scene it should be saved (SAVE button) and then you can start it up pressing 'START'. Written before the script will be executed once for each time you press the 'START'. In this way you can also run this scene from mobile interfaces. It will turnON the device every time, when you will click RUN button. 


Script 2

In this script we will create the scene that TurnOFF the device. To turn off the device we will use again the same function fibaro:call(). The only difference is that you will put another action in it, which will be 'turnOff'. 

1 - [ [
2 % % properties
3 % % globals
4 -- ] ]
6 fibaro:call ( 2 , ' TurnOFF ' ) -- UIT  functie

After Save you can start the scene by pressing run.


Script 3

In the next script we will create the scene that turns ON the device and turns it OFF after 3 seconds.

For turn on and off instruction we can use fibaro:call() function.

For time delay we will use function calls fibaro:sleep(). This functions delays next instruction for some declared time. For example function fibaro:call(3000) will freeze the instruction for next 3 seconds. The time 3000 is expressed in mili-seconds. 1ms = 1000s. 

 1 - [ [
2 % % properties
3 % % globals
4 -- ] ]
6 fibaro:call ( 2 , ' TurnON ' )          -- module wordt ingeschakeld
7 fibaro:sleep ( 3000 )                    -- wacht 3s
8 fibaro:call ( 2 , ' TurnOFF ' )        --  module wordt uitgeschakeld

As before first upper part of the script is a standard header, the rest of the code is are fibaro:call and fibaro:sleep functions.
In this script scene will turn ON the device with device ID 2, then waits 3 seconds and after that turns OFF the same device.

Hint: As you can see it is very easy to create such delays in LUA. Delays are very useful in smart home systems. The delay time is not limited in FIBARO home automation system. 


Script 4

The basic mechanism of almost every programmable languages is a debugger. FIBARO LUA has the debugger as well. After wiki: "Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a computer program or a piece of electronic hardware, thus making it behave as expected." 

The definition sounds maybe a little bit tricky, but debugging is a verry usefull tool. It helps programmers to control exact places in the code where the script is executed, so it improves program analysis and time of bug detection. You should notice, that in LUA, as in other programmable languages code is executed line by line. In first place the instructions from header and then line by line script code is executed.

Debug function in FIBARO LUA scenes is called fibaro:debug()

This functions print desired logs. This debug logs are printed in debug section of script creator.

Let's create another script that uses simple debug mechanism:  

1 - [ [
2 % % properties
3 % % globals
4 -- ] ]
6 fibaro:debug ( ' Hallo world! ' )

In fact this scene makes nothing beside showing one simple log with "Hello world!" string of letters. In the debug window we will see: 



Script 5

In previous script you familiar with fibaro:debug() function. Let's create more advanced example 

1 - [ [
2 % % properties
3 % % globals
4 -- ] ]
6 fibaro:call ( 2 , ' TurnON ' )                                                           – module wordt ingeschakeld
7 fibaro:debug ( ' ! Module met DeviceID 2 is ingeschakeld ' )
8 fibaro:debug ( ' ik wacht 3 seconden ' )
9 fibaro:Sleep ( 3000 )                                                                    -- wacht 3seconden
10 fibaro:debug ( 'Nu ik zal het module uitschakelen ' )
11 fibaro:call ( 2 , ' TurnOFF ' )                                                       -- schakelt het module
12 fibaro:debug ( ' Het script werd correct uitgevoerd ' )


How does it work?

As we now script is executed line by line. In first please the header code is checked and then script turns on the device. In next step debugger is showing the message Device with DeviceID 2 is Turned ON!' and 'I'm waiting 3 seconds'. Then fibaro:sleep function is starting to count 3000ms. After this time script is executing next line which is fibaro:debug function with 'Now I will turn off the device' message. Then it actually turns of the device by fibaro:call function. In last line debugger prints the message that 'The script was executed correctly'.

Where can we use it?

As you can see above the code is executed line by line and after every executed instruction debugger informs us about currect code execution place. Thank to it if debug will not print the last information 'The script was executed correctly' we can learn that there was some mistake in previous line. This is priceless feedback information for us. In very simple scripts debugger is not so important, becuase the code is short and simple. If something goes wrong we can probably find the bug by ourselves. However in more advanced scripts or in scripts written by someone else finding the bugs in code without debugger would be very difficult and time-consuming. 

Hint :  If you are just starting the experience with Home Automation programming please be sure you will create elegant script from the very beging. Use debug functions and comments. 


Script 6

In this step you will learn how to pass value to a function. As you probably now not every home automation devices works as Turn ON or Turn OFF. There are not only binary switches in your Smart Homes system. There are also other type of devices like dimmers or roller shutter that can have indirect states. There can be instruction like: Open the roller blinds for 50% or turn on the dimmer for 75%. To handle such commands we will use (again) our basic function calls fibaro:call.

fibaro:call sends execution instruction to some device. The arguments that it exepts are deviceID and the action instruction. Dimmer or roller shutter has additional action which is called 'setValue'.

Syntax is based as follows:

fibaro:call(10, 'setValue', '50') 

Let's say we will have following devices in our Smart Home:

  • Relay Switch(with deviceID 2)
  • Dimmer (with deviceID 5)
  • Roller Shutter (with deviceID 10)
Let's create example with more advanced values: 

1  --[[
2  %% properties
3  %% globals
4  --]]
6  fibaro:debug('Start of the program')
7  fibaro:debug('I'm turning on the device with deviceID =2')
8  fibaro:call(2, 'turnOn') –- turnsON the device
9  fibaro:debug('I am waiting 5 seconds')
10  fibaro:sleep(5000) –- waits 5 seconds
11  fibaro:debug('I've waited for 5 seconds, no I will open roller blinds for 50%')
12  fibaro:call(10, 'setValue', '50') –- opens blinds
13  fibaro:debug('I've opened the shutters, no I will turn on the light for 75%')
14  fibaro:debug('I'm waiting 5 seconds')
15  fibaro:sleep(5000) –- waits 5 seconds
16  fibaro:call(5, 'setValue', '75') –- set light
17  fibaro:debug('Light set')
18  fibaro:debug('I'm turning
19  fibaro:call(2, 'turnOff')
20  fibaro:call(5, 'turnOff')
21  fibaro:debug('I'm closing
22  fibaro:call(10, 'turnOff') –- closes the shutter
23  fibaro:debug('Devices was turned off – code was executed correctly') 

As you can see above. You can not only turn on or off devices. You can also send values to them. 


Script 7

In the next step you will lear very important functionality – how to get value of the device. As you know beside perform some action of the device you can also get its properties. For example motion sensor propertie would be if sensor is breached or not breached. Dimmer property would be level of light. Relay Switch property would be if its turned on or off. Of course devices can share many of such properties, but in the begining you will learn only basic ones.

For example:

Let's say we have Relay Switch with deviceID 2. This device type is binary_light, which can have few properties. At the moment the most important for us would be property called "value".

Value is property of the device that stores some exact state of the device. If value = 0 then device is turned OFF, if value=1 then device is turned on.

To get value of the device you will need to use proper function. This function is called fibaro:getValue(deviceID, propertyName).

Now you will learn next basic mechanism of programming languages – the 

variable. In computer programming, a variable is a storage location and an associated symbolic name (an identifier) which contains some known or unknown quantity or information, this information is called a value. So in fact – variable has always three atributes – type, name and value.

To create variable in LUA we need to declare it:

1  local myValue
2  myValue = 10

This command will declare value calls myValue, and in next line we assign value 10 to it.

Now, if we want to use some devices value than we need to assign this value to variable. So we can say, that system will see the value of the device if we assign it to variable.

We can do it as follows:

1 local myValue = fibaro:getValue(2, 'value')

Now, when system will execute function fibaro:getValue, it will assign the value of the device (with DeviceID=2) to variable called myValue.

For turned on relay switch this value will amount 1. 

1  --[[
2  %% properties
3  %% globals
4  --]]
6 local myVariable --declaration of Value 7
8  fibaro:debug('Now I will turn on the Deivce with deviceID=2')
9  fibaro:call(2, 'turnOn') –-turns on the device
10  myVariable = fibaro:getValue(2, 'value')
11  fibaro:debug('myVariable is = ' .. myVariable)
12  fibaro:debug('Now I will turn off the device')
13  fibaro:debug('Waiting for 3 seconds')
14  fibaro:sleep(3000)
16  fibaro:call(2, 'turnOff')
17  fibaro:debug('I've turned off the device')
18  fibaro:sleep(3000)
19  myVariable = fibaro:getValue(2, 'value')
20 fibaro:debug('MyVariable is now = ' .. myVariable) 21
22  fibaro:debug('Now I will turn off deviceID=2')
23  fibaro:call(2, 'turnOn')
24  fibaro:sleep(3000)
25  myVariable = fibaro:getValue(2, 'value')
26  fibaro:debug('Now my variable, again is = ' .. myValue)

In the begining we declared local variable called myVariable. System shows information by function fibaro:debug, and then by function fibaro:call turns on the device. Value of the variable is set for 1. Then script turning off the device and showing the value of variable again. It is equal 0. In final stage the system using the fibaro:call function is showin final value of variable.

Script 8

Now you will lear next element of programming languages which is called IF-THEN conditional statement. Conditional statement is a feauture which performs specyfic action depending on declared condition. For example IF deviceID=3 is turnedON THEN turnOFF deviceID=5.

Every IF-THEN conditional statement has following syntax:

1 if (condition) then

3 instruction
5 end

In the begining we will create simple contional statement. IF deviceID=3 is turnedON THEN turnOFF deviceID=5

1  --[[
2  %% properties
3  %% globals
4  --]]
5  local myVariable –-declaration of variable
7  fibaro:call(2, 'turnOn') –-turnsON device=2
8  myVariable = fibaro:getValue(2, 'value')
10  if (myVariable == '1') then
11  fibaro:call(3, 'turnOn')
12  end 

In first step we declare variable called myVariable. Next system turns on the deviceID = 2. To variable myVariable system assignes value 1. Next system is checking the condition. If condition is true (so value will be = 1) then script will move to next line and turns on the device with deviceID = 3. If condition is false (so value will be = 0) then script will stop the execution of the code.


Script 9

Now we can use previous information to create scene that will turn ON the switch from the door/window sensor.

Let sey we have door window sensor with deviceID 145. Type of such device is door_sensor. It has several properties like batteryLevel, armed), but at this stage, we are only interested in "VALUE".

As we now value gets the state of the device. If sensor is breached (or opened) then value =1, if sensor is not breached or doors are closed. the value =0.

If we want to check what is the value of our sensor we need to check it by fibaro:getValue function.

Such function looks as follows:

1 fibaro:getValue(145, 'value')

If we would like to check if sensor is armed we could use

1 fibaro:getValue(145, 'armed').

Let's try followind script:

1  --[[
2  %% properties
3  %% globals
4  --]]
6 local myVar1 = fibaro:getValue(145, 'value') –-state of the sensor
7  fibaro:debug('value = ' .. myVar1) –-prints value of variable1
8  local myVar2 = fibaro:getValue(2, 'value') –-state of on/off switch
9  fibaro:debug('value = ' .. myVar2) –-prints value of variable2
11 if (myVar1 == '1') then 
13    if (myVar2 == '0') then
14       fibaro:call(2, 'turnOn') 
15    else
16       fibaro:call(2, 'turnOff')
17    end
18 end

The scene will work as follows

1) to myVar1 script assigns value of sensor
2) fibaro:debug prints value of myVar1
3) to myVar2 script assigns value of switch
4) fibaro:debug prints value of myVar2
5) Script checks the condition – if myVar1 = 1, so door window sensor is opened
6) In next step we used next condition statement – if door are opened and switch is turnedOFF then turn it ON or set it to oposite state.

Script 10

All previous scripts befor had one common feature – single time execution. These scripts worked only when we pressed START button. Of course there are scenes that should be triggered only by the button, but mostly the conception of scenes is that they should be triggered automaticly based on other devices states.

To turn on the state automaticly we need to declare TRIGGER. Triggers are events that call the scene for script execution.

Let say we want to create the scene that will turn on the light (deviceID 3) when motion_sensor(deviceID 50) will sens the movement. Script will be simple:

1 local myVariable –declaration of Variable
3  myVariable = fibaro:getValue(50, 'value')
4  if (myVariable == '1') then
5     fibaro:call(3, 'turnOn')
6  end

1) We declare variabble
2) We assign the value of the motion sensor (deviceID 50) to the variable
3) We create condition statement – If sensor is breached then turn on the light.

The question is – how will system now to check the value of the motion sensor everytime?
The answer is – the TRIGGER.

A trigger informs the scene engine, that our important property of the device is changing and scene should be executed.

A trigger has following syntax:

[deviceID] [property]
And we declare it in the header:

1  --[[
2  %% properties
3  50 value
4  %% globals
5  –]]

So we declare trigger for deviceID=50. For now on, everytime if the value of deviceID 50 will change – scene engine will execute the script.

Lets check again previous example with sensor

1  --[[
2  %% properties
3  50 value
4  %% globals
5  –]]
6  local myVariable –declaration of Variable
8  myVariable = fibaro:getValue(50, 'value')
9  if (myVariable == '1') then
10  fibaro:call(3, 'turnOn')
11  end

This scene will work as follows:

1) We declare trigger in the header. The trigger is declared for deviceID=50 for property value. So everytime this value changes – the system is execute the scene.
2) We declare local variable called myVariable. 3) To myVariable we assign value of motion sensor.
3) We create condition statement – If sensor is breached then turn on the light (deviceID=3).

We can also add next line to turn deviceID 3 if the door are closed.

1  --[[
2  %% properties
3  50 value
4  %% globals
5  –-]]
6  local myVariable –declaration of Variable
8  myVariable = fibaro:getValue(50, 'value')
9  if (myVariable == '1') then
10  fibaro:call(3, 'turnOn')
11  else
12  fibaro:call(3, 'turnOff')
13  end 


LUA progamming tips

fibaro:sleep command

With this command it is possible to execute commands after a pause. See the figure in the grafical block 

De scene is triggered by switching on the LED ligthing. After execution the following will happen.
After 2 seconds the wall light changes status and after 20 seconds the diningligth will change statustatus.

What you would expect is the following:

The Dininglight (Eetkamerlamp)  changes status after 20 seconden and 2 seconds later ( 22 seconden after the scene started)  the wall light (wandlamp) will change status.

This is not the case. 


We hope that after reading this LUA tutorial you can make youre own simple z-wave scenes based on LUA in the Home Center 2 by FIBARO. In case you still have questions, you can always contact us via email or phone.

Team ROBBshop

Customer Service Phone

mon-fri: 10am-5pm and sat 10am-3pm


Gerstakker 16 5236 VG Empel Netherlands

Google maps