In een vorige blog heb ik uitgelegd hoe je met een Xiaomi Aqara Motion Sensor een lamp automatisch aan en uit kunt zetten. Tevens heb ik aangegeven hoe je kunt voorkomen dat je opeens in het donker komt te staan als je toch nog aanwezig bent (vanwege de vast ingestelde 2 minuten delay in de Xiaomi Aqara Motion Sensor).

Waf

Dit werkt nu erg goed bij ons en iedereen in huis begint het langzaam te waarderen. Echter zie ik dat soms toch nog uit gewoonte de aan/uit knop per ongeluk wordt gebruikt terwijl ik aangegeven heb dat deze altijd aan moeten blijven staan.

Vanwege de WAF wil ik niet al te grote stappen ineens nemen en heb besloten dat de normale fysieke knoppen nog blijven zitten. De planning is dat ik deze later vervang door Aqara Smart switches, maar meer daarover in een toekomstige blog.

Oplossing

Als je nu echter per ongeluk de fysieke aan/uit knop gebruikt dan zal de lamp altijd aan blijven staan met mijn eerdere oplossing. Dit probleem kun je eenvoudig oplossen met een zogenaamde watchdog. Idee hierachter is dat je de lamp alsnog uitzet als deze een langere tijd aanstaat. Dit is heel eenvoudig voor elkaar te krijgen door een LUA ‘Time’ script te maken.




Een LUA ‘Time’ script wordt door Domoticz precies elke minuut 1 keer aangeroepen. Je kunt hiermee dus perfect een teller maken die aangeeft hoe lang een lamp al aanstaat en bij een bepaalde waarde alsnog wordt uitgeschakeld zolang er geen beweging wordt gedetecteerd.

Maak hiervoor een teller aan in de Gebruikersvariabelen (Instellingen > Meer opties > Gebruikersvariabelen):




Maak nu een ‘Time’ LUA script aan met de volgende inhoud:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
--------------------------------------------------------------------------------
-- Watchdog Bijkeuken
--------------------------------------------------------------------------------
-- Script to check if 'Lamp Bijkeuken' is on for more than 'x' minutes.
-- If this is the case it will be switched off implicitly. This use-case
-- can happen in case somebody switched off/on the power-switch itself,
-- where it should be turned on always.
--------------------------------------------------------------------------------
commandArray = {}
-- Loop through all the devices
for deviceName,deviceValue in pairs(otherdevices) do
-- Check if 'Lamp Bijkeuken' is still on
if (deviceName == 'Lamp Bijkeuken') then
if deviceValue ~= "Off" then
commandArray['Variable:Watchdog_Bijkeuken'] = tostring( uservariables["Watchdog_Bijkeuken"] + 1 );
print("Watchdog 'Lamp Bijkeuken' has value : " .. uservariables["Watchdog_Bijkeuken"] );
end
end
end
-- Check if 'Lamp Bijkeuken' is on for 'x' minutes
if ( (uservariables["Watchdog_Bijkeuken"] >= 15) and (uservariables["Motion_Bijkeuken_1"] == 0) ) then
print("Watchdog triggered and no motion, turning off 'Lamp Bijkeuken'");
commandArray['Lamp Bijkeuken'] = 'Off';
commandArray['Variable:Watchdog_Bijkeuken'] = tostring(0);
end
return commandArray
-------------------------------------------------------------------------------- -- Watchdog Bijkeuken -------------------------------------------------------------------------------- -- Script to check if 'Lamp Bijkeuken' is on for more than 'x' minutes. -- If this is the case it will be switched off implicitly. This use-case -- can happen in case somebody switched off/on the power-switch itself, -- where it should be turned on always. -------------------------------------------------------------------------------- commandArray = {} -- Loop through all the devices for deviceName,deviceValue in pairs(otherdevices) do -- Check if 'Lamp Bijkeuken' is still on if (deviceName == 'Lamp Bijkeuken') then if deviceValue ~= "Off" then commandArray['Variable:Watchdog_Bijkeuken'] = tostring( uservariables["Watchdog_Bijkeuken"] + 1 ); print("Watchdog 'Lamp Bijkeuken' has value : " .. uservariables["Watchdog_Bijkeuken"] ); end end end -- Check if 'Lamp Bijkeuken' is on for 'x' minutes if ( (uservariables["Watchdog_Bijkeuken"] >= 15) and (uservariables["Motion_Bijkeuken_1"] == 0) ) then print("Watchdog triggered and no motion, turning off 'Lamp Bijkeuken'"); commandArray['Lamp Bijkeuken'] = 'Off'; commandArray['Variable:Watchdog_Bijkeuken'] = tostring(0); end return commandArray
--------------------------------------------------------------------------------
-- Watchdog Bijkeuken
--------------------------------------------------------------------------------
-- Script to check if 'Lamp Bijkeuken' is on for more than 'x' minutes.
-- If this is the case it will be switched off implicitly. This use-case
-- can happen in case somebody switched off/on the power-switch itself,
-- where it should be turned on always.
--------------------------------------------------------------------------------
commandArray = {}

-- Loop through all the devices
for deviceName,deviceValue in pairs(otherdevices) do
    
    -- Check if 'Lamp Bijkeuken' is still on
    if (deviceName == 'Lamp Bijkeuken') then
        if deviceValue ~= "Off" then
            commandArray['Variable:Watchdog_Bijkeuken'] = tostring( uservariables["Watchdog_Bijkeuken"] + 1 );
            print("Watchdog 'Lamp Bijkeuken' has value : " .. uservariables["Watchdog_Bijkeuken"] );
        end
    end
end

-- Check if 'Lamp Bijkeuken' is on for 'x' minutes
if ( (uservariables["Watchdog_Bijkeuken"] >= 15) and (uservariables["Motion_Bijkeuken_1"] == 0) ) then
    print("Watchdog triggered and no motion, turning off 'Lamp Bijkeuken'");
    commandArray['Lamp Bijkeuken'] = 'Off';
    commandArray['Variable:Watchdog_Bijkeuken'] = tostring(0);
end

return commandArray

Tenslotte moet we het eerdere ‘Device’ LUA script dat gebruik maakt van de Xiaomi Aqara Motion Sensor aanpassen door de hierboven aangemaakte teller te resetten indien de lamp uitgaat:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
--------------------------------------------------------------------------------
-- Motion
--------------------------------------------------------------------------------
-- Script to check all motion sensors and act accordingly.
--------------------------------------------------------------------------------
commandArray = {}
-- loop through all the changed devices
for deviceName,deviceValue in pairs(devicechanged) do
if (deviceName == 'Motion Bijkeuken 1') then
if (deviceValue == "On") then
print("Beweging (1) in Bijkeuken gedetecteerd")
-- Set 'Motion_Bijkeuken_1' toggle
commandArray['Variable:Motion_Bijkeuken_1'] = tostring(1);
-- Check if 'Lamp Bijkeuken' is not already on
if (otherdevices['Lamp Bijkeuken'] == 'Off') then
-- Turn 'Lamp Bijkeuken' on for 'x' minutes
commandArray['Lamp Bijkeuken'] = 'On FOR 3'
end
elseif (deviceValue == "Off") then
-- Reset 'Motion_Bijkeuken_1' toggle
commandArray['Variable:Motion_Bijkeuken_1'] = tostring(0);
end
-- Lamp Bijkeuken switches Off but motion detected in the meantime?
elseif ( (deviceName == 'Lamp Bijkeuken') and (deviceValue == "Off") ) then
-- Reset watchdog
commandArray['Variable:Watchdog_Bijkeuken'] = tostring(0);
-- In the meantime motion detected
if (uservariables["Motion_Bijkeuken_1"] == 1) then
-- Check if 'Lamp Bijkeuken' is not already on
if (otherdevices['Lamp Bijkeuken'] == 'Off') then
-- Turn 'Lamp Bijkeuken' on for 'x' minutes
commandArray['Lamp Bijkeuken'] = 'On FOR 3'
end
end
end
end
return commandArray
-------------------------------------------------------------------------------- -- Motion -------------------------------------------------------------------------------- -- Script to check all motion sensors and act accordingly. -------------------------------------------------------------------------------- commandArray = {} -- loop through all the changed devices for deviceName,deviceValue in pairs(devicechanged) do if (deviceName == 'Motion Bijkeuken 1') then if (deviceValue == "On") then print("Beweging (1) in Bijkeuken gedetecteerd") -- Set 'Motion_Bijkeuken_1' toggle commandArray['Variable:Motion_Bijkeuken_1'] = tostring(1); -- Check if 'Lamp Bijkeuken' is not already on if (otherdevices['Lamp Bijkeuken'] == 'Off') then -- Turn 'Lamp Bijkeuken' on for 'x' minutes commandArray['Lamp Bijkeuken'] = 'On FOR 3' end elseif (deviceValue == "Off") then -- Reset 'Motion_Bijkeuken_1' toggle commandArray['Variable:Motion_Bijkeuken_1'] = tostring(0); end -- Lamp Bijkeuken switches Off but motion detected in the meantime? elseif ( (deviceName == 'Lamp Bijkeuken') and (deviceValue == "Off") ) then -- Reset watchdog commandArray['Variable:Watchdog_Bijkeuken'] = tostring(0); -- In the meantime motion detected if (uservariables["Motion_Bijkeuken_1"] == 1) then -- Check if 'Lamp Bijkeuken' is not already on if (otherdevices['Lamp Bijkeuken'] == 'Off') then -- Turn 'Lamp Bijkeuken' on for 'x' minutes commandArray['Lamp Bijkeuken'] = 'On FOR 3' end end end end return commandArray
--------------------------------------------------------------------------------
-- Motion
--------------------------------------------------------------------------------
-- Script to check all motion sensors and act accordingly.
--------------------------------------------------------------------------------
commandArray = {}

-- loop through all the changed devices
for deviceName,deviceValue in pairs(devicechanged) do

    if (deviceName == 'Motion Bijkeuken 1') then
    
        if (deviceValue == "On") then
            print("Beweging (1) in Bijkeuken gedetecteerd")
            
            -- Set 'Motion_Bijkeuken_1' toggle
            commandArray['Variable:Motion_Bijkeuken_1'] = tostring(1);
            
            -- Check if 'Lamp Bijkeuken' is not already on
            if (otherdevices['Lamp Bijkeuken'] == 'Off') then
    
                -- Turn 'Lamp Bijkeuken' on for 'x' minutes
                commandArray['Lamp Bijkeuken'] = 'On FOR 3'
    
            end
            
        elseif (deviceValue == "Off") then
            
            -- Reset 'Motion_Bijkeuken_1' toggle
            commandArray['Variable:Motion_Bijkeuken_1'] = tostring(0);
            
        end
    

    -- Lamp Bijkeuken switches Off but motion detected in the meantime?
    elseif ( (deviceName == 'Lamp Bijkeuken') and (deviceValue == "Off") ) then

        -- Reset watchdog
        commandArray['Variable:Watchdog_Bijkeuken'] = tostring(0);
    
        -- In the meantime motion detected
        if (uservariables["Motion_Bijkeuken_1"] == 1) then
            
            -- Check if 'Lamp Bijkeuken' is not already on
            if (otherdevices['Lamp Bijkeuken'] == 'Off') then
                -- Turn 'Lamp Bijkeuken' on for 'x' minutes
                commandArray['Lamp Bijkeuken'] = 'On FOR 3'
            end
        end

    end
end

return commandArray

Met bovenstaande scripts wordt de lamp in de bijkeuken automatisch aangezet indien er beweging wordt gedetecteerd. Verder zal deze minimaal 3 minuten aanblijven zolang er beweging gedetecteerd blijft worden. Wordt in de tussentijd per ongeluk de fysieke aan/uit knop gebruikt dan zal de bijkeuken lamp die aanstaat alsnog automatisch na 15 minuten worden uitgezet.

In een toekomstige blog ga ik uitleggen hoe je dit alles weer op een slimme manier kunt combineren met een intelligente schakelaar.