Fibaro System Lua API
Fibaro System Lua API
Scenes control
Global variables
Devices control
management
fibaro:call(deviceID, actionName, …)
Additional functions Name
Plugins control
Function name must be always the same: fibaro:call
Popup service
Application
Notifications control
Function sends a request to device to perform an action.
Parameters
deviceID: device ID
Returned values
None
Code example
fibaro:call(12, 'turnOff');
fibaro:get(deviceID, propertyName)
Name
Application
Gets the latest data (value and time of last modification) of the device
properties.
Parameters
Returned values
Code example
-- get a value and time of the last "brightness" property modification (device id=11)
fibaro:call(142, 'turnOff');
end
fibaro:getValue(deviceID, propertyName)
Name
Application
Parameters
Returned values
Please note that the return value is of type string. When comparing it with a
variable of type number, use tonumber to convert it first.
Code example
fibaro:getModificationTime(deviceID, propertyName)
Name
Application
Parameters
Code example
else
end
fibaro:getType(deviceID)
Name
Application
Parameters
Returned values
Code example
else
end
fibaro:getRoomID(deviceID)
Name
Application
Parameters
deviceID: device ID number
Returned values
Code example
if (room == 0) then
else
end
fibaro:getSectionID(deviceID)
Name
Application
Parameters
Returned values
Code example
if (section == 0) then
else
end
fibaro:getDevicesId(filters)
Name
Application
"filter": "hasProperty",
"filter": "interface",
"filter": "parentId",
"value": [664]
"filter": "type",
"value": ["com.fibaro.multilevelSwitch"]
"filter": "roomID",
"value": [2, 3]
"filter": "baseType",
"value": ["com.fibaro.binarySwitch"]
"filter": "isTypeOf",
"value": ["com.fibaro.binarySwitch"]
"filter": "isPlugin",
"value": [true]
"filter": "propertyEquals",
"value":
"propertyName": "configured",
"propertyValue": [true]
},
"propertyName": "dead",
"propertyValue": [false]
},
"propertyName": "deviceIcon",
"propertyValue": [15]
},
"propertyName": "deviceControlType",
"propertyValue": [15,20,25]
"filter": "deviceID",
"value": [55,120,902]
}
Parameters
Returned values
Code example
--[[
%% properties
%% events
%% globals
--]]
local data =
args = { 1 },
filters =
filter = "roomID",
value = { 2 }
},
filter = "type",
value = { "com.fibaro.motionSensor" }
print (v)
end
fibaro:callGroupAction(action, filters)
Name
Application
"filter": "hasProperty",
"filter": "interface",
"filter": "parentId",
"value": [664]
"filter": "type",
"value": ["com.fibaro.multilevelSwitch"]
"filter": "roomID",
"value": [2, 3]
"filter": "baseType",
"value": ["com.fibaro.binarySwitch"]
"filter": "isTypeOf",
"value": ["com.fibaro.binarySwitch"]
"filter": "isPlugin",
"value": [true]
"filter": "propertyEquals",
"value":
"propertyName": "configured",
"propertyValue": [true]
},
"propertyName": "dead",
"propertyValue": [false]
},
"propertyName": "deviceIcon",
"propertyValue": [15]
},
"propertyName": "deviceControlType",
"propertyValue": [15,20,25]
"filter": "deviceID",
"value": [55,120,902]
}
Parameters
Returned values
Code example
--[[
%% properties
%% events
%% globals
--]]
local data =
args = { 1 },
filters =
filter = "roomID",
value = { 2 }
},
filter = "type",
value = { "com.fibaro.motionSensor" }
print (v)
end
Scenes control
fibaro:abort()
Name
Application
Parameters
None
Returned values
None
Code example
local a = 0;
while true do
fibaro:abort();
end
a = a + 1;
fibaro:sleep(1);
end
fibaro:getSourceTrigger()
Name
Application
Gets information about the trigger that caused the current scene to run. This
function can be also used to determine which of the triggers was the direct
cause of the script execution.
Parameters
None
Returned values
Returned array includes obligatory ‘type’ field which, depending on the trigger’s
type may take the following values:
Depending on the ‘type’ value, the array may have additional fields:
deviceID
triggered device
name
ID number
name of no additional
propertyName
triggered global fields
name of
variable
triggered
property
Code example
--[[
%% properties
13 value
15 value
%% globals
isItDarkOutside
–]]
fibaro:debug('Other source');
end
fibaro:getSourceTriggerType()
Name
Application
Gets information about the type of trigger that caused the current scene to run.
Parameters
None
Returned values
Code example
end
-- the same result may be achieved by getting the whole table using getSourceTrigger()
end
fibaro:startScene(sceneID)
Name
Application
Returned values
None
Code example
fibaro:startScene(10);
end
fibaro:killScenes(sceneID)
Name
Application
Parameters
Returned values
None
Code example
-- If the value of 'a' variable is in the range [1;5], terminate all instances of scene (id=2)
fibaro:killScenes(2);
end
fibaro:setSceneEnabled(sceneID, enabled)
Name
Application
Parameters
Returned values
None
Code example
if (a>0) then
fibaro:setSceneEnabled(3, false);
else
fibaro:setSceneEnabled(3, true);
end
fibaro:isSceneEnabled(sceneID)
Name
Application
Parameters
Returned values
Code example
if (fibaro:isSceneEnabled(3)) then
fibaro:setSceneEnabled(5, true);
end
fibaro:countScenes()
Name
Application
Parameters
None
Returned values
Code example
-- number of running scenes
if (num == 1) then
else
end
fibaro:abort();
end
fibaro:countScenes(sceneID)
Name
Application
Parameters
Returned values
Code example
fibaro:debug('Scene is running');
else
end
fibaro:setSceneRunConfig(sceneID,runConfig)
Name
Application
TRIGGER_AND_MANUAL
MANUAL_ONLY
DISABLED
Which will set given scene respectively to Automatic, Manual and Disabled
mode. Any other value will set scene triggering mode to Automatic.
Parameters
Returned values
None
Code example
fibaro:setSceneRunConfig(123, 'TRIGGER_AND_MANUAL');
fibaro:getSceneRunConfig(sceneID)
Name
Application
Parameters
Returned values
Code example
fibaro:getSceneRunConfig(123);
fibaro:getGlobal(varName)
Name
Application
Retrieves the ‘value’ and ‘time last modified’ of a global variable registered in
the Variables Panel.
Parameters
Returned values
Please note that these return values are of type string. When comparing them
with a variable of type number, use tonumber to convert them first.
Code example
-- get a value and time of the last "isNight" global variable modification
fibaro:debug('It's night!');
end
fibaro:getGlobalModificationTime(varName)
Name
Application
Retrieves the ‘time last modified’ of a global variable registered in the Variables
Panel.
Parameters
Returned values
Please note that this return value is of type string. When comparing it with a
variable of type number, use tonumber to convert it first.
Code example
else
end
fibaro:getGlobalValue(varName)
Name
Parameters
Returned values
Please note that this return value is of type string. When comparing it with a
variable of type number, use tonumber to convert it first.
Code example
fibaro:setGlobal(varName, value)
Name
Application
Parameters
Returned values
None
Code example
fibaro:setGlobal('index', 1);
local a = 10 * 234;
fibaro:setGlobal('index', a);
Additional functions
fibaro:calculateDistance(position1, position2)
Name
Parameters
Points are defined using geographical coordinates. Their values are expressed in
degrees with a decimal part and separated by a semicolon. Negative values are,
respectively, west and south. The decimal separator is a dot.
Returned values
Distance in meters
Code example
local result;
fibaro:debug(text)
Name
Application
Parameters
Returned values
None
Code example
fibaro:debug('Example text');
fibaro:sleep(time)
Name
Application
Parameters
time: number of miliseconds
Returned values
None
Code example
-- Wait 10 seconds
fibaro:sleep(10000);
HomeCenter.SystemService.reboot()
Name
Requirements
Fibaro Home Center 2 updated to 4.081 software version or higher
Application
Reboots system.
Parameters
None
Returned values
None
Code example
-- Reboot system
HomeCenter.SystemService.reboot();
HomeCenter.SystemService.shutdown()
Name
Requirements
Fibaro Home Center 2 updated to 4.081 software version or higher
Application
Shutdowns system.
Parameters
None
Returned values
None
Code example
-- Shutdown system
HomeCenter.SystemService.shutdown();
Plugins control
function onAction(deviceId, action)
Name
Application
Function is usually used for handling an event assigned to the elements (button,
select, etc.) at “Advanced” tab. However this function may be called via any http
client by constructing a request as described here. Users of our system may also
call such function for example by scenes. Let us assume that we have
lightControl plugin, which is able to switch off a light. Plugin contains a method:
switchOff(LightID). Scene developer may call function switchOff(LightID) by
sending the POST request: POST api/devices/DEVICE_ID/action/ACTION_NAME
{“args”:["arg1", ..., "argN"]}
Location
Trigger
Parameters
1. deviceId - plugin’s ID (device or plugin) distinctive for this resource
2. action - array containing:
1. actionName - (action.actionName) string containing name of the
function you want to run (e.g. by pushing the button at “Advanced”
tab). Generally, function is set in PluginName.lua file.
2. args - (action.args) it’s an array containing the list of additional
arguments
Code example
end
Name
Application
Function is usually used for handling an event assigned to one of the handlers
(button, select, switch, etc.) at “General” tab.
Location
Trigger
Function is triggered as a result of plugin’s handler usage (button or slider) from
“General” tab (called “View”).
Parameters
1. deviceId - plugin’s ID (device or plugin) distinctive for this resource
2. event - array containing:
1. elementName - (event.elementName) string containing handler’s
name (name=”handlerName” – defined in view.xml). This allows to
know which GUI component (from “General” tab) has just been used.
Furthermore, it enables running appropriate function (bounded with
that name) implemented usually in PluginName.lua file.
uiBinding = {
if (callback) then
callback(event)
end
end
self:mouseUp(event)
self:mouseDown(event)
self:cancelEvents()
end
end
4. values - array containing e.g. slider’s value, bool value (false/true) for
relay switch, etc.
Code example
if (callback) then
callback(event)
end
end
Name
Function name must be always the same: configure
Application
This function is used for handling “Save” event performed by the user at plugin’s
“General” tab. Generally, it’s applied to make sure that all plugin properties are
in line with our requirements. Otherwise we can react for such a situation by
changing the property value.
Location
Trigger
Function is triggered when “Save” button from plugin’s “General” tab (called
“View”) is clicked.
Parameters
1. deviceId - plugin’s ID (device or plugin) distinctive for this resource
2. config - array containing a list of all current plugin’s properties.
Code example
restart = false
if (config.ip) then
lightControl:updateProperty('ip', config.ip)
restart = true
end
if (config.pollingTimeout) then
lightControl:updateProperty('pollingTimeout', config.pollingTimeout)
end
if (restart) then
plugin.restart()
end
end
end
Name
Application
Location
Trigger
Parameters
1. name - string containing the name of updated property
2. value - (variable supporting various types: string, int, float, etc.) new
value assigned for the property given in the “name” parameter.
Code example
lightControl:updateProperty('userName', 'Marcin')
function setName(name)
Name
Application
This is function is used for assigning new value for the “name” plugin’s property.
Location
Trigger
Parameters
1. name - string containing value for the “name” property we want to
update.
Code example
function Device:setName("multiSensor")
function setEnabled(enabled)
Name
Application
This function is used for saving “enabled” plugin’s property. If it’s set to false,
selected device/plugin is visible in the system, but can’t be controlled. Setting to
true enables its control.
Location
Trigger
Parameters
1. enabled - boolean state (true/false) of “enabled” property we want to
update.
Code example
function Device:setEnabled(true)
function setVisible(visible)
Name
Function name must be always the same: setVisible
Application
This function is used for saving “visible” plugin’s property. If it’s set to false,
selected device/plugin is invisible in the system. Setting to true enables its
visibility.
Location
Trigger
Parameters
1. visible - boolean state (true/false) of “visible” property we want to update.
Code example
function Device:setVisible(visible)
function getDevice(deviceId)
Name
Application
This function returns all available properties for selected device (ID given in
deviceId parameter).
Location
Trigger
Parameters
1. deviceId - number - plugin’s ID (device or plugin) distinctive for this
resource.
Code example
function plugin.getDevice(15)
Name
Application
Location
Function is implemented in plugin.lua. On the other hand, function call may be
performed in various files, e.g.: main.lua, PluginName.lua
Trigger
Parameters
1. deviceId - number - plugin’s ID (device or plugin) distinctive for this
resource.
2. propertyName - string containing name of the property which value we
want to get.
Code example
function getChildDevices(deviceId)
Name
Application
This function returns an array containing all child devices (owning “parentId”
property which is equal to parent’s “deviceId” property) of selected plugin.
Location
Trigger
Parameters
1. deviceId - number - plugin’s ID (device or plugin) distinctive for this
resource.
Code example
function plugin.getChildDevices(15)
Name
Application
This function creates new child device for selected plugin and returns all
available properties as an array
Location
Trigger
Code example
function restart()
Name
Application
Location
Trigger
Code example
function plugin.restart()
Inheritance
Dependencies
require('common.Object')
require('net.TCPSocket')
Overview
Handles asynchronous read of data from TCP socket. Provides interaction with
events:
Properties
delimiter
type: string
discution: Data received will be sliced by delimiter. If not provided,
dataReceived event would be rised after every succesfull read.
connectTimeout
type: integer
discution: Number of millisecond after which connection will be dropped in
case of lack in response.
readTimeout
type: integer
discution: Number of millisecond after which waiting for data to received
will be dropped in case of lack in response.
sock
type: net.TCPSocket object
discution: Responsible for sending and receiving data through socket..
self.sock = net.TCPSocket()
Methods
__init(options)
overview: Contstructor.
params:
options:
type: array
discution: Optional parameter. If not empty, should be an
array(key => value) with pairs (delimiter => value,
connectionTimeou t=> value, readTimeout => vale).
close()
overview: Closes tcp connection.
send(data)
overview: Sends data to socket.
params:
data:
type: string
discution: Data to be sent.
__succes()
overview: Called after a successful reading data from the socket.
params:
data:
type: string
discution: Data received from socket.
__error(err)
overview: Called after a failed reading data from the socket.
params:
err:
type: string
discution: Data received from socket.
Use example
self.sock = TCPListener({delimiter = string.char( 0x0D , 0x0A ), readTimeout = 10000, con
Overview
require('net.HTTPClient')
Methods
HTTPClient(timeout)
overview: Contstructor.
params:
timeout:
type: array
discution: Number of millisecond after which connection will be
dropped in case of lack in response.
self.http = net.HTTPClient({timeout=2000})
options={
headers = self.controlHeaders,
data = requestBody,
method = 'POST',
timeout = 5000
},
success = function(status)
if result[1] then
if result[1].error then
print ("ERROR")
self:updateProperty("userName", result[1].success.username)
else
print(status)
end
else
print(status)
end
end,
error = function(error)
print "ERROR"
print(error)
end
})
Overview
require('net.UDPSocket')
Methods
UDPSocket(broadcast)
overview: Contstructor.
params:
broadcast:
type: boolean
discution: Define if broadcast mode should be used.
success = function()
print('Secces:')
end,
error = function(error)
print('Error:', error)
end
})
Popup service
HomeCenter.PopupService.publish({title, subtitle,
contentTitle, contentBody, img, type, buttons})
Name
Application
Requirements
Fibaro Home Center 2 updated to 4.045 software version or higher
Fibaro mobile application:
Fibaro for iPhone 2.5 or higher
Fibaro for iPad 1.50 or higher
Fibaro for Android phones 1.6.0 or higher
Fibaro for Tablet 1.3.0 or higher
Parameters
1. title - string containing text to be displayed as a pop-up window title
(parameter required)
2. subtitle - string containing text to be displayed as a pop-up window
subtitle
3. contentTitle - string containing text to be displayed as a pop-up content
title
4. contentBody - string containing text to be displayed as a pop-up content
(parameter required)
5. img - string containing path of an image to be displayed in the pop-up
window (supported extensions: .jpg, .bmp, .png, .gif)
6. type - notification type indicated with a colour, available types:
'Info' - blue (default)
'Success' - green
'Warning' - yellow
'Critical' - red
7. buttons - array containing definitions of buttons to be displayed in the
pop-up, single button definition must be an array containing:
'caption' - text displayed on the button
'sceneId' - scene id triggered after pushing the button
At most 3 buttons may be defined. There is no need to create any button - ‘ok’
button will be created by default.
t
1st Code example
--[[
%% properties
%% globals
--]]
local imgUrl =
'http://www.fibaro.com/sites/all/themes/fibaro/images/motion-
sensor/en/motion_sensor_manual.png'
-- pop-up call
HomeCenter.PopupService.publish({
-- title (required)
-- content (required)
img = imgUrl,
type = 'Success',
-- buttons definition
buttons = {
})
NOTE
Please note that the example scene must be triggered manually. It just
illustrates the way of creating pop-ups. Execution of this scene will not affect
any device status (sceneId = 0).
NOTE
Setting an action of the button to 'sceneId = 0' means that no action will be
performed.
NOTE
Created pop-up is sent to each of users and mobile devices connected with
the main controller.
NOTE
There is no maximum size of image displayed in the pop-up window. However,
using too large file may result in long waiting times required for downloading
the image.
NOTE
Pushing one of the buttons displayed in the pop-up window may only trigger
another scene.
%% properties
3814 value
%% globals
--]]
if (
or
startSource["type"] == "other"
then
HomeCenter.PopupService.publish({
contentTitle = 'Dimmer',
type = 'Critical',
buttons = {
})
end
Notifications control
HomeCenter.NotificationService.publish(payload)
Name
Application
Publishes notification.
Parameters
Code example
HomeCenter.NotificationService.publish({
type = "GenericDeviceNotification",
priority = "warning",
data =
deviceId = 2643,
title = "foo",
text = "bar"
})
HomeCenter.NotificationService.update(id, payload)
Name
Application
Updates notification.
Parameters
id: notification id
Code example
HomeCenter.NotificationService.update(7, {
canBeDeleted = true,
data =
})
HomeCenter.NotificationService.remove(id)
Name
Application
Removes notification.
Parameters
id: notification id
Code example
HomeCenter.NotificationService.remove(7)