Custom Sunrise/Sunset as scene trigger

I’ve had a Vera Edge UI7 for a few weeks now and am trying to figure out how best to turn on/off some lights at sunrise/sunset but for different location than the system location. If this seems weird, you are not wrong. I want to use this for environmental controls for tropical frogs. It would be a slick way to simulate the subtle seasonal changes in day length the animals would experience in the wild.

I downloaded an luup script for calculating sunrise and sunset and storing them as variables from another thread and it looks pretty straightforward to modify for any location of interest. Where I am stuck is figuring out how to use those variables to trigger an event. Do I need to create a virtual device and assign the sunrise/sunset variables to it? And where would the calculate sunrise/sunset script go so that it updates the variables each day?

I have reasonably good proficiency programming in python but luup is brand new to me and I’m struggling with understanding the bits and pieces of executing luup on the Edge. Any pointers would be much appreciated.

bbro, I think you are trying to find a $100 fix to a $5 dollar problem.

We have this built into the UI. All you have to do is create a scene to turn them on and one to turn them off. When you create the scene choose schedule as the trigger and then from the menu choose sunrise from the drop down menu.

@Marc, I think the point is that he wanted sunrise sunset from a diffrent location to his own.

@bbro, Is it simply a consistent offset time from your own sunrise and sunset of does that vary. If consistent you can still use built in with offset.

It would have to be on the same latitude, just east or west of your current location for that to be true.

It would have to be on the same latitude, just east or west of your current location for that to be true.[/quote]
Based on the tropical comment then, I guess consistent offset is no good.

So, we throw it back to the programmers.

One (easy) option is to schedule a scene to run every day and place the Lua code you have in there. Having calculated the necessary times you can simply schedule a timer request, specifically for the time(s) you require, which runs another bit of code which could reside the the Lua Startup area (a context shared with all scene code) to do whatever you want.

This would allow you to avoid the tedium of actually creating a virtual device.

I have reasonably good proficiency programming in python but luup is brand new to me and I'm struggling with understanding the bits and pieces of executing luup on the Edge. Any pointers would be much appreciated.

I hope this helps a bit - more detail available if required.

Wow! Thanks for all the quick responses! As was already pointed out, using an offset won’t work because I want to simulate sunrise/sunset at ~ 10 degrees latitude and I live at 45 degrees. One nice thing is that I’m in the same time zone though.

One (easy) option is to schedule a scene to run every day and place the Lua code you have in there. Having calculated the necessary times you can simply schedule a timer request, specifically for the time(s) you require, which runs another bit of code which could reside the the Lua Startup area (a context shared with all scene code) to do whatever you want.

This would allow you to avoid the tedium of actually creating a virtual device.

Yes, this helps. I did consider that I could set the sunrise/sunset (or remote day/night) parameters with a scene scheduled to run just after midnight each day. But I wasn’t sure where to put the on off requests. Could you tell me when the Startup Lua runs? Judging by the name, I thought maybe it only ran when the controller rebooted or Luup was reloaded and googling around didn’t turn up any answers. I guess this is not the case?

Thanks again

No, you were right the first time, it runs at startup. However, any global definitions you make there are persistent and in scope for any scene code. Your callback code can reside in a function there and do whatever it likes when invoked by the timer code you trigger in your ‘just after midnight’ scene.

However, any global definitions you make there are persistent and in scope for any scene code. Your callback code can reside in a function there and do whatever it likes when invoked by the timer code you trigger in your 'just after midnight' scene.

I see. That gives me something to chew on.

Right now I’m just trying to sort out some basics by executing the code in this thread http://forum.micasaverde.com/index.php?topic=2073.msg8132#msg8132 and setting the output to variables of a virtual switch. I thought if I ran it in the Test Lua code window I would at least see the output of print statements so I could verify the script is working, but all I get is a popup saying the code executed successfully. The script also appears to be writing the output variables to a log file but where is this file? Understanding this development environment seems to be my biggest hurdle.

I take it you are using UI7, or possibly UI5. Don’t.

Switch immediately to AltUI and your Lua Test window will have a proper editor, print statements will work to an output frame there, and you have access to pretty-printing of table structures.

The log file is in /var/log/LuaUPnP.log, and AltUI has a way to view that too.

[quote=“akbooer, post:10, topic:191580”]I take it you are using UI7, or possibly UI5. Don’t.

Switch immediately to AltUI and your Lua Test window will have a proper editor, print statements will work to an output frame there, and you have access to pretty-printing of table structures.

The log file is in /var/log/LuaUPnP.log, and AltUI has a way to view that too.[/quote]

That sounds like the ticket! Thanks! Yes, I’m using UI7. I actually think I have this script working. I ended up just setting up a scene to run at 1 am everyday to run the Lua script. The script calculates the remote sunrise and sunset times and then call_timer statements to activate two other scenes to turn on and turn off the lights. Right now those are just activating a virtual switch for testing but it seems to be working.

Anyone know how to set the “Last Run” parameter on a scene. That would add a finishing touch and provide an easy visual to check whether the on/off times have been set.

AFAIK, it’s set by the system, rather sensibly, whenever the scene is run. Are you saying that you’re not seeing this behaviour?

No, it remains blank. I’m assuming it doesn’t set a “Next Run” time because I’m setting an absolute timer with the call_timer function. So there isn’t any repeat schedule for setting a next run time. Sounds like there isn’t a way to manually set it. Not a big deal.

Well, I thought I had this project licked but have run into a strange problem. My lua scene has turned on my virtual switch reliably at the appropriate time for several days now but the scene to turn the switch off at remote sunset never triggers. Here is the code that is executing after the sunrise and sunset times are calculated:

[code]-- Format Date time for sunrise and sunset for callback timer

today = os.date (“%Y-%m-%d”)

t_on = today … " " … sunrise … “:00”
t_off = today … " " … sunset … “:00”

– Set variables on virtual switch for monitoring whether variables are set correctly

luup.variable_set(“LaSelva”,“sunrise”,t_on,72)
luup.variable_set(“LaSelva”,“sunset”,t_off,72)

– Run on and off scenes at sunrise and sunset times

function LightsOn()
luup.call_action(“urn:micasaverde-com:serviceId:HomeAutomationGateway1”, “RunScene”, {SceneNum = “17”}, 0)
end

function LightsOff()
luup.call_action(“urn:micasaverde-com:serviceId:HomeAutomationGateway1”, “RunScene”, {SceneNum = “20”}, 0)
end

luup.call_timer(“LightsOn”, 4, t_on, “”, “”)
luup.call_timer(“LightsOff”, 4, t_off, “”, “”)

luup.log('Sunrise: '…sunrise)
luup.log('Sunset : '…sunset)[/code]
I have verified that the ‘t_on’ and ‘t_off’ variables are correct. I’ve also tested the timer calls and functions in the Test Lua code window and in another scene when I set the on or off times just a few minutes after triggering. The only thing I can think of is that the off timer is getting canceled or lost during the day. For now I’ve set the calculation lua to run 4 times a day to reduce the time between the call_timer being set and the scene action executed. No idea yet if it will work but does anyone have any ideas about what is going on? Does some of this need to be run in Startup Lua?

If Vera reloads some time during the day (highly likely), then the off timer will be lost. Check for this having happened in the logs. You can code around this failing.

Thanks again. It looks like my log file rotated at 6:58 this morning. Are old logs retained somewhere or do I just need to wait to gather more data? I poked around in the /var/log directory but didn’t see anything other than current files.

Also, would you happen to know what log level I’m looking for to detect a reload? 03 - LV_STARTSTOP perhaps?

At least it sounds like my kludge of rerunning the Lua script a few times a day to refresh the timers may be on track. But it will be nice to do this as efficiently as possible. Ideally though, it seems like active timers should persist through a system reload similar to the way a cron job behaves. This seems like a design failure.

Alas, it’s been ages since I knew the answer to this. I have moved to a system which does not crash (openLuup.)

Also, would you happen to know what log level I'm looking for to detect a reload? 03 - LV_STARTSTOP perhaps?

AFAIK, a reload starts a new logfile anyway? For my Vera systems, I used to send myself a notification every time they reloaded.

At least it sounds like my kludge of rerunning the Lua script a few times a day to refresh the timers may be on track. But it will be nice to do this as efficiently as possible.

If you do it in the startup Lua, then you guarantee that it’s run on every reload.

Ideally though, it seems like active timers should persist through a system reload similar to the way a cron job behaves. This seems like a design failure.

Yes, it is. That’s why people move to using PLEG which does all the hard work for you (but you need to learn a complete new syntax - take a look at the PLEG thread to understand some of the challenges people find.)

Ah! The little light bulb finally went off in my head. I got stuck on the idea of passing variables and functions between the scene Lua and startup. But no, I can just drop the whole Lua into startup and eliminate an unnecessary scene! Duh!

Yes, it is. That's why people move to using PLEG which does all the hard work for you (but you need to learn a complete new syntax - take a look at the PLEG thread to understand some of the challenges people find.)

Yeah, I fiddled around with PLEG a bit and decided that Lua looked closer to something I already knew than the PLEG syntax. But eventually I’m guessing I will get there. I may wind up migrating to running your openLuup on a Pi eventually. For now, I think I have the functionality I need with the Edge and MiOS even if the spotty documentation makes the learning curve a bit steep.