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
If motion triggered between sunset and sunrise, control device
Moderator: leecollings
-
- Posts: 543
- Joined: Saturday 02 July 2016 5:17
- Target OS: Linux
- Domoticz version: Beta
- Contact:
If motion triggered between sunset and sunrise, control device
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
-
- Posts: 1355
- Joined: Friday 29 August 2014 11:26
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Location: Ermelo
- Contact:
Re: If motion triggered between sunset and sunrise, control device
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.
-
- Posts: 543
- Joined: Saturday 02 July 2016 5:17
- Target OS: Linux
- Domoticz version: Beta
- Contact:
Re: If motion triggered between sunset and sunrise, control device
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.
Nighttime can be changed to Daytime in the script if you need the opposite.
Cheers
Code: Select all
commandArray = {}
if (devicechanged["Living Room"] == 'On' and timeofday['Nighttime']) then
commandArray['Main Bedroom Hallway'] = 'On'
end
return commandArray
Cheers
Last edited by ben53252642 on Monday 11 June 2018 7:04, edited 1 time in total.
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
- waaren
- Posts: 6028
- Joined: Tuesday 03 January 2017 14:18
- Target OS: Linux
- Domoticz version: Beta
- Location: Netherlands
- Contact:
Re: If motion triggered between sunset and sunrise, control device
If I understand your requirement correctly it can be implemented as a device triggered Lua script like
[EDIT] My posting just crossed yours
(btw: you don't need to switch the light off again ?)
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

Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
-
- Posts: 543
- Joined: Saturday 02 July 2016 5:17
- Target OS: Linux
- Domoticz version: Beta
- Contact:
Re: If motion triggered between sunset and sunrise, control device
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.
Also uses this to accelerate the device control (in some instances):
viewtopic.php?f=61&t=21336
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
viewtopic.php?f=61&t=21336
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
- waaren
- Posts: 6028
- Joined: Tuesday 03 January 2017 14:18
- Target OS: Linux
- Domoticz version: Beta
- Location: Netherlands
- Contact:
Re: If motion triggered between sunset and sunrise, control device
Understand. Nice use of priority based switching !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.
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
Code: Select all
for i = 1, #numdevices do
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
-
- Posts: 543
- Joined: Saturday 02 July 2016 5:17
- Target OS: Linux
- Domoticz version: Beta
- Contact:
Re: If motion triggered between sunset and sunrise, control device
waaren, thank you for making the code even better!
This works without the extra loop (had to use #devices):
Cheers
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
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
Who is online
Users browsing this forum: No registered users and 1 guest