Page 1 of 1

If motion triggered between sunset and sunrise, control device

Posted: Saturday 09 June 2018 22:39
by ben53252642
Hey folks,

I'm trying to figure out how to control a device using an if condition only if it's between sunset and sunrise.

I have a light by the window which should only be activated when it is dark, I could use set times but I think between sunset and sunrise is the best way to do it.

It has to be in Lua since I've got a fairly complex "Device Control" script already running pretty much all the lights in my apartment.

Has anyone got a script to do this?

Cheers

Re: If motion triggered between sunset and sunrise, control device

Posted: Sunday 10 June 2018 23:20
by dannybloe
Use dzVents. it's all in there with just a few lines of code (and plain Lua).

Re: If motion triggered between sunset and sunrise, control device

Posted: Sunday 10 June 2018 23:41
by ben53252642
Thanks, I figured it out in Lua, here is an example script for anyone else looking to do the same. Domoticz has a built in feature called timeofday which makes it easy.

Code: Select all

commandArray = {}
if (devicechanged["Living Room"] == 'On' and timeofday['Nighttime']) then
commandArray['Main Bedroom Hallway'] = 'On'
end
return commandArray
Nighttime can be changed to Daytime in the script if you need the opposite.

Cheers

Re: If motion triggered between sunset and sunrise, control device

Posted: Sunday 10 June 2018 23:46
by waaren
If I understand your requirement correctly it can be implemented as a device triggered Lua script like

Code: Select all

commandArray = {}

if devicechanged['Your motion detector'] == 'On' then
    if timeofday['Nighttime'] then
       commandArray['light by the window'] = 'On'
       print ("light by the window ==>> On");
    end
elseif devicechanged['Your motion detector'] == 'Off' then
    commandArray['light by the window'] = 'Off'
    print ("light by the window ==>> Off");
end

return commandArray
[EDIT] My posting just crossed yours :D (btw: you don't need to switch the light off again ?)

Re: If motion triggered between sunset and sunrise, control device

Posted: Monday 11 June 2018 7:00
by ben53252642
waaren the script is just a quick example if anyone is looking how to use the function.

My use case is quite a bit more complex, it's a sub if statement that chooses which devices in the living room to control based on time of day.

Code: Select all

-- Device Last Updates
t1 = os.time()
    devices = {
        "Main Entrance Motion Sensor",
        "Living Room Motion"
        }
    numdevices = 0 -- Count number of devices in the array
    for index in pairs(devices) do
        numdevices = numdevices + 1
    end
    for i = 1, numdevices do
    s = otherdevices_lastupdate[devices[i]]
    year = string.sub(s, 1, 4)
    month = string.sub(s, 6, 7)
    day = string.sub(s, 9, 10)
    hour = string.sub(s, 12, 13)
    minutes = string.sub(s, 15, 16)
    seconds = string.sub(s, 18, 19)
    t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
    str = (devices[i] .. "LastUpdate")
    str = str:gsub("%s+", "")
    str = string.gsub(str, "%s+", "")
    _G[str] = (os.difftime (t1, t2))
    end

commandArray = {}

--- Living Room and Kitchen + Main Entrance Hallway
if (devicechanged["Main Entrance Motion Sensor"] == 'On' and LivingRoomMotionLastUpdate > 32 and otherdevices["Motion Detection"] == 'On' or
devicechanged["Living Room Motion"] == 'On' and MainEntranceMotionSensorLastUpdate > 32 and otherdevices["Motion Detection"] == 'On' or
devicechanged["Main Entrance Door Sensor"] == 'Open') then
    if (timeofday['Nighttime']) then
    devices = {"Main Entrance Hallway","Living Room Middle","Living Room Window","Kitchen","Fish Tank"}
    else
    devices = {"Main Entrance Hallway","Living Room Middle","Kitchen","Fish Tank"}
    end
    numdevices = 0
    for index in pairs(devices) do
        numdevices = numdevices + 1
    end
    for i = 1, numdevices do
        if (otherdevices[devices[i]] == 'Off') then -- Priority control
        commandArray[devices[i]] = 'On'
        print("Priority On:" .. devices[i])
        end
    end
    for i = 1, numdevices do
        if (otherdevices[devices[i]] == 'On') then -- Remaining control
        commandArray[devices[i]] = 'On'
        print("Remaining On:" .. devices[i])
        end
    end
end

return commandArray
Also uses this to accelerate the device control (in some instances):
viewtopic.php?f=61&t=21336

Re: If motion triggered between sunset and sunrise, control device

Posted: Monday 11 June 2018 8:37
by waaren
ben53252642 wrote: Monday 11 June 2018 7:00 waaren the script is just a quick example if anyone is looking how to use the function.
Understand. Nice use of priority based switching !

Just a hint: you make use of this snippet in your code to count the number of devices in your array

Code: Select all

numdevices = 0
for index in pairs(devices) do
        numdevices = numdevices + 1
end
for i = 1, numdevices do
Standard Lua offers you this to do the same with less codelines

Code: Select all

for i = 1, #numdevices do

Re: If motion triggered between sunset and sunrise, control device

Posted: Monday 11 June 2018 9:06
by ben53252642
waaren, thank you for making the code even better!

This works without the extra loop (had to use #devices):

Code: Select all

-- Device Last Updates
t1 = os.time()
    devices = {
        "Main Bathroom Motion",
        "Main Bedroom Hallway Motion",
        "Main Entrance Door Sensor",
        "Main Entrance Motion Sensor",
        "Living Room Motion"
        }
    for i = 1, #devices do
    s = otherdevices_lastupdate[devices[i]]
    year = string.sub(s, 1, 4)
    month = string.sub(s, 6, 7)
    day = string.sub(s, 9, 10)
    hour = string.sub(s, 12, 13)
    minutes = string.sub(s, 15, 16)
    seconds = string.sub(s, 18, 19)
    t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
    str = (devices[i] .. "LastUpdate")
    str = str:gsub("%s+", "")
    str = string.gsub(str, "%s+", "")
    _G[str] = (os.difftime (t1, t2))
    end

commandArray = {}

--- Living Room and Kitchen + Main Entrance Hallway
if (devicechanged["Main Entrance Motion Sensor"] == 'On' and LivingRoomMotionLastUpdate > 32 and otherdevices["Motion Detection"] == 'On' or
devicechanged["Living Room Motion"] == 'On' and MainEntranceMotionSensorLastUpdate > 32 and otherdevices["Motion Detection"] == 'On' or
devicechanged["Main Entrance Door Sensor"] == 'Open') then
    if (timeofday['Nighttime']) then
    devices = {"Main Entrance Hallway","Living Room Middle","Living Room Window","Kitchen","Fish Tank"}
    else
    devices = {"Main Entrance Hallway","Living Room Middle","Kitchen","Fish Tank"}
    end
    for i = 1, #devices do
        if (otherdevices[devices[i]] == 'Off') then -- Priority control
        commandArray[devices[i]] = 'On'
        print("Priority On:" .. devices[i])
        end
    end
    for i = 1, #devices do
        if (otherdevices[devices[i]] == 'On') then -- Remaining control
        commandArray[devices[i]] = 'On'
        print("Remaining On:" .. devices[i])
        end
    end
end

return commandArray
Cheers