-
Notifications
You must be signed in to change notification settings - Fork 11
Home
This framework allows for an 'alternative' way to program/design scenes for the Fibaro home automation system. The system allows users to design 'scenes' and 'virtual devices' (VDs) in Lua. This framework is only for scenes.
Scenes on the Fibaro HC2 is typically invoked by declaring device triggers in the header. Ex
--[[
%% properties
55 value
44 value
65 power
%% globals
TimeOfDay
%% events
362 CentralSceneEvent
%% autostart
--]]
When the state of the declared devices changes state the scene is invoked:
- the 'value' property of device 55 changes
- the 'value' property of device 44 changes
- the 'power' property of device 65 changes
- the global Fibaro variable 'TimeOfDay' changes
- the device 362 emits a 'CentralSceneEvent'
The '%% autostart' tells the scene to respond to triggers
Every time a scene is triggered, a new instance of the scene is spawned, something that makes it difficult to "remember" state between scene invocations. There are global Fibaro variables that can be set and retrieved but they are a bit cumbersome to use.
This framework takes care of transforming a new scene instances to 'timer threads' in the intial scene instances. The model is based on events being posted to user defined 'event handlers'
Handlers are defined with Event.event. Ex:
function main()
Event.event({type='property', deviceID=55, value='$>0'}, function(e) fibaro:call(44,'turnOn') end)
Event.event({type='property', deviceID=65, propertyName='power', value='$<10'}, function(e) Log(LOG.LOG,"Power less than 10") end)
Event.event({type='global', name='TimeOfDay', value='Day'}, function(e) Log(LOG.LOG,"It's daytime!") end)
end
Handlers are defined in a 'main()' function. The above example registers a handler for an incoming event for a device (i.e. sensor) that if the value is above 0, will call the action that turns on device 44 (i.e. light) The magic is that all event handlers are invoked in the same initial scene instance which allows for local variables to be used. Ex.
function main()
local counter = 0
Event.event({type='property', deviceID=55, value='$>0'},
function(e) counter=counter+1; Log(LOG.LOG,"Light turned on %s times",counter) fibaro:call(44,'turnOn'))
end
Something that would be impossible in the normal model as each 'handler' would be called in a new instance.
Events are table structures with a 'type' key, which is true for Fibaro's own events. However, the framework allows for posting user defined events with 'Event.post(event[,time])' The optional 'time' parameter specifies a time in the future that the event should be posted (if omitted it is posted imediatly). This turn the framework into a programming model. Ex. (main() is omitted in the examples from now)
Event.event({type='loop'},
function(e) Log(LOG.LOG,"Ding!") Event:post({type='loop'},"+/00:10") end)
Event.post({type='loop'})
This will print "Ding!" imediatly, and then print "Ding!" every 10 minutes.