Page 1 of 1

When sun goes up and down, take control of your lights

Posted: Tue Aug 28, 2007 10:46 am
by krambriw
Hi,

Finally, I have migrated my C++ application (NexaSunRise) to a Python script that I would like to share with you. See attached eg .xml project.

Basically the script handles 12 labels (called A1-A3, B1-B3, C1-C3 and D1-D3) that you can map to any defined eg event.

For each label you have possibility for individual time settings for each day of the week.

All settings are described in the script where you have your dedicated area to change the values according to your personally needs.

The script activates the defined events when the sun goes up and down and when the defined time settings matches actual time.

I use eg with this script to control outdoor and indoor lights of my house.

The script also creates a simple html-log file of the actions and other stuff the script does. I have linked this to my web server, then its easy to see that the script is doing the work also from remote when not at home.

My special thanks goes to Bitmonster that has helped and answered my questions promptly. This forum is really great.

As you know there is always room for learning. You will most likely find some functions that can be written "smarter". But the script works fine and fast. And the Python compiler seems to be very efficient :D

Also a thanks to Henrik H├ñrk├Ânen. I found the nice Sun.py script at his site which provides the actual calculation of Sunrise/Sunset at the specified geographical position.

Hope you have some use of this script. I do not want to know how much time I used to spend, adjusting timers before I used it...

Best regards, Walter

Re: When sun goes up and down, take control of your lights

Posted: Sun Sep 02, 2007 8:28 am
by krambriw
Just a small adjustment to make the synchronization work better around the clock

Walter

Re: When sun goes up and down, take control of your lights

Posted: Fri Sep 14, 2007 5:00 pm
by krambriw
There is a little bug in the script related to daylight savings and time zones. I┬┤m working on an update....

In the meanwhile, use the following setting to read the status of daylight savings from the computer instead of calculating it (remember to activate that in Windows)

Code: Select all

# Select if status for daylight savings shall be calculated (EU-rules) or 
# taken from os
iDST_From_OS = 1

Re: When sun goes up and down, take control of your lights

Posted: Sat Sep 15, 2007 6:20 am
by krambriw
Alright, here is the updated and fixed script...

I had a bug in the part that calculates the daylight savings (for EU) that would have crashed the script in October when daylight savings ends. If you used the info from OS as mentioned in previous posting, it would work without problem.

Another thing I have improved is the handling of different time zones. I have not seen anybody using this script in Vladivostok or Hawaii but it should now be possible too.

Before you replace the script, take a copy of your current one with your personal settings!!!!

Best regards, Walter

Re: When sun goes up and down, take control of your lights

Posted: Sun Nov 04, 2007 5:57 pm
by krambriw
I found some more bugs :oops:

In the time offset calculation, in the daylight saving routines and in the routine that reads the actual status via OS.

The attached script has all the fixes included.

Remember to copy your personal settings before you paste this script over the other in EG.

Best regards,
Walter

Re: When sun goes up and down, take control of your lights

Posted: Sun Nov 04, 2007 7:05 pm
by Bitmonster
Well, you use fixed repeating structures very excessive in this code and this bloats the code very much. I would suggest to get this down to an array of dictionaries. This way it is much easier to administer the settings and you are not limited to twelve simultaneous settings. Something like this:

Code: Select all

setup = [
   {
        "thursdayEvening": 0200,
        "eventName": "A1_ON",
        "iOffset": -15,
    },
    {
        "sundayEvening: 2300,
        "eventName": "A1_OFF",
        "iOffset": -15,
    },
]


or even better to function calls, that put their data into an array, so the user can configure it like:

Code: Select all

RegisterDaylightEvent(
    eventName = "A1_ON",
    time = "sunrise", # possible values are "sunrise", "sunset" or a fixed time
    offset = -15,
    weekdays = "Monday, Tuesday, Thursday", # apply only to some weekdays
)

RegisterDaylightEvent(
    eventName = "A1_OFF",
    weekdays = "all", # "all" might be the default, so it could be omitted
    time = 2300,
)

Re: When sun goes up and down, take control of your lights

Posted: Mon Nov 05, 2007 7:13 am
by krambriw
I would be more than happy to improve...but I'm not that skilled...however willing to learn

The twelve limitation would be nice to get rid of, yes, even though its more then enough for a normal building. But if you extend it eg could control all lights in Berlin and we would save a lot of energy...

The user configuration part I think you should keep together in one chunk because editing function call parametres is very risky...or have I misunderstood you???

The best would be to have a gui that is used to edit the settings. And with support of drag & drop of eg events :D

Best regards, Walter

Re: When sun goes up and down, take control of your lights

Posted: Mon Nov 05, 2007 11:28 am
by Bitmonster
I like functions for configuring, because you can use default arguments to limit the count of parameters that need to be set and you can check the values for validity immediately (and not when the codes first uses them). And they are a big step forward a plugin.

Re: When sun goes up and down, take control of your lights

Posted: Mon Nov 05, 2007 7:26 pm
by krambriw
OK, I accept, can you show me an exampel taken from my script how you would preferre it?

Best regards, Walter

Re: When sun goes up and down, take control of your lights

Posted: Sat Jan 05, 2008 3:15 pm
by vuego
Thanks alot krambriw.
This is possibly the only feature I miss in EventGhost. I hope that it will be included in the main program sometime :)

Now to my issue. I don't really know how to install the script. I downloaded your Nov 03 version, added a Python Script Action in Autostart and copied/pasted everything from the .py file. Perhaps it should be done in another way?

When I execute the script (with F5 or restart EventGhost) I get the following errors logged:
15:43:29 Traceback (most recent call last):
15:43:29 Python script "0", line 3535, in <module>
15:43:29 TheTask()
15:43:29 Python script "0", line 3515, in TheTask
15:43:29 SunUpDown()
15:43:29 Python script "0", line 1550, in SunUpDown
15:43:29 st = str(eg.globals.sunRiseSet(year, month, day, myLongitude, myLatitude))
15:43:29 AttributeError: Bunch instance has no attribute 'sunRiseSet'
I'm using EventGhost v0.3.6.1262.

Thanks

Re: When sun goes up and down, take control of your lights

Posted: Mon Jan 07, 2008 7:35 am
by krambriw
Dear Vuego,
The best is to start looking at the sample xml I attached in the first posting. The one you have used is only the latest update of the main script.

The sample uses 3 scripts in total;
- the one you have already
- a basic sunset/sunrise calculation script (missing this is why you get the errors)
- a script to ask the main script to shut down itself

In the sample you see that to get automatic start of the script you need to move it to the autostart (move it to the end of the autostart list) as in the picture sample below

Best regards, Walter

Re: When sun goes up and down, take control of your lights

Posted: Thu Jan 10, 2008 5:07 pm
by vuego
The sample works much better. Thanks alot :)

Re: When sun goes up and down, take control of your lights

Posted: Thu Jan 10, 2008 8:19 pm
by krambriw
Good to hear!

And now when you got the original sample to work, its easy to update to the latest script (just download, copy and paste it using the EG editor)

BestR

Re: When sun goes up and down, take control of your lights

Posted: Sun Feb 17, 2008 3:53 pm
by halle
hallå
är ny på det här, inte så lätt att hänga med på allt

jag b├Ârjade med att l├ñgga in en ny action/python script, sen klistrade jeg in allt fr├Ñn SunRiseSet_sample.xml och d├Âpte den till Sun.py Script, sen gjorde jag ett nytt script med alla koder fr├Ñn 03.11.2007_SunRiseSet_with_scheduler.py. N├ñr jag k├Âr execute item s├Ñ f├Ñr jag upp domma felkoderna. Vad har jag gjort f├Âr fel?????? N├Ñgon som kan hj├ñlpa mej????

Re: When sun goes up and down, take control of your lights

Posted: Sat Feb 23, 2008 6:58 pm
by krambriw
I think you are not far away...

You have an indent problem in line 1 as indicated in the log.

Python is very sensitive with the format....