(HC2) Getting Started With Lua: Description
(HC2) Getting Started With Lua: Description
(https://manuals.!baro.com/knowledge-
MANUALS (HTTPS://MANUALS.FIBARO.COM) CATEGORIES (HTTPS://MANUALS.FIBARO.COM/KNOWLEDGE-BASE/)
base/)FAQ (HTTPS://MANUALS.FIBARO.COM/FAQ/)
TABLE OF CONTENTS
1. Description
2.1. Introduction
2.2. Devices
2.3. Scenes
2.4. Debugging
Description !
You’re already an expert in creating Magic and Block scenes? Do you want to start making scenes in Lua? This article will show you how to do it!
As you can see some part of the code is already written. Each scene written in Lua should always have a form:
1 --[[
2 %% properties
3 %% events
4 %% globals
5 --]]
6
7
8
This part is called header. The header is absolutely essential for any scene and is used to de!ne what actions will automatically run the scene. Under the heading %% prop-
erties are de!ned devices’ properties- every change in their value will trigger the scene, under the heading %% events you can !nd events that will trigger the scene and un-
der the heading %% globals are de!ned global variables – every change in their value will trigger the scene.
The script can be typed in the code editing window. In Lua, type the commands, as in other programming languages in the lines below each other.
If you already know other programming languages, this line does not terminate any sign, although Lua ignores the semicolon at the end of the line, so you do not have to get
rid of habits acquired from other programming languages requiring that symbol.
Devices !
In the Home Center 2 interface, all available devices are characterized by:
https://manuals.fibaro.com/knowledge-base-browse/getting-started-with-lua/ Page 1 of 6
[HC2] Getting started with Lua | FIBARO Manuals 03/08/2020, 10:11
1 – Home Center
2 – Admin
3 – Weather
For example, if we have a Relay Switch module, we can Turn it ON (turnOn action) or Turn it OFF (turnO" action).
If we have dimmable light we can turn it on, for some exact level (for example for 50%), which is represented by action setValue().
Scenes !
Using Lua language allows us to program any control algorithm.
First, we will create the simplest possible scene, which will be simple switching on the device.
The function takes at least two arguments:
1 --[[
2 %% properties
3 %% events
4 %% globals
5 --]]
6
7 fibaro:call(20, 'turnOn') --turn ON function
8
9
10
The phrase “–turn ON function” is a comment and it will be skipped during the execution of the script (The headers are a special case – this place does not apply to com-
ments).
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.
In this way, you can also run this scene from mobile interfaces. It will turn ON the device every time when you will click the RUN button.
In this script, we will create a scene that turns OFF the device.
To turn o" the device we will use again the same function !baro:call().
The only di"erence is that you will choose another action, which will be ‘turnO"’.
1 --[[
2 %% properties
3 %% events
4 %% globals
5 --]]
6
7 fibaro:call(20, 'turnOff') --turn OFF function, 20 - deviceID
8
9
10
Now we will create a scene which turns on the device, wait for 3 seconds and turns it o".
To call the time delay we will use !baro:sleep() function.
Time 3000 is expressed in milliseconds (1s = 1000ms).
1 --[[
2 %% properties
3 %% events
4 %% globals
5 --]]
6
7 fibaro:call(20, 'turnOn') -- turns on the device with deviceID=20
8 fibaro:sleep(3000) -- waits 3 seconds
9 fibaro:call(20, 'turnOff') -- turns off the device with deviceID=20
10
11
12
The top part of the code is a header, the rest are functions !baro:call and !baro:sleep. This scene sends request turnOn to device with deviceID=20, then waits for 3 sec-
onds and sends request turnO!.
https://manuals.fibaro.com/knowledge-base-browse/getting-started-with-lua/ Page 2 of 6
[HC2] Getting started with Lua | FIBARO Manuals 03/08/2020, 10:11
Debugging !
The basic mechanism of almost every programmable languages is a debugger. Debugging is a very useful tool.
Debugger output is printed in the black box below the code, it will inform you about mistakes in syntax or errors.
Debugger does not automatically inform you about mistakes in script logic.
Lua is executed line by line. In the !rst place, the instructions from the header and then line by line script code is executed.
To check if script arrived at some line, print some message or variable value you can use !baro:debug().
Let’s create a script containing a simple debugging mechanism:
1 --[[
2 %% properties
3 %% events
4 %% globals
5 --]]
6
7 fibaro:debug('Hello world!')
8
9
10
In this case, scene will only return the string: “Hello world!”.
“Value” is a device property, which contains device state. If value=0 device is o", if the value=1 device is on. To get value from the device you have to use appropriate Lua
function. In this case:
1 fibaro:getValue(deviceID, propertyName)
Next step will be learning of basic mechanism of programming languages – variables. They store values to use them further. To use a variable in Lua we must declare it:
1 local myValue
2 myValue=20
These commands declare a variable named myValue, and in the next line we assign this variable a value of 20.
If we want to use the device property value, we need to assign a value to the variable.
1 local myValue=fibaro:getValue(15,'value')
Now it stores myValue variable stores value of property named “value” for devices with ID 15.
1 --[[
2 %% properties
3 %% events
4 %% globals
5 --]]
6
7 local myVariable -- declaring a variable
8 fibaro:debug('Now I will turn on the Device with deviceID=15')
9 fibaro:call(15, 'turnOn') --turns on the device
10 myVariable = fibaro:getValue(15, 'value') -- gets value of property and stores in variable
11 fibaro:debug('myVariable is value is:')
12 fibaro:debug(myVariable) --prints value of variable
13
14
15
1 if (condition) then
2 instruction
3 end
https://manuals.fibaro.com/knowledge-base-browse/getting-started-with-lua/ Page 3 of 6
[HC2] Getting started with Lua | FIBARO Manuals 03/08/2020, 10:11
1 --[[
2 %% properties
3 %% events
4 %% globals
5 --]]
6
7 local myVariable
8
9 fibaro:call(14, 'turnOn')
10 myVariable = fibaro:getValue(14, 'value')
11
12 if (myVariable =='1') then
13 fibaro:call(7, 'turnOn')
14 end
15
16
17
In the !rst step, we declare a variable named myVariable. Next, system turn on the device with deviceID=14. Then to the myVariable variable system assigns value=1. The
next step is to check the condition.
You can use di"erent relational operators to compare to value, eg. value1==value2 is true if both values are equal, value1>value2 is true if value 1 is greater than value2.
If the condition is met (value = 1), then the script goes to the next line and enables the device with deviceID = 7. If condition is false (value=0) then script will omit code be-
tween “then” and “end” and !nish the script without turning the device on.
Example of using IF-THEN conditional statement in the scene. This scene will switch o" the thermostat when the window is opened.
1 --[[
2 %% properties
3 %% events
4 %% globals
5 --]]
6
7 local myVariable
8 myVariable = fibaro:getValue(55, 'value') --55 - deviceID of the Door/Window Sensor 2
9 if (myVariable == '1') then
10 fibaro:call (97, 'turnOff') --97 - deviceID of The Heat Controller
11 end
12
13
14
Trigger “property” informs the scenes engine, that our important property of the device is changing and scene should be executed. We declare it in the header. Trigger using
change of property has following syntax:
[deviceID] [property]
1 --[[
2 %% properties
3 50 value
4 %% globals
5 –]]
6
7
It declares trigger for property named “value” of device with ID 50. From now on, every time if the value of device with 50 changes – scene engine will execute the script.
1 --[[
2 %% properties
3 50 value
4 %% globals
5 --]]
6 local myVariable -- declaration of Variable
7
8 myVariable = fibaro:getValue(50, 'value')
9 if (myVariable == '1') then
10 fibaro:call(13, 'turnOn')
11 else
12 fibaro:call(13, 'turnOff')
13 end
14
15
The scenes triggers every time value of device with ID 50 changes, then if value is equal to 1 it turns on device with ID 13 or turns it o" otherwise.
Code hinting !
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 while creating new Lua scene.
https://manuals.fibaro.com/knowledge-base-browse/getting-started-with-lua/ Page 4 of 6
[HC2] Getting started with Lua | FIBARO Manuals 03/08/2020, 10:11
(https://manuals.!baro.com/wp-
content/uploads/2018/11/kua.png)
For example, when you click turnOn() action, following code will be generated.
(https://manuals.!baro.com/wp-content/uploads/2018/11/kod.png)
If you’re not sure how to write some part of the code you can make a block scene which works the same and then convert it to the Lua scene.
(https://manuals.!baro.com/wp-content/uploads/2018/11/switch.png)
https://manuals.fibaro.com/knowledge-base-browse/getting-started-with-lua/ Page 5 of 6
[HC2] Getting started with Lua | FIBARO Manuals 03/08/2020, 10:11
& '
Related articles:
- Pakiet Smart na Start – przygotowanie do zdalnej kon!guracji (https://manuals.!baro.com/knowledge-base-browse/pakiet-smart-na-start-przygotowanie-do-zdalnej-
kon!guracji/)
- Example integration with Home Connect (Bosch/Siemens) using IFTTT (https://manuals.!baro.com/knowledge-base-browse/an-example-of-integration-with-home-connect-
bosch-siemens-by-ifttt/)
- Pairing devices with Intercom (https://manuals.!baro.com/knowledge-base-browse/pairing-devices-with-intercom/)
- Setting parameters in the FIBARO for HomeKit app (https://manuals.!baro.com/knowledge-base-browse/advanced-parameters-!baro-for-homekit-devices/)
- Creating scenes in the FIBARO for HomeKit app (https://manuals.!baro.com/knowledge-base-browse/creating-scenes-in-homekit/)
(https://www.!baro.com/!les/FIBARO_Guarantee.pdf)
(https://compatibility.!baro.com/) Architect Why FIBARO
intercom/id1304252727?mt=8) id=com.!baro.installer&hl=en)
(https://play.google.com/store/apps/details?
id=com.!baro.intercom)
HomeKit
(https://www.!baro.com/en/homekit/)
https://manuals.fibaro.com/knowledge-base-browse/getting-started-with-lua/ Page 6 of 6