Page 1 of 1

Time over midnight

Posted: Thursday 19 January 2017 21:17
by Mikey
Hi

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:

Code: Select all

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
		
Can anybody help me with the midnight problem?

Re: Time over midnight

Posted: Thursday 19 January 2017 21:28
by desertdog
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

Re: Time over midnight

Posted: Thursday 19 January 2017 21:35
by Mikey
Perfect, thanks for your help. I will try this. Do you also know this:

If i put in time.hour < 23

Will this stop at 23:01 or will this stop at 0:00?

Re: Time over midnight

Posted: Thursday 19 January 2017 21:41
by desertdog
I am not sure. It could be that the clock never reaches 24 as after 23:59 it resets to 00:00.