I try to turn off light in my living room automatically. But I'm facing a problem. When I use time between 16 and 23, then it works perfekt. But when I use the time between 23 and 6 in the morning, the script does not work anymore. Does anybody know, how I can make a time shift over midnight? The script looks like this:
dayNow = tonumber(os.date('%w'))
hourNow = tonumber(os.date('%H'))
if (globalvariables['Security']) == 'Disarmed' or devicechanged['Dummy Disarmed'] == 'On' then
if ((devicechanged['Bewegungssensor_WZ'] == 'On' and tonumber(w) < 20) and otherdevices['Licht_WZ'] == 'Off') then
commandArray['Licht_WZ'] = 'On'
elseif ((otherdevices['Bewegungssensor_WZ'] == 'Off' and tonumber(w) < 20) and otherdevices['Licht_WZ'] == 'On') then
if (((dayNow > 0) and (dayNow < 6)) and ((hourNow > 16) and (hourNow <= 23))) then
commandArray['Licht_WZ'] = 'Off AFTER 3600'
print('Es ist Wochentag zwischen 16 und 23 Uhr')
elseif (((dayNow > 0) or (dayNow < 6) and (hourNow > 23) and (hourNow <= 6))) then
commandArray['Licht_WZ'] = 'Off AFTER 3600'
print('Es ist Wochentag zwischen 17 und 22 Uhr')
elseif (((dayNow == 0) or (dayNow == 6) and (hourNow > 19) and (hourNow < 6))) then
commandArray['Licht_WZ'] = 'Off AFTER 1200'
print('Es ist Wochenende zwischen 22 und 6 Uhr')
elseif (((dayNow == 0) or (dayNow == 6) and (hourNow > 17) and (hourNow <= 19))) then
commandArray['Licht_WZ'] = 'Off AFTER 3600'
print('Es ist Wochenende zwischen 17 und 22 Uhr')
end
A day starts at 00:00
So you can put in something like: time => 23:00 and time < 00:00 or time => 00:00 and < 06:00
Another way to make this even easier and your script more compact is to make a virtual switch 'switch1' and add two timers here:
command on at 23:00 and a command off at 06:00.
In your original script, instead of putting in the times, you can check the status of this switch to turn your light on or off