Skip to content

Event functions

Jan Gabrielsson edited this page Apr 2, 2018 · 32 revisions

Event.event

Event.event(<event>,<action>)

<event> is a Lua table with a 'type' key.
e.g. {type='property', deviceID=58, propertyName='value'}.
<action> is a Lua function that should be called when an event is received matching the first argument.
e.g. function(env) fibaro:debug("Ding!") end
Functions take one argument, and 'environment' with context info.
An event handler that turns on lamp with id 77 when motion sensor with id 58 is triggered would look like this

Event.event({type='property', deviceID=58, propertyName='value'},
           function(env) if fibaro:getValue(58,'value')>'0' then fibaro:call(58,'turnOn') end end)

In this case we check if the sensor was breached and then turn on the light.
The framework is helpful and complements the Fibaro event above with a 'value' field before handing it over to the event handler. It also allows for 'constraint' pattern in the matching event. E.g.

Event.event({type='property', deviceID=58, value='$>0'},
           function(env) fibaro:call(58,'turnOn') end)

This handler only matches if the value is above '0' so the handler can safely turn on the light when called without having to check the sensor value. Constraints can be '>', '<', '>=', '<=', '~='. The check can be against a numeric value or a string. For checks with equality no constraint operator is needed as fields are matched with each other.

Event.event({type='global', name='TimeOfDay', value='Night'},
           function(env) fibaro:debug('TimeOfDay is set to Night') end)

Event.post

Event.post(<event>[,<time>])

Event.schedule

Event.schedule(<time>,<action>[,<options>])

Event.cancel

Event.cancel(<postRef>)

Event.postRemote

Event.postRemote(<id>,<event>)

...