How do you write a Lu code to check for sunrise and sunset

I currently have my door lock turn on my light to my stairs when I come in but does so day and night. I would like to turn on only at night or 30 mins after sunset but not during the day. Thanks.

http://wiki.micasaverde.com/index.php/Luup_Lua_extensions#function:_is_night
http://wiki.micasaverde.com/index.php/Luup_Scenes_Events#Calculate_sunrise_and_sunset

For simplicity, I suggest just turning the light on after sunset and before sunrise. The addition of 30 minutes after sunset adds a bit more code and complexity.

Add the following code to the scene (Luup tab) that turns on the light:

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

Returning true causes the commands in the Command tab will run; returning false prevents them from running.

Guys thanks so much you’re a life saver. Just learning I’ll get better at this.

The same code could be written as a one-liner as well:

return luup.is_night()

This information should really be added to

http://wiki.micasaverde.com/index.php/Luup_Scenes_Events

I think wanting “lights on” types of events to happen only at night is such a basic home automation need that it should be more prominently featured in the wiki.

Adding “return luup.is_night()” works great and is easy to do, but it took me a lot of searching to locate this information. I went as far as to install DAD before I ran across this thread. :slight_smile:

[quote=“djrobx, post:6, topic:167375”]This information should really be added to

http://wiki.micasaverde.com/index.php/Luup_Scenes_Events[/quote]
Then go ahead and add it :wink:

Anyone can create an account on that system… It goes through an approval step by MCV’s Team, and then you’ll be free to edit the Wiki pages there.

It’s Easter all year 'round at MCV. Go find them eggs. ;D

[code][quote=“Guard, post:5, topic:167375”]The same code could be written as a one-liner as well:

return luup.is_night()

Is the function luup.is_ night()[/code] simply built into UI4 now so the system automatically knows the date and time of day to determine the correct sunset/sunrise time? I just ordered my Vera2 today and am reading as much as possible to start learning, so thanks for any help. Coding seems pretty straight forward to other simple coding I have played with.

@Cherokee180c

Yes, this is part of the Luup engine. The sunrise/sunset time are determined based on your location.

Thanks