If motion triggered between sunset and sunrise, control device

Post Reply
ben53252642
Posts: 524
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

If motion triggered between sunset and sunrise, control device

Post by ben53252642 » Saturday 09 June 2018 22:39

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

dannybloe
Posts: 1481
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi
Domoticz version:
Location: Ermelo
Contact:

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

Post by dannybloe » Sunday 10 June 2018 23:20

Use dzVents. it's all in there with just a few lines of code (and plain Lua).
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.

ben53252642
Posts: 524
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

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

Post by ben53252642 » Sunday 10 June 2018 23:41

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
Last edited by ben53252642 on Monday 11 June 2018 7:04, edited 1 time in total.

User avatar
waaren
Posts: 1216
Joined: Tuesday 03 January 2017 15:18
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

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

Post by waaren » Sunday 10 June 2018 23:46

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 ?)
Raspberry (debian stretch via berryboot on Synology DS916+) , Domoticz (almost) latest Beta, , dzVents 2.6, RFLink, RFXtrx433e, P1, Youless, Harmony, Hue, Yeelight, Xiaomi, HomeWizard, Zwave, Amazon echo

ben53252642
Posts: 524
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

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

Post by ben53252642 » Monday 11 June 2018 7:00

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

User avatar
waaren
Posts: 1216
Joined: Tuesday 03 January 2017 15:18
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

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

Post by waaren » Monday 11 June 2018 8:37

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
Raspberry (debian stretch via berryboot on Synology DS916+) , Domoticz (almost) latest Beta, , dzVents 2.6, RFLink, RFXtrx433e, P1, Youless, Harmony, Hue, Yeelight, Xiaomi, HomeWizard, Zwave, Amazon echo

ben53252642
Posts: 524
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

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

Post by ben53252642 » Monday 11 June 2018 9:06

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

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests