Page 1 of 8

[userModule] Daylight (updated 2015-07-05)

Posted: 31 Dec 2014 13:23
by mhorst
Last updated: 2015-07-05
Tested with: v2.0.1

Another sunrise/sunset module. This one creates a binarySensor that indicates whether it is daytime (on) or nighttime (off), which is useful in combination with logical rules.

This module calculates the sunset and sunrise times based on the longtitude and latitude that you configure and compares that with the current system time to determine whether the sun is in the sky or not (so no internet connection needed). It also allows to configure what counts as sun up or down: official twilight, civil twilight, nautical twilight or astronomical twilight (see wikipedia for definitions).

Using this sensor you can make a logical rule to trigger actions when the sun sets or rises, i.e. when the value of this sensor changes.
Personally I will use this to close my window shutters when it becomes dark outside.

Also, you can make scenes that behave differently during daytime or night time. Just make three scenes: one empty one, one for daytime and one for night time. Then make two logical rules: one that triggers the daytime scene when the empty scene is triggered and the Daylight sensor is on, and one that triggers the night time scene when the empty scene is triggered when the Daylight sensor is off.
Hide these daytime and nighttime scenes and only put the empty scene on the dashboard. Now when you trigger the "empty" scene you will get different behavior based on whether it is day or night time.
Personally I will use this to change the behavior of my "Arrive home" scene; if I arrive home during the day the window shutters open, if I arrive home after dark then the lights come on and the window shutters stay closed.

Why this module if there exist already the SunriseSunset and ScheduleSun modules? Well, I just wanted to see if I could write a module myself. Also I thought it would be useful to be able to tell whether it is daytime or nighttime in logical rules.

Version history:
1.0.0 - Updated for version 2.0.1 by Pieter (pz1), he added an Icon, support for internationalization, and the sunrise and sunset times to the virtual device.
0.7.0 - Fixed bug that causes sunrise/sunset times to change between schedule and actual event. Update schedule also during sunrise/sunset events.
0.6.0 - Calculate new sunrise/sunset times at 03:01 to ensure it is done after any daylight savings time change
0.5.0 - Added more logging. Fixed bug when sunrise/sunset falls on different dates in UTC and the local timezone
0.4.0 - Fixed potential bug when the sun rises or sets 1 minute before a whole hour
0.3.0 - Added Cron dependency and ensured sensor is only updated when a real change occurs.
0.2.0 - Fixed bug in algorithm that caused sunrise to be one day too late on some dates
0.1.0 - First version

Re: [userModule] Daylight

Posted: 31 Dec 2014 13:46
by pz1
Great idea! I'll give it a try later. Something like this would imho definitely qualify as a standard module :) , but the proof of the pudding is in the eating :mrgreen:

Re: [userModule] Daylight

Posted: 01 Jan 2015 12:08
by pz1
I did install the module half an hour ago. It is 10:00h now, and it still shows as off For my location sunrise is 8:51 today. File /etc/timezone says Europe/Amsterdam.
Restart of Z-way did not cure the problem.

Re: [userModule] Daylight (updated 2015-01-01)

Posted: 01 Jan 2015 13:19
by mhorst
You are right, I had the same problem on my system.
I found a typo in the algorithm that caused the sunrise to occur only the next day in some cases, i.e. on the 2nd of januari instead of the 1st. That date is in the future so the module assumed the sun had not risen yet.

I fixed the typo in the new version (0.2.0). That now works for me: after a restart the sensor is on.
Please try if the new version also works for you.

Re: [userModule] Daylight (updated 2015-01-01)

Posted: 01 Jan 2015 13:31
by pz1
I'll give it a try later today. (I was about to add some log data to my earlier response, but that seems irrelevant now)

Re: [userModule] Daylight (updated 2015-01-01)

Posted: 01 Jan 2015 13:32
by mhorst
Ok, thanks.

Re: [userModule] Daylight (updated 2015-01-01)

Posted: 01 Jan 2015 14:06
by pz1
It is OK now (I could squeeze this in some other testing requiring a restart.)
This module of yours seems useful for me when I transfer the logic of the Poor man's Sunshade Control from OpenRemote Drools to the RaZberry Logical Rules

Re: [userModule] Daylight (updated 2015-01-01)

Posted: 10 Jan 2015 12:09
by pz1
There seems to be a flaw in this module: After removal of the module the corresponding object (in my case Daylight64) is NOT removed from the configuration.

Re: [userModule] Daylight (updated 2015-01-01)

Posted: 11 Jan 2015 16:16
by mhorst
That's strange, I can't reproduce that error in my setup.
did have to refresh the webpage (by hitting F5) before the UI showed that the Daylight widget was gone, but other than that I could remove it without any problems.

In my case the log simply shows:

Code: Select all

[2015-01-11 14:09:35.728] [I] [core] Deleting device sensorBinary Daylight12
[2015-01-11 14:09:35.731] [I] [core] --- Stopping module Daylight
Are there any error messages in your log?

Re: [userModule] Daylight (updated 2015-01-01)

Posted: 11 Jan 2015 18:10
by pz1
mhorst wrote:Are there any error messages in your log?
Sorry I did remove it some time ago, so the logs have been overwritten in the meantime.
I had a few others (see viewtopic.php?f=3422&t=21052&p=54227#p54227), which I found later, that had not been cleaned up. So the cause might quite well have been elsewhere