
Filename : Script_device_luxdimmer.lua
Version: 0.1
Author: M van Meurs
Date: 23-11-2016
Purpose: Set a light device dimming level in line with light intensity (Lux) needed when there is motion
Requirements: A light device capable of dimming and a motion sensor with lux measurement (all in the same room)
Tested: Yes, tested with a Fibaro FGMS001 Motion Sensor and a RFX433 KaKu/CoCo Dimming Module
Code: Select all
--[[
Filename : Script_device_luxdimmer.lua
Version: 0.1
Author: M van Meurs
Date: 23-11-2016
Purpose: Set a light device dimming level in line with light intensity (Lux) needed when there is motion
Requirements: A light device capable of dimming and a motion sensor with lux measurement (all in the same room)
Tested: Yes, tested with a Fibaro FGMS001 Motion Sensor and a RFX433 KaKu/CoCo Dimming Module
--]]
--Set user variables below this line (need to edit)
DeviceDimmer = 'Dummy_Dimmer' -- The EXACT name of your dimming device
DeviceLux = 'Dummy_Lux' -- The EXACT name of your sensor with lux measurement
DeviceSensor = 'Dummy_Sensor' -- The EXACT name of your motion sensor
MinValue = 1 -- Minimum Lux level when your lights need to be 100%
MaxValue = 100 -- Maximum Lux level when your lights doesn`t need to be switched on (for example 100 lux for a hallway and 500 lux for a working space)
debug = false -- Set to 'true' when you want to debug to domoticz log
--Program below this line (no need to edit)
--Error handling of user variables (regardless of debug setting)
assert(otherdevices[DeviceDimmer],"ERROR:Variable DeviceDimmer not filled in correct! '"..DeviceDimmer.."' name is incorrect,please change to correct name")
assert(otherdevices[DeviceLux],"ERROR:Variable DeviceLux not filled in correct! '"..DeviceLux.."' name is incorrect,please change to correct name")
assert(otherdevices[DeviceSensor],"ERROR:Variable DeviceSensor not filled in correct! '"..DeviceSensor.."' name is incorrect,please change to correct name")
if MinValue>=MaxValue then print ("ERROR:Variable MinValue must be smaller then variable MaxValue! Please correct value")end
--Set variables needed later on
local LuxRealtime = tonumber(otherdevices_svalues[DeviceLux]) --write the current lux value
--Function to round a number
function round(num, idp)
local mult = 10^(idp or 0)
return math.floor(num * mult + 0.5) / mult
end
commandArray = {}
--Set the dimlevel needed
if LuxRealtime<=MinValue then DimLevel = 100
elseif LuxRealtime>MaxValue then DimLevel = 0
else
DimFactor = (MinValue/MaxValue-1)*-1 --Get the logarithmic scale for dimming between Lux level settings defined in Min/MaxValue
DimLevel = round((((LuxRealtime/DimFactor)/MaxValue-1)*100)*-1, 0) --Set the needed dimlevel in % (rounded)
end
--Set the dimmer to calculated level when there is motion detected
if (devicechanged[DeviceSensor] == 'On' and otherdevices[DeviceDimmer] == 'Off') then
commandArray[DeviceDimmer]='Set Level '..(DimLevel)
--Turn of the dimmer when there is no motion detected
--Set your motion cancellation delay higher on your sensor if the offtime is too short
--++TO DO:Add time based script for motion sensors that doesn`t have motion cancellation delay settings
elseif (devicechanged[DeviceSensor] == 'Off' and otherdevices[DeviceDimmer] ~= 'Off') then
commandArray[DeviceDimmer]='Off'
end
--Print debug information when debug = true
if (debug) then
print ("*** DEBUGINFO ***")
print ("Stored Lux Value: ".. LuxRealtime)
print ("Minimum Value set: ".. MinValue.." Lux")
print ("Maximum Value set: ".. MaxValue.." Lux")
print ("Dimming level calculated: "..DimLevel.."%")
print ("STATUS "..DeviceSensor.." "..otherdevices[DeviceSensor])
print ("STATUS "..DeviceDimmer.." "..otherdevices[DeviceDimmer])
for i, v in pairs(commandArray) do print('Setting [' .. i .. '] to ' .. v) end
end
return commandArray