Page 1 of 1

Random lights turning on and off while on vacation

Posted: Monday 26 April 2021 13:42
by RvdM
For my holiday plans I would like to make a dzVents script that will randomly turn on light and off when we're not at home.
Now I already have a dzVents script that will turn on and off light but I was thinking something like turning a light on in the hall, driveway, toilet, or bedroom based on a random number match.

CreateRandomNumber between 1 and 10
If CreateRandomNumber = 4 then Turn on a random light from a list of devices for about 10 minutes.
And run this script every minute between X and Y.

The "run between X and Y" I have experience with but not how to create a randomnumber and select a random device from a list of devices.
Perhaps someone on this forum has already build something like this and can help me out.

Thanks in advane

Re: Random lights turning on and off while on vacation

Posted: Monday 26 April 2021 15:24
by waaren
RvdM wrote: Monday 26 April 2021 13:42 The "run between X and Y" I have experience with but not how to create a randomnumber and select a random device from a list of devices.
Perhaps someone on this forum has already build something like this and can help me out.
Script could look like below.

Code: Select all

return
{
    on =
    {
        timer =
        {
            'at nighttime at 17:00-01:00',
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'Random lights',
    },

    execute = function(dz)
        myDevices =
        {
            'light1',  -- list of names of your devices
            'light2',
            'light3',
            'lightn',
        }

        math.randomseed( os.time() )
        local randomNumber = math.random(1, 10)

        if randomNumber == 4 then
            local randomDevice = dz.devices(myDevices[ math.random(1, #myDevices)] )
            local randomTime = math.random(400,700)
            dz.log('Device ' .. randomDevice.name .. ' will be switched on for ' .. randomTime .. ' seconds.' , dz.LOG_DEBUG)
            randomDevice.cancelQueuedCommands()
            randomDevice.switchOn().silent()
            randomDevice.switchOff().silent().afterSec(randomTime)
        else
            dz.log('No action now. randomNumber is ' .. randomNumber , dz.LOG_DEBUG)
        end

    end
}



Re: Random lights turning on and off while on vacation

Posted: Monday 26 April 2021 15:48
by waltervl
For future reference for people that come across this post and do not want to script: There is also the Random timer setting that can be used on Domoticz timer plans.
So with this you can create an holiday timer plan with lights that switch on/off in a random time frame (+/- the random timer setting in minutes)
It only does not have the possibility to randomly select the lights. You will have deviate in your timer plan (even days/uneven days etc).

Re: Random lights turning on and off while on vacation

Posted: Monday 26 April 2021 16:36
by jake
waltervl wrote:For future reference for people that come across this post and do not want to script: There is also the Random timer setting that can be used on Domoticz timer plans.
So with this you can create an holiday timer plan with lights that switch on/off in a random time frame (+/- the random timer setting in minutes)
It only does not have the possibility to randomly select the lights. You will have deviate in your timer plan (even days/uneven days etc).
That is how I do it for years. I have thought about automating it by checking a motion sensor in the house to decide to let it kick in by itself: no motion longer than 24hr? Go into vacation mode and start executing script for random lights.

Re: Random lights turning on and off while on vacation

Posted: Tuesday 27 April 2021 13:36
by RvdM
waaren wrote: Monday 26 April 2021 15:24 Script could look like below.
This is amazing. Thank you for writing the core of the script.
I've made some small adjustments and put some comments at the top.
Added female behavioral simulation :lol:

Code: Select all

-- Script written by Waaren and modified bij RvdM
-- https://www.domoticz.com/forum/viewtopic.php?f=59&t=36140&sid=fbe5c6002dc7b1d092c4dc30af5d61b7
--
-- The script randomly picks a number between 1 and 20 and when it is 1
-- it will turn on a light (which is defined in an array) for a random amount 
-- of seconds.Running it every minute results in a 5% chance. Running it
-- once every 2 minutes reduces the chance to 2.5% and 4 minutes reduces
-- the change to 1.25%
--
-- Special note about the timer:
--   Before 00:00 this script will run once every 2 minutes, after 00:00 it 
--   will only run once every 4 minutes to decrease the chance of being activated. 
--   This simulates human (mostly female ;-)) behavior.
--
-- Important variables to look at: 
--   vakantiemode (the device which is required to be set to On)
--   myDevices (the array with devices)
--   randomNumber (the random number generated between 1 and 20)
--   randomNumberWait (the time it waits before it will turn on the light)

return
{
    on =
    {
        timer =
        {
            'at nighttime at 16:00-23:59 every 2 minutes', 
            'at nighttime at 00:00-08:00 every 4 minutes',  
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'Random lights',
    },

    execute = function(dz)
        
        holliday = dz.devices('Holliday Mode')

        if (holliday.state == 'On') then

            myDevices =
            {
                'Attic - Light',
                'Hall - Light',
            }

            math.randomseed( os.time() )
            
            local randomNumber = math.random(1, 20)
            local randomNumberWait = math.random(1, 59)

            if randomNumber == 1 then
                local randomDevice = dz.devices(myDevices[ math.random(1, #myDevices)] )
                local randomTime = math.random(23,314)
                dz.log('RandomNumber is ' .. randomNumber , dz.LOG_DEBUG)
                dz.log('Device ' .. randomDevice.name .. ' will be switched on after ' .. randomNumberWait .. ' seconds for ' .. randomTime .. ' seconds.' , dz.LOG_DEBUG)
                randomDevice.cancelQueuedCommands()
                randomDevice.switchOn().silent().afterSec(randomNumberWait)
                randomDevice.switchOff().silent().afterSec(randomTime)
            else
                dz.log('No action now. randomNumber is ' .. randomNumber , dz.LOG_DEBUG)
            end
        else
            dz.log('Turning lights on and off randomly only happens when Holliday Mode is ON' , dz.LOG_DEBUG)
        end
    end
}


Re: Random lights turning on and off while on vacation

Posted: Tuesday 27 April 2021 13:39
by RvdM
jake wrote: Monday 26 April 2021 16:36
waltervl wrote:For future reference for people that come across this post and do not want to script: There is also the Random timer setting that can be used on Domoticz timer plans.
So with this you can create an holiday timer plan with lights that switch on/off in a random time frame (+/- the random timer setting in minutes)
It only does not have the possibility to randomly select the lights. You will have deviate in your timer plan (even days/uneven days etc).
That is how I do it for years. I have thought about automating it by checking a motion sensor in the house to decide to let it kick in by itself: no motion longer than 24hr? Go into vacation mode and start executing script for random lights.
Holliday Mode for me is a virtual device which can be switched manually or it will turn on automatically once no one is home for more than 24 hours (geofencing with phone)

Re: Random lights turning on and off while on vacation

Posted: Monday 20 June 2022 19:31
by ilkaskim
Hello, I'm just trying to use this script but unfortunately I can't get any further. I'm also learning right now and I'm already failing at small things 😁
I created this script dzVents as device and entered my switches. Unfortunately, nothing changes and there is nothing in the log.
Other dzVents scripts work for me. Do variables need to be created?

RvdM wrote: Tuesday 27 April 2021 13:36
waaren wrote: Monday 26 April 2021 15:24 Script could look like below.
This is amazing. Thank you for writing the core of the script.
I've made some small adjustments and put some comments at the top.
Added female behavioral simulation :lol:

Code: Select all

-- Script written by Waaren and modified bij RvdM
-- https://www.domoticz.com/forum/viewtopic.php?f=59&t=36140&sid=fbe5c6002dc7b1d092c4dc30af5d61b7
--
-- The script randomly picks a number between 1 and 20 and when it is 1
-- it will turn on a light (which is defined in an array) for a random amount 
-- of seconds.Running it every minute results in a 5% chance. Running it
-- once every 2 minutes reduces the chance to 2.5% and 4 minutes reduces
-- the change to 1.25%
--
-- Special note about the timer:
--   Before 00:00 this script will run once every 2 minutes, after 00:00 it 
--   will only run once every 4 minutes to decrease the chance of being activated. 
--   This simulates human (mostly female ;-)) behavior.
--
-- Important variables to look at: 
--   vakantiemode (the device which is required to be set to On)
--   myDevices (the array with devices)
--   randomNumber (the random number generated between 1 and 20)
--   randomNumberWait (the time it waits before it will turn on the light)

return
{
    on =
    {
        timer =
        {
            'at nighttime at 16:00-23:59 every 2 minutes', 
            'at nighttime at 00:00-08:00 every 4 minutes',  
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'Random lights',
    },

    execute = function(dz)
        
        holliday = dz.devices('Holliday Mode')

        if (holliday.state == 'On') then

            myDevices =
            {
                'Attic - Light',
                'Hall - Light',
            }

            math.randomseed( os.time() )
            
            local randomNumber = math.random(1, 20)
            local randomNumberWait = math.random(1, 59)

            if randomNumber == 1 then
                local randomDevice = dz.devices(myDevices[ math.random(1, #myDevices)] )
                local randomTime = math.random(23,314)
                dz.log('RandomNumber is ' .. randomNumber , dz.LOG_DEBUG)
                dz.log('Device ' .. randomDevice.name .. ' will be switched on after ' .. randomNumberWait .. ' seconds for ' .. randomTime .. ' seconds.' , dz.LOG_DEBUG)
                randomDevice.cancelQueuedCommands()
                randomDevice.switchOn().silent().afterSec(randomNumberWait)
                randomDevice.switchOff().silent().afterSec(randomTime)
            else
                dz.log('No action now. randomNumber is ' .. randomNumber , dz.LOG_DEBUG)
            end
        else
            dz.log('Turning lights on and off randomly only happens when Holliday Mode is ON' , dz.LOG_DEBUG)
        end
    end
}


Re: Random lights turning on and off while on vacation

Posted: Tuesday 21 June 2022 1:32
by ilkaskim
After midnight it suddenly worked.