Turning lights off after 5 min during the night

Hi guys.

I have a motion detector set to trigger the “turn lights on” scene when motion is detected and light level is lower than 10% (luup code). The lights then stay on until I manually turn them off when I go to bed. This is OK, as I wanted. The problem is if I go to the toilet at night, the lights turn on and then I have to manually turn them off (or they will be on all night).

How do I modify the scene to turn the lights off automatically after 5 minutes but only during the night (between 00:00 and 06:00)?

Thanks for all your help.


What version of the UI are you running? If you are running UI5, you can edit the scene that you want to add a time. To the right of the scene name in the editor mode, you will see a drop down menu with the words immediate. Click on the drop down menu and click on manage delays. There you can enter 5 minutes. Once you enter the 5 minutes and click close, you can then select the light that you want to turn off. Once done, just click on the confirm changes button and then the save button.

  • Garrett

Garrett hi,

thanks for the reply.

I am using UI5, yes. I know about this feature you mentioned… but if I do that, then the lights will alway go off after 5 minutes which is not what I want.

I want the lights to go off after 5 minutes only during the night (from 00:00 to 06:00)… otherwise the lights should stay on “forever” / until I turn them of manually.

I was looking into “schedules” but there is no way to specify this option that I want. I think there should be some kind of a luup code that checks the time when sensor is tripped and if the time is in this interval (00:00 - 06:00) the timer is set to 5 minutes and then the “lights off” scene is triggered. This is my logical thinking, but unfortunately I don’t know how to program this :frowning:


an easy approach: create a new scene that triggers when the light is turned on, add the following code in LUUP:

if (luup.is_night()) then return true else return false end

add a 5 minute delay (as Garrett said) and turn the light off. The scene won’t run if it’s night time.

edit: meant to say it won’t run unless it’s night time…

Great, it seems to be working :slight_smile:

I also would like to know what time period “is.night” defines?

Because now there is another issue: every time a light is turned on, the scene switches it off after 5 minutes (the way i set it). Even if I turn on the light myself. So, if this is happening before 00:00 it is not OK… how can I tell Vera that is.night is actually after 00:00?


Oops! you are right. is_night is after sunset and before sunrise. Change the code as follows to do what you want:

local t = os.date('*t') if (t.hour>=0) and (t.hour<=5) then return true else return false end

now it will only run between 00 and 06.

Capjay thank you, this is exactly what I was looking for!

Can I ask you how could I also incorporate minutes in this code? For instance, if I wanted to change the “night time” to 23:30 till 6:30?


Have a look at this post to get you started:


Basically you want to convert the current time and the time that you want to seconds and compare from there.

  • Garrett

I made this a long time ago and not sure if it works or not. But another way of doing it. You give it a start time and an end time. This will work for you as your start time is before midnight and the end time is after midnight.

local startTime = "10:30"
local endTime   = "22:00"

local startHour = tonumber( startTime:sub( startTime:find("%d+") ) )
local startMinute = tonumber(startTime:sub(-2))
if startHour and startMinute then
    startTime = startHour * 100 + startMinute
    luup.log("ERROR: invalid start time")
    return false
endHour = tonumber( endTime:sub( endTime:find("%d+") ) )
endMinute = tonumber(endTime:sub(-2))
if endHour and endMinute then
    endTime = endHour * 100 + endMinute
    luup.log("ERROR: invalid end time")
    return false
local currentTime = os.date("*t")
currentTime = currentTime.hour * 100 + currentTime.min
if startTime <= endTime then
    -- Both the start time and the end time are in the same day:
    -- if the current time is in the given interval, run the scene.
    if startTime <= currentTime and currentTime <= endTime then
        -- YOUR CODE HERE --
        return true
    -- The start time is before midnight, and the end time is after midnight:
    -- if the current time is not outside the given interval, run the scene.
    if not (endTime < currentTime and currentTime < startTime) then
        -- YOUR CODE HERE --
        return true
return false