So we managed to add some Philips Hue devices as Virtual Devices,
but it would we nice if we are able to automate some things
for me I have used the Philips Hue motion sensor (SML003 ) in the bathroom to trigger a Fibaro light switch ( FGS-223 ZW5 )
in order to trigger the light
Create a Scene in Fibaro and select the LUA scene
and paste the following code:
--[[
%% autostart
%% properties
%% events
%% globals
--]]
local sourceTrigger = fibaro:getSourceTrigger();
-- Function to find the label of the Virtual Device
function NamesID(devID,ui)
jsonDeviceID = api.get("/virtualDevices/"..devID)
return jsonDeviceID["properties"][ui];
end
-- clock, so we can use sleep in seconds
local clock = os.clock
function sleep(n)
local t0 = clock()
while clock() - t0 <= n do end
end
-- lets create a loop, since there is nothing to trigger
function loopFunc()
-- Select the Virtual Device with the monitored label
-- and the requested value
if(tonumber(NamesID(196,"ui.presence.value")) == 1) then
fibaro:debug(NamesID(196,"ui.presence.value"))
-- Select the lightID and the action
fibaro:call(54, "turnOn")
sleep(60)
end
-- Select the Virtual Device with the monitored label
-- and the requested value
if(tonumber(NamesID(196,"ui.presence.value")) == 0) then
fibaro:debug(NamesID(196,"ui.presence.value"))
-- Select the lightID and the action
-- fibaro:call(54, "turnOff")
-- sleep(300)
end
setTimeout(loopFunc,3000)
end
if (sourceTrigger["type"] == "autostart") then
loopFunc()
end
but there are some things that needs to be changed, I didn't create variables because it might make the script too complex.
so replace the 196 here with your Virtual Device
in my case this was my Hue Motionsensor
and also replace the 54 here, with your Fibaro light
now when motion is detected, your sensor is triggered and your light will turn on
you might have noticed in my script I have commented out the Off trigger,
because I noticed that when I use the wall switch to turn on the light the Scene will turn it off again
(without movement activity)
In order to turn off my light automatically I created an Magic scene on the Fibaro light
When it turns on, it turns off after 5 minutes
So now I still have full control over my wall switch (besides that it turns off after 5 minutes (unless there is movement)
We have the basics, when you walk in the sensor, the light turns on, but its also triggered during the day.
so it would be nice if it will only be triggered when the light level is below a percentage light.
so what I did, I added another label on my Hue Lightsensor called lightlevelRAW with the same ID
because my lightlevel contains an % character, and I needed a number only.
and added the following code in the main loop
Code for Virtual Device
-- code for the Virtual Device
fibaro:call(devId, "setProperty", "ui.lightlevelRAW.value" , math.floor(jsonString["state"]["lightlevel"]*0.002301655));
Code for the Scene
replace the following code
from:
if(tonumber(NamesID(196,"ui.presence.value")) == 1) then
fibaro:debug(NamesID(196,"ui.presence.value"))
-- Select the lightID and the action
fibaro:call(54, "turnOn")
sleep(60)
end
to
if(tonumber(NamesID(196,"ui.presence.value")) == 1 and tonumber(NamesID(212,"ui.lightlevelRAW.value")) <= 30) then
fibaro:debug(NamesID(196,"ui.presence.value"))
-- Select the lightID and the action
fibaro:call(54, "turnOn")
sleep(60)
end
Keep in mind that you monitor the light sensor, for me this was ID 212, you might want to double check what ID your light sensor has.