[userModule] SunriseSunset - No Longer Maintained

Tips, Tricks and Scripts to enhance your home automation and workaround known device bugs, limitations and incompatibilities
islipfd19
Posts: 126
Joined: 07 Jul 2014 03:35

[userModule] SunriseSunset - No Longer Maintained

Post by islipfd19 »

Where to begin? Since getting my razberry daughter board and installing this wonderful piece of software, I embarked on a mission. I noticed (in my opinion) that this lacked a few things and I thought I'd contribute to the project as much as I can. The way I can is by coming up with modules that not only I can use but others can use as well. With a little help and direction from Kambis. I was able to come up with this module, this isn't the first module I started to work on (that one is still a work in progress and I hope to complete it soon and make it available to everyone). While I was beating my head over the first module I figured I'd start to work on my second and here is why and what I did.

My Razberry acts as a secondary controller to my alarm system (as it supports z-wave communication). My alarm company provides me with some basic methods to setting up rules on their website. Which is fine for most people but for me I'd like to have the flexibility to add to things as I see fit. So I was basically limited to what I could do with my alarm company. What they did have is the ability to turn lights on and off based on sunrise and sunset times. Sure, one can setup a logical module to turn lights on and off close to sunrise and sunset but it would have to be adjusted periodically throughout the year and since this is supposed to be an automated system why would one have to do that. I looked at how I could get the sunrise and sunset information and the method on how to do that. Well there is the OpenWeather module. This provides me the means to get the information from the web. And by good fortune, the site where we get the weather from also provides the sunrise and sunset times in Epoch format. This couldn't be more perfect that what I had hoped for. After trial and error and digging through some other modules I was finally able to get the lights to turn on and off at set times of the day based on sunrise and sunset.

If you are running version 1.7.1 or earlier, you will need to extract the contents of the zip package to your /opt/z-way-server/automation/modules directory in a folder named SunriseSunet. The full path should be '/opt/z-way-server/automation/modules/SunriseSunset'. If you are running version 1.7.2, the SunriseSunset folder should be placed in the userModules directory. After placing it in the appropriate directory you will need to restart z-way-server. One of the methods to do that is by executing this command `sudo service z-way-server restart`.

In the Automation menu, you should now see the new module 'Sunrise / Sunset'. Fill in your City and Country and add the switches you'd like to control during these times of the day. I have two switches and a dimmer that are activated. I also provided two drop down selections that contain 10, 20, 30, 45 and 60 minute intervals. The first drop down will turn off your selected devices X number of minutes past sunrise and the second will turn your devices on X number of minutes prior to sunset.

The module can also be used more than once, I'm sure the creative people may enjoy it. I was think on the lines of possibly using this with a plug-in wall device to turn Christmas lights on and off during the Holiday season.

Enjoy! :D

********** Updated module Sept 8th, 2014 ***********
Version 2.0.5 - Added the ability to disable the turning off/on of the device
for either the sunset or sunrise time. As per request from ozgreg.
Removed console.log references used for debugging.

No known bugs or issues with it.
Last edited by islipfd19 on 17 Sep 2015 08:45, edited 7 times in total.
pofs
Posts: 688
Joined: 25 Mar 2011 19:03

Re: New Module SunriseSunset

Post by pofs »

Great job!

I have the only concern about timer interval used: 3600*84.
It is a very strange to fire timer every 302.4 seconds, you know :)

If you meant firing once a day, it should be 1000*3600*24 (interval is in milliseconds).
But executing it just once a day will also fire fetching data just once, and you probably will have no chance to check the conditions correctly.
On the other hand, firing timer every ~5 minutes makes the checks work, but I doubt there's much sense in requesting data from openweathermap.org so often.

The better approach is to use built-in Cron module.
You may specify two separate schedules: fetch weather data once a day at midnight, and perform checks of current time, let's say, once in 5 minutes.
islipfd19
Posts: 126
Joined: 07 Jul 2014 03:35

Re: New Module SunriseSunset

Post by islipfd19 »

I fire it every five minutes to get the current Epoch time, it also gets the current days sunrise and sunset time. This of course would only change once a day.I do it every 5 minutes because the lowest selectable value is 10 minutes. This guarantees that this will fire during that interval to either turn on or off the selected devices.

I do agree, using cron would be a better approach. I haven't tried implementing it yet, perhaps in the next version.

Your suggestion for two separate schedules is a good suggestion, but what if z-way were to be restarted? then the first schedule wouldn't fire until the next day and the second schedule wouldn't fire. But as I type this module could check if the sunrise and sunset data is blank and if so, then fire and then update after midnight.

I'll definitely try implementing this approach.

I'm currently struggling on trying to create a separate module that just updates the current state of the selected switch but I am having trouble finding the correct format to get the state of the switch. You know, the actual command that is triggered in the Expert UI under the switches menu. The reason for the module is that z-way doesn't know about outside switch events; i.e. someone physically turning on or off the switch or the device being turned on or off by a different control method other than z-way. If you have any experience or guidance in this type of command and would like to contribute the method I'd be grateful.

I also plan on adding a months selection to the LogicalRules module. My plan with that is to schedule to turn my thermostat to heat mode during specific times of the year (winter of course) but only if the temperature outside has dropped below a certain degree. And then to turn the thermostat off in the Spring. I don't have central air but I would also include a way to turn the cold mode on as well. This module may be a little tricky but sounds like it will be a good learning experience.
pofs
Posts: 688
Joined: 25 Mar 2011 19:03

Re: New Module SunriseSunset

Post by pofs »

islipfd19 wrote:I fire it every five minutes to get the current Epoch time, it also gets the current days sunrise and sunset time. This of course would only change once a day.I do it every 5 minutes because the lowest selectable value is 10 minutes. This guarantees that this will fire during that interval to either turn on or off the selected devices.
Okay, but why not 5*60*1000 == 300000 then? Why 302400? :)
islipfd19 wrote:what if z-way were to be restarted? then the first schedule wouldn't fire until the next day and the second schedule wouldn't fire.
Just request it once in the init() handler before setting the schedules. You'll also know if city settings are incorrect.
islipfd19 wrote:The reason for the module is that z-way doesn't know about outside switch events; i.e. someone physically turning on or off the switch or the device being turned on or off by a different control method other than z-way.
Well, if the switch supports associations, it should report such changes to controller. I believe there's only a few devices left on the marker which doesn't report their state.

But it's true that some switches have bugs. For example, Philio PAN06 doesn't report which channel has changed its state. Other devices might have theirs own issues.
I suggest not to target the whole device class (for example, binary switches), but targeting specific flawed devices instead. It could save a lot of network overhead.
islipfd19
Posts: 126
Joined: 07 Jul 2014 03:35

Re: New Module SunriseSunset

Post by islipfd19 »

Okay, but why not 5*60*1000 == 300000 then? Why 302400? :)
At the time, I just needed a delay. There's no specific reason why it is 302400.
Just request it once in the init() handler before setting the schedules. You'll also know if city settings are incorrect.
That's what I was thinking.

I'm using Linear switches and the models I'm using don't allow for associations. I wasn't aware of it when I bought them. The SwitchPolling module I'm working on would be directed only to those switches. Any others that are polled or updated more frequently wouldn't need to be polled.
n0ahg
Posts: 87
Joined: 08 May 2013 23:41

Re: New Module SunriseSunset

Post by n0ahg »

I offered to do this on the GitHub site using OpenWeatherMap and was told they didn't want it done that way...

My alternative was to use Suncalc!
islipfd19
Posts: 126
Joined: 07 Jul 2014 03:35

Re: New Module SunriseSunset

Post by islipfd19 »

I can't say what their reasons were, but I am glad we are able to create and use our own modules. You can use this one if you choose, be my guest. It's why I posted it. I hope to be uploading a newer version shortly though.
pz1
Posts: 2053
Joined: 08 Apr 2012 13:44

Re: New Module SunriseSunset

Post by pz1 »

islipfd19 wrote:I can't say what their reasons were.
This is strange indeed, because quite a while ago, they made some explicit request to users to submit to the github. As they moved to commenting commenting in the russian laguage in the github, I guess they are now considering it more as their internal thing. It is a pity, because it makes it harder to get the latest version of user contributions.
It seems best to use the Recepies section for this, just like you and some others are doing. It would be nice if contributors would reserve and update the very first post of a thread for the latest release of their contributions. Not as good as github, but better thang long or multiple threads to work through.
Thanks for your contributions
Since 29-12-2016 I am no longer a moderator for this forum
n0ahg
Posts: 87
Joined: 08 May 2013 23:41

Re: New Module SunriseSunset

Post by n0ahg »

Agreed, something weird going on. I even commented on a change made to my DummyDevice 21 days ago and no response. I was happy with the openness but now they seem to have swung the other way.

Thanks for the contributions.
islipfd19
Posts: 126
Joined: 07 Jul 2014 03:35

Re: New Module SunriseSunset - ***Updated Module***

Post by islipfd19 »

Updated module!!! - Version 2.0.2

I added the recommended features by Pofs. It contain two cron tasks. One polls for the current Epoch time and is configurable and the second polls how many times a day it should check for updated Sunrise and Sunset times also configurable. I do recommend that the Epoch polling number is NOT greater than the amount of time you wish to set the devices on or off at the respective time of day, otherwise you risk the chance that the devices will not either turn off or on. For example. The Epoch check also checks whether it is within the valid range of sunrise or sunset and you configured minutes. If you poll Epoch time every 20 minutes and set you devices to turn on or off within 10 minutes of sunrise or sunset, there's a very strong possibility that those devices will not turn on or off because the Epoch check will occur before and after that desired time.

I also fixed the removal of the cron task when the module is removed from automation.

Known Bugs/Issues:
1. I've also noticed that cron is buggy, either my code or the cron module itself. I wasn't able to configure the module to fire at an exact point during the day. So I've set my Sunrise and Sunset times to be updated every hour so that it does happen. I'm still debugging this.

2. I've also noticed that the module, on occasion has a problem retrieving the sunrise and sunset time even though it was able to successfully retrieve the data from the website. I'm still debugging this.
Post Reply