Jump to content
  • 0

DW sensor + PIR


Question

Hi all,

 

I have a DW sensor that should switch on the light when it is evening and the door is open.

Once triggered, the light should remain on until the door is closed and no motion is detected in that room.

When door is closed and motion is no longer detected the light should slowly fade out.

 

I wrote below code for this but cant get it working properly.

I have the problem that the light will perfectly go when the door is opened but it will not go out! 

 

Anybody an idea what I do wrong? It seems not to reach the "repeat" part of my code which is within the IF statement.

 

Here is the code:

 

--[[
%% autostart
%% properties
21 value
127 value
%% weather
%% events
%% globals
omtrekverlichting
--]]

--============ HOME TABLE =======================
fibaro:getGlobalValue("HomeTable")
local jT = json.decode(fibaro:getGlobalValue("HomeTable"))
--===============================================

-- used devices and its id
fibaro:debug("device: hal - voordeur DW = ID: " ..jT.hal.voordeursensor) --127
fibaro:debug("device: hal - hal PIR = ID: " ..jT.hal.HalMo21) --21
fibaro:debug("device: hal - duospot = ID: " ..jT.hal.duospot) --160
fibaro:debug("device: hal - halspot = ID: " ..jT.hal.halspot) --116
--===============================================

local debug = false

Debug = function (debug,color, message )
  if (debug) then
      fibaro:debug(os.date("%x")..string.format('<%s style="color:%s;">%s</%s>', "span", color, message, "span")); 
  end
end

 

if (fibaro:countScenes() > 1) then
    Debug( true,"purple", " too many instances --> abort ");
      fibaro:abort()
end

 

local trigger = fibaro:getSourceTrigger()

if (trigger['type'] == 'property')         then Debug(true, "Grey", ' scene triggered by - Source device = ' .. trigger['deviceID'])
elseif (trigger['type'] == 'global')     then Debug(true, "Grey", ' scene triggered by - Global variable source = ' .. trigger['name'])
elseif (trigger['type'] == 'other')     then Debug(true, "Grey", ' scene triggered by - Other source')
end

-- start up ---------------------------------------


local deurstatus = fibaro:getValue(jT.hal.voordeursensor, "value")
local Darkness = fibaro:getGlobalValue("Darkness")
local motion = fibaro:getValue(jT.hal.HalMo21, "value")
local lampstatus = fibaro:getValue(jT.hal.duospot, "value")

local dimlevel = 60


if     (tonumber(Darkness) == 1) and (tonumber(deurstatus) == 1) then
                  Debug( true,"Orange",' hal verlichting ingeschakeld')
                  fibaro:call(jT.hal.duospot , 'setValue' , dimlevel) 
                  fibaro:call(jT.hal.halspot , 'setValue' , dimlevel) 
                 

                  repeat fibaro:sleep(3*1000) -- always wait at least x seconds before fade out.
                  until (tonumber(motion) == 0) and (tonumber(deurstatus) == 0)
          
                 while( dimlevel > 10 )       do -- start fade -out.
                 fibaro:call(jT.hal.duospot , 'setValue' , dimlevel) 
                 fibaro:call(jT.hal.halspot , 'setValue' , dimlevel) 
                 fibaro:sleep(20)
                 Debug( true,"Yellow",' dimlevel is '..dimlevel)
                dimlevel = (dimlevel -0.4)
                end


      fibaro:call(jT.hal.duospot , 'turnOff' ) -- make sure lights are really off
      fibaro:call(jT.hal.halspot , 'turnOff' )
      Debug( true,"Green",' hal verlichting uitgeschakeld')      
end

 

Link to post
Share on other sites

1 answer to this question

Recommended Posts

  • 0

 

--[[
%% properties
798 value
%% events
%% globals
--]]

if (fibaro:countScenes() > 1) then
	fibaro:abort()
end

local motionId = { 798 }
local doorId = { 777 }
local powerId = { 784 }
local luxId =  { }
local lumen = 2000
local timeout = 1 -- 1 minute
local timePeriod = '18:00-21:00' -- evening

--======================================================================

function getLastBreach(dev)
	local breach = 0;
	for i, n in ipairs(dev) do
		local nBreach = tonumber(fibaro:getValue(n, "lastBreached"));
		if(nBreach > breach) then
			breach = nBreach;
		end
	end
	return breach;
end

function getMotionState(dev)
	for i, n in ipairs(dev) do
		if (tonumber(fibaro:getValue(n, "value")) > 0) then
			return 1;
		end
	end
	return 0;
end

function getLux(dev)
	local totalLux   = 0;
	local averigeLux = 0;
	if #dev > 0 then
		for i = 1, #dev do
			totalLux = totalLux + tonumber(fibaro:getValue(dev[i], 'value'));
		end
		averigeLux = math.floor(totalLux / #dev.luxs);
	else
		averigeLux = 100000 -- Direct sunlight
	end
	return averigeLux;
end

function getPowerState(dev)
	for i, id in ipairs(dev) do
		local deviceState, deviceLastModification = fibaro:get(id, "value");
		if (tonumber(deviceState) > 0) then
			return 1, deviceLastModification
		end
	end
	return 0, 0
end

function getDoorState(dev)
	for i, id in ipairs(dev) do
		local deviceState, deviceLastModification = fibaro:get(id, "value");
		if (tonumber(deviceState) > 0) then
			return 1, deviceLastModification
		end
	end
	return 0, 0
end

function setPowerState(dev, value)
	for i, id in ipairs(dev) do
		local lightType = fibaro:getType(id);
		if (lightType == "com.fibaro.multilevelSwitch" or lightType == "com.fibaro.FGD212") then
			fibaro:call(id, "setValue", value);
		elseif(lightType == "com.fibaro.FGRGBW441M") then
			if type(value) == "string" then
				local r, g, b = hex2rgb(value)
				fibaro:call(id, "setColor", r,  g,  b,  0);
			else
				if value > 0 then
					fibaro:call(id, "setW", value);
				--fibaro:call(id, "setValue", value) 
				else
					fibaro:call(id, "turnOff");
				--fibaro:call(id, "setColor", 0, 0, 0, 0);
				end
			end
			else
			if value > 0 then
				fibaro:call(id, "turnOn");
			else
				fibaro:call(id, "turnOff");
			end
		end
	end
end

function between(str)
	local t = os.date("*t",os.time());
	local h1,m1,h2,m2 = str:match("(%d+):(%d+)-(%d+):(%d+)")
	m1, m2, t = h1*60+m1, h2*60+m2, t.hour*60+t.min
	if (m1 <= m2) then
		return m1 <= t and t <= m2 -- 01:00-02:00
	else
		return m1 <= t or t <= m2 -- 23:00-21:00
	end
end


fibaro:debug("Strated...");
local startSource = fibaro:getSourceTrigger();

local state = nil
while true do 
	-- power
	local powerState = getPowerState(powerId)

	if (startSource["type"] == "property") then
		if state == nil and powerState > 0 then
			fibaro:debug("LAMP ALREADY ON");
			break
		end
	else
	--fibaro:debug("Script can start only via property");
	--break
	end 

	-- our state
	state = state or powerState

	-- motion
	local motionState = getMotionState(motionId)
     
	-- is motion here
	if motionState > 0 then
		-- is not light  
		if powerState == 0 then
			-- is time?
			if between(timePeriod) then
				-- is darkness?
				if getLux(luxId) < lumen then
					-- is marked?
					if state == 0 then
						state = 1
						fibaro:debug("LAMP ON (MOTION)");
						setPowerState(powerId, 99)
					end
				else
					fibaro:debug("IS NOT TIME FOR LAMP");
				end
			else
				fibaro:debug("IS NOT TIME FOR LAMP");
			end
		end
	else
		-- safe?
		-- is light?
		if powerState > 0 then 
			-- is marked?
			if state == 1 then
				if os.time() - getLastBreach(motionId) > 60 * timeout then
					-- check is door closed?
					if getDoorState(doorId) == 0 then
						state = 0
						fibaro:debug("LAMP OFF (NO MOTION)");
						setPowerState(powerId, 0)
			
						if (startSource["type"] == "property") then
							break
						end
					end
				end
			end
		else
			-- is not light?
			-- Opps!
			if state == 1 then
				state = 0
				fibaro:debug("LAMP OFF (MANUALLY)");
				if (startSource["type"] == "property") then
					break
				end
			end
		end
	end  
    
	fibaro:sleep(1000);
end

fibaro:debug("End.");

 

Edited by 10der
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...