Random lights turning on and off while on vacation

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

Moderator: leecollings

Post Reply
User avatar
RvdM
Posts: 39
Joined: Friday 10 July 2015 21:06
Target OS: Linux
Domoticz version: 2020.2 1
Location: Deventer, NL
Contact:

Random lights turning on and off while on vacation

Post 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
Running virtualized domoticz server on a proxmox cluster with a slave for optimal RFX communication. Using Aeotec Z-Stick Gen5, rfxtrx433e, Homebridge and a Philips HUE bridge for all sort of home automation.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Random lights turning on and off while on vacation

Post 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
}


Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
waltervl
Posts: 5715
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Random lights turning on and off while on vacation

Post 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).
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
jake
Posts: 742
Joined: Saturday 30 May 2015 22:40
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

Re: Random lights turning on and off while on vacation

Post 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.
User avatar
RvdM
Posts: 39
Joined: Friday 10 July 2015 21:06
Target OS: Linux
Domoticz version: 2020.2 1
Location: Deventer, NL
Contact:

Re: Random lights turning on and off while on vacation

Post 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
}

Last edited by RvdM on Tuesday 27 April 2021 13:41, edited 1 time in total.
Running virtualized domoticz server on a proxmox cluster with a slave for optimal RFX communication. Using Aeotec Z-Stick Gen5, rfxtrx433e, Homebridge and a Philips HUE bridge for all sort of home automation.
User avatar
RvdM
Posts: 39
Joined: Friday 10 July 2015 21:06
Target OS: Linux
Domoticz version: 2020.2 1
Location: Deventer, NL
Contact:

Re: Random lights turning on and off while on vacation

Post 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)
Running virtualized domoticz server on a proxmox cluster with a slave for optimal RFX communication. Using Aeotec Z-Stick Gen5, rfxtrx433e, Homebridge and a Philips HUE bridge for all sort of home automation.
ilkaskim
Posts: 19
Joined: Friday 20 August 2021 2:20
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Germany
Contact:

Re: Random lights turning on and off while on vacation

Post 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
}

ilkaskim
Posts: 19
Joined: Friday 20 August 2021 2:20
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Germany
Contact:

Re: Random lights turning on and off while on vacation

Post by ilkaskim »

After midnight it suddenly worked.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest