Presence detection  [Solved]

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
Cndiann

Presence detection

Post by Cndiann »

Hello,

I have made a system alive young device for my mobile phone. I have set the correct mac and IP address, so domoticz does ping my phone correct.

Now I would like to when I come home the scene 'woonkamer aan' will be activated after sunset when my phone connects to the Wifi.

But when I would like to set a other scene, for example 'watch movie' I want to overrule the presence detection. So when I decided to turn off wifi on phone and after half hour turn wifi on the lights will not set back to 'woonkamer aan'.

Is it possible to run the detection script again only after scene 'all lights out' has been activated? So that when I get home the detection script starts again?

Thanks
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Presence detection

Post by waaren »

Cndiann wrote: Tuesday 20 October 2020 0:58 ...
Is it possible to run the detection script again only after scene 'all lights out' has been activated? So that when I get home the detection script starts again?
Sure, it's possible..

My approach to this would be to separate the required functionality into different scripts.

One script to identify presence of phones on wifi and have this script communicate that state by using a customEvent or using Global persistent data-, a uservariable or a dummy device.

One script to identify switching of 'watch movie' (and/or other scenes) and have this script communicate that state using Global persistent data-, a uservariable or a dummy device.

One script to switch the scene 'Woonkamer aan' after checking time / presence state / scene state.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Cndiann

Re: Presence detection

Post by Cndiann »

Thank you. For the first script I have made a dummy switch and set my phone as a sub slave device, so the dummy switch works.

But the other 2 script, I'm totally new with lua or dzvents. I have tried the search function for similar scripts but can't find one.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Presence detection

Post by waaren »

Cndiann wrote: Wednesday 21 October 2020 9:37 Thank you. For the first script I have made a dummy switch and set my phone as a sub slave device, so the dummy switch works.

But the other 2 script, I'm totally new with lua or dzvents. I have tried the search function for similar scripts but can't find one.
Will try to come with an example but might take some time.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Presence detection

Post by waaren »

Cndiann wrote: Wednesday 21 October 2020 9:37 Thank you. For the first script I have made a dummy switch and set my phone as a sub slave device, so the dummy switch works.
Initial version (I combined 2nd 3rd script into 1)

Code: Select all

-- One script to switch the scene 'Woonkamer aan' after checking time / presence state / scene state.

local blockingScenes =
{
    'watch movie',
    'watch tv',   -- example to show you can have multiple blocking scenes
}

local blockingGroups =
{
    'romantic lights', -- Scenes and groups are distinct so need to be addressed separate
    'another Group',
}

local myPhone = 'name of Phone Switch' -- change to name of dummy switch 

return
{
    on =
    {
        scenes = blockingScenes, -- if one or more of these scenes / groups are activated after sunset 
        groups = blockingGroups, -- the switchingScene will be blocked until next sunset

        timer =
        {
            'at sunset',
            'at sunrise',
        },

        devices =
        {
            myPhone,
        }
    },

    data =
    {
        blockers =
        {
            initial = {},
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- change to domoticz.LOG_ERROR when all OK
        marker = 'scene Control',
    },

    execute = function(dz, item)
        local switchingScene = dz.scenes('woonkamer aan') -- change to name of your switchingScene
        local phoneAtHome = dz.devices(myPhone).active

        local function switchAllowed(t)
            for scene, blocked in pairs(t) do
                if blocked then
                    dz.log(scene .. ' is blocking the switch', dz.LOG_DEBUG)
                    return false
                end
            end
            return true
        end

        if item.isScene or item.isGroup and item.state ~= 'Off' and dz.time.matchesRule('between sunset and 23:59') then
            dz.data.blockers[item.name] = true
            return
        end

        if item.isTimer and dz.time.matchesRule('at sunrise') then
           dz.data.blockers = {} -- reset blockers
           return
        end

        if phoneAtHome and dz.time.matchesRule('between sunset and 23:59') then -- This will be evaluated at sunset and when the phone becomes active after sunset
            if switchAllowed(dz.data.blockers) then
                switchingScene.switchOn()
            end
        end
    end
} 
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Cndiann

Re: Presence detection

Post by Cndiann »

Thanks the script is working!!

I have only 2 phones as a dummy, is this script correct? Does it work when only 1 phone is active? And when the second phone comes home does it not overrule the script?

Is it possible to reset the script when for example scene leaving home is activated?

Code: Select all

-- One script to switch the scene 'Woonkamer aan' after checking time / presence state / scene state.

 

local blockingScenes =

{

'Film kijken',

-- example to show you can have multiple blocking scenes

}

 

local blockingGroups =

{

'Film kijken', -- Scenes and groups are distinct so need to be addressed separate

 

}

 

local myPhone =

{ 'Telefoon 1',

'Telefoon 2',-- change to name of dummy switch

}

 

return

{

on =

{

scenes = blockingScenes, -- if one or more of these scenes / groups are activated after sunset

groups = blockingGroups, -- the switchingScene will be blocked until next sunset

 

timer =

{

'at sunset',

'at sunrise',

},

 

devices =

{

myPhone,

}

},

 

data =

{

blockers =

{

initial = {},

},

},

 

logging =

{

level = domoticz.LOG_ERROR, -- change to domoticz.LOG_ERROR when all OK

marker = 'scene Control',

},

 

execute = function(dz, item)

local switchingScene = dz.scenes('Woonkamer aan') -- change to name of your switchingScene

local phoneAtHome = dz.devices(myPhone).active

 

local function switchAllowed(t)

for scene, blocked in pairs(t) do

if blocked then

dz.log(scene .. ' is blocking the switch', dz.LOG_DEBUG)

return false

end

end

return true

end

 

if item.isScene or item.isGroup and item.state ~= 'Off' and dz.time.matchesRule('between sunset and 23:59') then

dz.data.blockers[item.name] = true

return

end

 

if item.isTimer and dz.time.matchesRule('at sunrise') then

dz.data.blockers = {} -- reset blockers

return

end

 

if phoneAtHome and dz.time.matchesRule('between sunset and 23:59') then -- This will be evaluated at sunset and when the phone becomes active after sunset

if switchAllowed(dz.data.blockers) then

switchingScene.switchOn()

end

end

end

}
 
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Presence detection

Post by waaren »

Cndiann wrote: Friday 23 October 2020 15:09 I have only 2 phones as a dummy, is this script correct?
Your modification will not work. What did you do to test it ?
Below modified script will check all phones and if one or more are active it will continue. The blocking will still be active when a next phone becomes active

Can you please explain what you mean with "reset the script"?

Code: Select all

-- One script to switch the scene 'Woonkamer aan' after checking time / presence state / scene state.


local blockingScenes =
{
    'watch movie',
    'watch tv',   -- example to show you can have multiple blocking scenes
}

local blockingGroups =
{
    'romantic lights', -- Scenes and groups are distinct so need to be addressed separate
    'another Group',
}

local myPhones =      -- change to names of all dummy switch
{
    'name of Phone Switch1',
    'name of Phone Switch2',
}

return
{
    on =
    {
        scenes = blockingScenes, -- if one or more of these scenes / groups are activated after sunset
        groups = blockingGroups, -- the switchingScene will be blocked until next sunset
        devices = myPhones,

        timer =
        {
            'at sunset',
            'at sunrise',
        },

    },

    data =
    {
        blockers =
        {
            initial = {},
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- change to domoticz.LOG_ERROR when all OK
        marker = 'scene Control',
    },

    execute = function(dz, item)
        local switchingScene = dz.scenes('woonkamer aan') -- change to name of your switchingScene

        local function phoneActive(t)
            for _, phone in ipairs(t) do
                if dz.devices(phone).active then
                    return true
                end
            end
        end

        local function switchAllowed(t)
            for scene, blocked in pairs(t) do
                if blocked then
                    dz.log(scene .. ' is blocking the switch', dz.LOG_DEBUG)
                    return false
                end
            end
            return true
        end

        if item.isScene or item.isGroup and item.state ~= 'Off' and dz.time.matchesRule('between sunset and 23:59') then
            dz.data.blockers[item.name] = true
            return
        end

        if item.isTimer and dz.time.matchesRule('at sunrise') then
           dz.data.blockers = {} -- reset blockers
           return
        end

        if phoneActive(myPhones) and dz.time.matchesRule('between sunset and 23:59') then -- This will be evaluated at sunset and when a phone becomes active after sunset
            if switchAllowed(dz.data.blockers) then
                switchingScene.switchOn()
            end
        end
    end
} 
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Cndiann

Re: Presence detection

Post by Cndiann »

Thanks I will test it when I'm at home.

If I want to leave the house for example at 22:00 I switch off all the lights. And when I come home at 23:00 I want to start the script again so it does switch Woonkamer aan.

But now it only does after midnight.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Presence detection

Post by waaren »

Cndiann wrote: Friday 23 October 2020 16:27 If I want to leave the house for example at 22:00 I switch off all the lights. And when I come home at 23:00 I want to start the script again so it does switch Woonkamer aan.
OK,

something like this? (untested)

Code: Select all

 -- One script to switch the scene 'Woonkamer aan' after checking time / presence state / scene state.

local blockingScenes =
{
    'watch movie',
    'watch tv',   -- example to show you can have multiple blocking scenes
}

local blockingGroups =
{
    'romantic lights', -- Scenes and groups are distinct so need to be addressed separate
    'another Group',
}

local myPhones =      -- change to names of all dummy switch
{
    'name of Phone Switch1',
    'name of Phone Switch2',
}

local resets =
{
    'leaving home',
}

local function mergeArrays(t1, t2)
    local t3 = {}
    for _,v in ipairs(t1) do  table.insert(t3, v) end
    for _,v in ipairs(t2) do  table.insert(t3, v) end
    return t3
end

allScenes = mergeArrays(blockingScenes, resets)
allGroups = mergeArrays(blockingGroups, resets)


return
{
    on =
    {
        scenes = allScenes, -- if one or more of the blocking scenes / groups are activated after sunset
        groups = allGroups, -- the switchingScene will be blocked until next sunset. If a resets scene / group then the block is lifted
        devices = myPhones,

        timer =
        {
            'at sunset',
            'at sunrise',
        },

    },

    data =
    {
        blockers =
        {
            initial = {},
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- change to domoticz.LOG_ERROR when all OK
        marker = 'scene Control',
    },

    execute = function(dz, item)
        local switchingScene = dz.scenes('woonkamer aan') -- change to name of your switchingScene

        local function phoneActive(t)
            for _, phone in ipairs(t) do
                if dz.devices(phone).active then
                    return true
                end
            end
        end

        local function switchAllowed(t)
            for scene, blocked in pairs(t) do
                if blocked then
                    dz.log(scene .. ' is blocking the switch', dz.LOG_DEBUG)
                    return false
                end
            end
            return true
        end

        function isReset(scene)
            for _, v in ipairs(resetScenes)do
                if v == scene then
                    return true
                end
            end
            return false
        end

        if item.isScene or item.isGroup and item.state ~= 'Off' and dz.time.matchesRule('between sunset and 23:59') then
            if isReset(item.name) then
                dz.data.blockers = {} -- reset blockers
            else
                dz.data.blockers[item.name] = true
            end
            return
        end

        if item.isTimer and dz.time.matchesRule('at sunrise') then
           dz.data.blockers = {} -- reset blockers
           return
        end

        if phoneActive(myPhones) and dz.time.matchesRule('between sunset and 23:59') then -- This will be evaluated at sunset and when a phone becomes active after sunset
            if switchAllowed(dz.data.blockers) then
                switchingScene.switchOn()
            end
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Cndiann

Re: Presence detection  [Solved]

Post by Cndiann »

Thanks for the script! It does seems half to work. When the script activates Woonkamer aan, and after that I set the scene Film kijken on then when I disconnect from wifi and get back on wifi the script starts Woonkamer aan again.

And even when phone 1 is on and I have set scene Film kijken on and Phone 2 comes home the script will turn Woonkamer aan on.

I also get this error:

Code: Select all

020-10-23 20:58:41.907 Error: dzVents: Error: (3.0.2) scene Control: An error occurred when calling event handler Telefoon
2020-10-23 20:58:41.907 Error: dzVents: Error: (3.0.2) scene Control: .../domoticz/scripts/dzVents/generated_scripts/Telefoon.lua:88: bad argument #1 to 'ipairs' (table expected, got nil) 
I use this script

Code: Select all

-- One script to switch the scene 'Woonkamer aan' after checking time / presence state / scene state.

local blockingScenes =
{
    'Film kijken',  -- example to show you can have multiple blocking scenes
}

local blockingGroups =
{
   'Film kijken',  -- Scenes and groups are distinct so need to be addressed separate
 
}

local myPhones =      -- change to names of all dummy switch
{
    'Telefoon 1',
    'Telefoon 2',
}

local resets =
{
    'Niet thuis',
}

local function mergeArrays(t1, t2)
    local t3 = {}
    for _,v in ipairs(t1) do  table.insert(t3, v) end
    for _,v in ipairs(t2) do  table.insert(t3, v) end
    return t3
end

allScenes = mergeArrays(blockingScenes, resets)
allGroups = mergeArrays(blockingGroups, resets)


return
{
    on =
    {
        scenes = allScenes, -- if one or more of the blocking scenes / groups are activated after sunset
        groups = allGroups, -- the switchingScene will be blocked until next sunset. If a resets scene / group then the block is lifted
        devices = myPhones,

        timer =
        {
            'at sunset',
            'at sunrise',
        },

    },

    data =
    {
        blockers =
        {
            initial = {},
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- change to domoticz.LOG_ERROR when all OK
        marker = 'scene Control',
    },

    execute = function(dz, item)
        local switchingScene = dz.scenes('Woonkamer aan') -- change to name of your switchingScene

        local function phoneActive(t)
            for _, phone in ipairs(t) do
                if dz.devices(phone).active then
                    return true
                end
            end
        end

        local function switchAllowed(t)
            for scene, blocked in pairs(t) do
                if blocked then
                    dz.log(scene .. ' is blocking the switch', dz.LOG_DEBUG)
                    return false
                end
            end
            return true
        end

        function isReset(scene)
            for _, v in ipairs(resetScenes)do
                if v == scene then
                    return true
                end
            end
            return false
        end

        if item.isScene or item.isGroup and item.state ~= 'Off' and dz.time.matchesRule('between sunset and 23:59') then
            if isReset(item.name) then
                dz.data.blockers = {} -- reset blockers
            else
                dz.data.blockers[item.name] = true
            end
            return
        end

        if item.isTimer and dz.time.matchesRule('at sunrise') then
           dz.data.blockers = {} -- reset blockers
           return
        end

        if phoneActive(myPhones) and dz.time.matchesRule('between sunset and 23:59') then -- This will be evaluated at sunset and when a phone becomes active after sunset
            if switchAllowed(dz.data.blockers) then
                switchingScene.switchOn()
            end
        end
    end
}
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Presence detection

Post by waaren »

Cndiann wrote: Friday 23 October 2020 21:04 I also get this error:

Code: Select all

020-10-23 20:58:41.907 Error: dzVents: Error: (3.0.2) scene Control: An error occurred when calling event handler Telefoon
2020-10-23 20:58:41.907 Error: dzVents: Error: (3.0.2) scene Control: .../domoticz/scripts/dzVents/generated_scripts/Telefoon.lua:88: bad argument #1 to 'ipairs' (table expected, got nil) 
First solve this error before checking again; change the string "resetScenes" to "resets"
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Cndiann

Re: Presence detection

Post by Cndiann »

I have changed it to

Code: Select all

 function isReset(scene)
            for _, v in ipairs(reset)do
                if v == scene then
                    return true
                    
The scene Woonkamer aan starts when I turn on wifi on my phone. But if I then choose scene Film kijken, turn off Wifi and turn Wifi back on the scene Woonkamer aan starts.


2020-10-23 23:56:36.174 Status: User: ***** initiated a scene/group command
2020-10-23 23:56:36.300 Status: dzVents: Info: Handling events for: "Film kijken", value: "On"
2020-10-23 23:56:36.300 Status: dzVents: Info: scene Control: ------ Start internal script: Telefoon: Scene: "Film kijken", Index: 1
2020-10-23 23:56:36.301 Status: dzVents: Debug: scene Control: Processing device-adapter for Woonkamer aan: Scene device adapter
2020-10-23 23:56:36.301 Status: dzVents: Info: scene Control: ------ Finished Telefoon
2020-10-23 23:56:36.301 Status: dzVents: Info: scene Control: ------ Start internal script: Telefoon: Scene: "Film kijken", Index: 1
2020-10-23 23:56:36.302 Status: dzVents: Info: scene Control: ------ Finished Telefoon
2020-10-23 23:56:36.301 Error: dzVents: Error: (3.0.2) scene Control: An error occurred when calling event handler Telefoon
2020-10-23 23:56:36.301 Error: dzVents: Error: (3.0.2) scene Control: .../domoticz/scripts/dzVents/generated_scripts/Telefoon.lua:88: bad argument #1 to 'ipairs' (table expected, got nil)
2020-10-23 23:56:36.301 Error: dzVents: Error: (3.0.2) scene Control: An error occurred when calling event handler Telefoon
2020-10-23 23:56:36.302 Error: dzVents: Error: (3.0.2) scene Control: .../domoticz/scripts/dzVents/generated_scripts/Telefoon.lua:88: bad argument #1 to 'ipairs' (table expected, got nil)
Cndiann

Re: Presence detection

Post by Cndiann »

Edit;

It works now! I was forgot the -s! Resets instead of reset.

Many thanks!
Cndiann

Re: Presence detection

Post by Cndiann »

Sorry to open again.

But I doesn't reset the script after sunrise and doesn't activate next sunset.

I first have to start the scene 'niet thuis' then it reset the script
Cndiann

Re: Presence detection

Post by Cndiann »

This is my current script. I want the script active after sunset till 02:00. Afther 02:00 it needs to reset so it will be activated at next sunset.
The problem now is the script doens't reset after 02:00 and be active next sunset, only when I have switch the scene 'Niet thuis'

Code: Select all

-- One script to switch the scene 'Woonkamer aan' after checking time / presence state / scene state.

local blockingScenes =
{
    'Film kijken',  -- example to show you can have multiple blocking scenes
}

local blockingGroups =
{
   'Film kijken',  -- Scenes and groups are distinct so need to be addressed separate
 
}

local myPhones =      -- change to names of all dummy switch
{
    'Telefoon Stefan',
    'Telefoon Sigrid',
}

local resets =
{
    'Niet thuis',
}

local function mergeArrays(t1, t2)
    local t3 = {}
    for _,v in ipairs(t1) do  table.insert(t3, v) end
    for _,v in ipairs(t2) do  table.insert(t3, v) end
    return t3
end

allScenes = mergeArrays(blockingScenes, resets)
allGroups = mergeArrays(blockingGroups, resets)


return
{
    on =
    {
        scenes = allScenes, -- if one or more of the blocking scenes / groups are activated after sunset
        groups = allGroups, -- the switchingScene will be blocked until next sunset. If a resets scene / group then the block is lifted
        devices = myPhones,

        timer =
        {
            'at sunset',
            'at 02:00',
        },

    },

    data =
    {
        blockers =
        {
            initial = {},
        },
    },

    logging =
    {
        level = domoticz.LOG_ERROR, -- change to domoticz.LOG_ERROR when all OK
        marker = 'scene Control',
    },

    execute = function(dz, item)
        local switchingScene = dz.scenes('Woonkamer aan') -- change to name of your switchingScene

        local function phoneActive(t)
            for _, phone in ipairs(t) do
                if dz.devices(phone).active then
                    return true
                end
            end
        end

        local function switchAllowed(t)
            for scene, blocked in pairs(t) do
                if blocked then
                    dz.log(scene .. ' is blocking the switch', dz.LOG_DEBUG)
                    return false
                end
            end
            return true
        end

        function isReset(scene)
            for _, v in ipairs(resets)do
                if v == scene then
                    return true
                end
            end
            return false
        end

        if item.isScene or item.isGroup and item.state ~= 'Off' and dz.time.matchesRule('between sunset and 02:00') then
            if isReset(item.name) then
                dz.data.blockers = {} -- reset blockers
            else
                dz.data.blockers[item.name] = true
            end
            return
        end

        if item.isTimer and dz.time.matchesRule('at 02:00') then
           dz.data.blockers = {} -- reset blockers
           return
        end

        if phoneActive(myPhones) and dz.time.matchesRule('between sunset and 02:00') then -- This will be evaluated at sunset and when a phone becomes active after sunset
            if switchAllowed(dz.data.blockers) then
                switchingScene.switchOn()
            end
        end
    end
}
EDIT I gues it's working now, I will let you know tomorrow.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Presence detection

Post by waaren »

Cndiann wrote: Sunday 25 October 2020 18:23 This is my current script. I want the script active after sunset till 02:00. Afther 02:00 it needs to reset so it will be activated at next sunset.
The problem now is the script doens't reset after 02:00 and be active next sunset, only when I have switch the scene 'Niet thuis'

[EDIT] I guess it's working now, I will let you know tomorrow.
Best to add some extra debug loglines in the script so you can quickly see what flow the code follows. That will make it easier to identify issues
By setting the log level to domoticz.LOG_ERROR you won't get the debug lines in your log during normal use.


like

Code: Select all

-- One script to switch the scene 'Woonkamer aan' after checking time / presence state / scene state.

local blockingScenes =
{
    'Film kijken',  -- example to show you can have multiple blocking scenes
}

local blockingGroups =
{
   'Film kijken',  -- Scenes and groups are distinct so need to be addressed separate
 
}

local myPhones =      -- change to names of all dummy switch
{
    'Telefoon Stefan',
    'Telefoon Sigrid',
}

local resets =
{
    'Niet thuis',
}

local function mergeArrays(t1, t2)
    local t3 = {}
    for _,v in ipairs(t1) do  table.insert(t3, v) end
    for _,v in ipairs(t2) do  table.insert(t3, v) end
    return t3
end

allScenes = mergeArrays(blockingScenes, resets)
allGroups = mergeArrays(blockingGroups, resets)


return
{
    on =
    {
        scenes = allScenes, -- if one or more of the blocking scenes / groups are activated after sunset
        groups = allGroups, -- the switchingScene will be blocked until next sunset. If a resets scene / group then the block is lifted
        devices = myPhones,

        timer =
        {
            'at sunset',
            'at 02:00',
        },

    },

    data =
    {
        blockers =
        {
            initial = {},
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- change to domoticz.LOG_ERROR when all OK
        marker = 'scene Control',
    },

    execute = function(dz, item)
        local switchingScene = dz.scenes('Woonkamer aan') -- change to name of your switchingScene
        
        if not(item.isTimer) then 
            dz.log(item.name .. '( a ' .. item.baseType .. ') triggered the script. State is ' .. item.state , dz.LOG_DEBUG )
        end

        local function phoneActive(t)
            for _, phone in ipairs(t) do
                if dz.devices(phone).active then
                    return true
                end
            end
            dz.log('-- debug phoneActive' , dz.LOG_DEBUG )
        end

        local function switchAllowed(t)
            for scene, blocked in pairs(t) do
                if blocked then
                    dz.log(scene .. ' is blocking the switch', dz.LOG_DEBUG)
                    return false
                end
            end
            dz.log('-- debug switchAllowed ', dz.LOG_DEBUG )
            return true
        end

        function isReset(scene)
            for _, v in ipairs(resets)do
                if v == scene then
                    return true
                end
            end
            dz.log('-- debug isReset ', dz.LOG_DEBUG )
            return false
        end

        if item.isScene or item.isGroup and item.state ~= 'Off' and dz.time.matchesRule('between sunset and 02:00') then
            dz.log('-- debug 1' , dz.LOG_DEBUG )
            if isReset(item.name) then
                dz.log('-- debug 2' , dz.LOG_DEBUG )
                dz.data.blockers = {} -- reset blockers
            else
                dz.data.blockers[item.name] = true
            end
            return
        end

        if item.isTimer and dz.time.matchesRule('at 02:00') then
            dz.log('-- debug 3' , dz.LOG_DEBUG )
           dz.data.blockers = {} -- reset blockers
           return
        end

        if phoneActive(myPhones) and dz.time.matchesRule('between sunset and 02:00') then -- This will be evaluated at sunset and when a phone becomes active after sunset
            dz.log('-- debug 4' , dz.LOG_DEBUG )
            if switchAllowed(dz.data.blockers) then
                dz.log('-- debug 5' , dz.LOG_DEBUG )
                switchingScene.switchOn()
            end
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Cndiann

Re: Presence detection

Post by Cndiann »

Script is working perfectly! Thanks again.
User avatar
Nateonas
Posts: 6
Joined: Wednesday 20 June 2018 20:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: NL
Contact:

Re: Presence detection

Post by Nateonas »

Since I could not find a simple solution for IP presence detection on Domoticz, I wrote one myself. It only requires a single dzVentz script, dzPresence. It uses simple ping shell commands and supports any number of devices. You can also create an "anyone home"-switch.

See: https://github.com/Nateonas/Domoticz_Dz ... eDetection

Enjoy!

Perhaps anyone can (tell me how to) update the outdated Domoticz Presence Detection Wiki (https://www.domoticz.com/wiki/Presence_detection), than I'm happy to know!
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 1 guest