Fibaro Lua API: Global Variables

Fibaro global variables are pre-defined to the Fibaro environment using the Home Center -> Panels -> Variables user interface (note: these are not the same as Lua global variables). Values associated with some Fibaro variables are predefined i.e. there is a finite set of defined values, others may have any value in either case the variable name must have been defined prior to use/reference.

Triggering a Scene when the Value of a Global Variable Changes

--[[
%% globals
global_variable_name
--]]

A Lua scene to be triggered when a Fibaro global variable changes must include the trigger requirements within Lua comments at the start of the scene. Whenever the value of the named global variable changes then this scene will be run (triggered).

--[[
%% globals
TimeOfDay
--]]
  fibaro:debug('Scene invoked, TimeOfDay = '
    ..fibaro:getGlobalValue('TimeOfDay'));

For example a global variable named TimeOfDay could be updated on a timer basis, after which any and all scenes to be triggered via this variable will be given control, as with the example to the left.

Consider using this technique to separate long running scenes e.g. timer based scenes or those using fibaro:sleep() from the actions to take place on a state change.

Lua functions for Global Variables

FunctionDescription
fibaro:getGlobal( "variable_name" ) Returns two string parameters, first the value of the fibaro defined global variable variable_name, second the time the global variable was last modified.
Example:
  local value, lastModTime = fibaro:getGlobal( "TestGlobal");
fibaro:getGlobalValue( "variable_name" ) Returns the value of the fibaro defined global variable variable_name.
Example:
  local value = fibaro:getGlobalValue( "TestGlobal");
fibaro:setGlobal( "variable_name", "value" ) Sets the value of the fibaro defined global variable variable_name to the value provided by value (note: if the global variable has pre-defined values then the value supplied here must be one of those allowed values).
Example:
  local newValue = "Some text string";
  fibaro:setGlobal( "TestGlobal", newValue);
fibaro:getGlobalModificationTime( "variable_name" ) Returns the timestamp associated with a change to the value of the fibaro defined global variable variable_name.
Example:
  local lastMod = fibaro:getGlobalModificationTime( "TestGlobal");
  fibaro:debug("TestGlobal last updated: " ..( os.time()-lastMod)
    .. " seconds ago");

Defining a Global Variable Programmatically

While it is more normal to pre-define Fibaro global variables through the Home Center Panels -> Variables Panel dialog, the Fibaro REST API can be used to programatically define global variables. In the code sample below a fibaro global variable, testVar is defined to the Fibaro system with an initial value of 'Hello World'.

--[[
      Sample: defines Fibaro global variable 'testVar'
--]]
   local varName = 'testVar';
   local varValue = 'Hello World';

   local varState = 'created'
   response, status = api.post("/globalVariables", 
        {
          name = varName, 
          value =  tostring(varValue)
         });  
   if (status == 409) then
      -- variable already defined
      fibaro:setGlobal( varName, varValue);
      varState = 'updated';
   elseif (status ~= 201) then
      fibaro:debug("Unable to create global variable "..varName
            ..', '..response.message );
      fibaro:abort();
   end

   fibaro:debug("Global variable "..varName.." "..varState
         ..", value: "..fibaro:getGlobalValue( varName));

Note:

 

HTTPClient
Next
Fibaro
Lua API
Contents

Questions or comments, please e-mail us at info@snys.nl