Sin/Cos in rules

1,486 views
Skip to first unread message

rak

unread,
Apr 24, 2012, 3:12:42 AM4/24/12
to ope...@googlegroups.com
Hi guys,

I am working on a ruleset to calculate the height of the sun above the horizon. This can be used to trigger things which should happen at sunset or sunrise.

For this I need Cosinus and Sinus as a function to be used in rules. Is this possible? What do I need to import?

I tried to using

import java.lang.Math


and tried to reference Math.cos without success.

I am thinking about a rule like this:

rule "Calculate Sun Height"

when

Time cron "0 0/5 * * * ?"

then

var current_time = Date.state as DateTimeType


var Number tageszahl

var Number deklin

var Number zeitdiff

var Number x 

var Number K

var Number height


// QUELLE: http://www.jgiesen.de/SME/tk/index.htm


K = 0.01745;

if (current_time.calendar != null) {

//early_enough = (current_hour < 18) && (current_hour >7)

}

tageszahl = (current_time.calendar.get(java::util::Calendar::MONTH) - 1) + current_time.calendar.get(java::util::Calendar::DAY_OF_MONTH) + 0.5

deklin   = -23.45 * Math.cos( K * 360 * (tageszahl + 10) / 365)

zeitdiff =  current_time.calendar.get(java::util::Calendar::HOUR) + current_time.calendar.get(java::util::Calendar::MINUTE) / 60 - ( 15.0 - longitude)/15.0 - 12

x        = sin(K * latitude) * sin(K * deklin) + cos(K * latitude) * cos(K * deklin) * cos(K * 15.0 * zeitdiff)

height   = x / K + 0.25 * x*x*x / K 

sendCommand(Sun, OFF)

end


Any help appretiated.

Kind regards.
Ralf Klüber

Thomas Eichstädt-Engelen

unread,
Apr 24, 2012, 3:34:30 AM4/24/12
to ope...@googlegroups.com
Hi Ralf,

please replace "." by "::" whithin your when clause,  e.g. Math::cos(number).

Cheers,

Thomas E.-E.


--
You received this message because you are subscribed to the Google Groups "openhab" group.
To view this discussion on the web visit https://groups.google.com/d/msg/openhab/-/y6gICkmQneQJ.
To post to this group, send email to ope...@googlegroups.com.
To unsubscribe from this group, send email to openhab+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/openhab?hl=en.

Ralf Klüber

unread,
Apr 24, 2012, 6:16:44 AM4/24/12
to ope...@googlegroups.com, Thomas Eichstädt-Engelen
Hi Thomas,

silly me. Thanks. Helped a lot. I think I made it. Please find below a rule based calculation of dawn events. See http://en.wikipedia.org/wiki/Dawn for the defined 4 types of dawn.

The height of the sun above the horizon is calculated based on a cron triggered rule. I decided every 5 min is enough. Change it if you feel the need.

I use the following as sun.items
------ schnipp ------
Number Sun_Height "Sonnenhöhe [%.1f °]" <none> 

Switch Sun_Dawn_Solar "" <none> // height < 0
Switch Sun_Dawn_Civil "" <none> // height < 6
Switch Sun_Dawn_Nautical "" <none> // height < 12
Switch Sun_Dawn_Astronomical "" <none> // height < 18

------ schnipp ------

I use the following as sun.rules
------ schnipp ------
import org.openhab.core.library.types.*
import org.openhab.core.types.State
import org.openhab.core.library.types.DateTimeType
import java.lang.Math

// tageszahl = (monat-1)*30 + datum + 0.5
// deklin = -23.45*cos(K*360*(tageszahl+10)/365)
// zeitdiff = stunde + minute/60 - (15.0-long)/15.0 - 12
// x = sin(K*lat)*sin(K*deklin) + cos(K*lat)*cos(K*deklin)*cos(K*15*zeitdiff)
// Ergebnis für den Höhenwinkel der Sonne in Grad:
// hoehe = x/K + 0.25*x*x*x/K (Die ARCSIN-Funktion wird durch ein Polynom approximiert)

// Change this reflecting your location
var Number longitude = 51.12345
var Number latitude = 6.7890

rule "Set Sun Height and Dawn States"
when
Time cron "0 0/5 * * * ?"
then

var Number tageszahl
var Number deklin
var Number zeitdiff
var Number x 
var Number K
var Number height


K = 0.01745


var current_time = Date.state as DateTimeType
if (current_time.calendar != null) {
var current_hour = current_time.calendar.get(java::util::Calendar::HOUR_OF_DAY)
var month        = current_time.calendar.get(java::util::Calendar::MONTH) + 1
var day          = current_time.calendar.get(java::util::Calendar::DAY_OF_MONTH)
var hour         = current_time.calendar.get(java::util::Calendar::HOUR)
var minute       = current_time.calendar.get(java::util::Calendar::MINUTE)


tageszahl =  (month - 1) * 30 + day + current_hour / 24 
deklin    = -23.45 * Math::cos( (K * 360 * (tageszahl + 10) / 365).doubleValue() )
zeitdiff  =  hour +  minute / 60 - ( 15.0 - longitude)/15.0 - 12
x         = Math::sin( (K * latitude).doubleValue() ) * Math::sin( (K * deklin).doubleValue() )
x         = x + Math::cos( (K * latitude).doubleValue() ) * Math::cos( (K * deklin).doubleValue()) * Math::cos( (K * 15.0 * zeitdiff).doubleValue() )
height    = x / K + 0.25 * x*x*x / K 
}


sendCommand(Sun_Height, height)


sendCommand(Sun_Dawn_Solar,        if( height < 0  ) {ON} else {OFF})
sendCommand(Sun_Dawn_Civil,        if( height < 6  ) {ON} else {OFF})
sendCommand(Sun_Dawn_Nautical,     if( height < 12 ) {ON} else {OFF})
sendCommand(Sun_Dawn_Astronomical, if( height < 18 ) {ON} else {OFF})


end
------ schnipp ------

A rule to shut down the blinds at dawn should then look like
------ schnipp ------
rule "Rolladen Garten Abends runter 1"
when
Item Sun_Dawn_Nautical changed from OFF to ON
then
sendCommand(Shutter_GF_Wohnen, DOWN)
end
------ schnipp ------

I need to test how this works. If you see the need for change/improvement, please let me know.

Kind regards.
Ralf Klüber


Am 24.04.2012 um 09:34 schrieb Thomas Eichstädt-Engelen:

Hi Ralf,

please replace "." by "::" whithin your when clause,  e.g. Math::cos(number).

Cheers,

Thomas E.-E.

Kai Kreuzer

unread,
Apr 24, 2012, 7:20:33 AM4/24/12
to ope...@googlegroups.com
Hi Ralf,

Cool, you are writing already writing pretty complex rules :-)

> I need to test how this works. If you see the need for
> change/improvement, please let me know.

Just one very minor suggestions:
At the end of your "Set Sun Height and Dawn States" rule, you could
use "postUpdate()" instead of "sendCommand()" as you are not really
telling the sun to go down, but you rather only observe its state (or
are you some kind of god, then apologies ;-)).
As we have the "autoupdate" feature, your current code works as well,
as after receiving the command, the item will automatically send an
update event.

Regards,
Kai

> Am 24.04.2012 um 09:34 schrieb Thomas Eichstädt-Engelen:
>
>> Hi Ralf,
>>
>> please replace "." by "::" whithin your when clause, e.g.
>> Math::cos(number).
>>
>> Cheers,
>>
>> Thomas E.-E.
>
> --
> r...@lf-klueber.de
>
>
>
>

> --
> You received this message because you are subscribed to the Google
> Groups "openhab" group.

Ralf Klüber

unread,
Apr 24, 2012, 7:32:50 AM4/24/12
to ope...@googlegroups.com
Hi Kai,

Good point. To be honest I did not fully understand the difference between
  • sendCommand(String itemName, String commandString): Sends the given command for the specified item to the event bus
  • postUpdate(String itemName, String stateString): Posts the given status update for the specified item to the event bus
Can you explain this to me in a bit more detail. An example with a knx binding would be good.

Is it the case that sendCommand would also update the KNX binding on the KNX bus and postUpdate would not?

Kind regards.
Ralf

Am 24.04.2012 um 13:20 schrieb Kai Kreuzer:

Just one very minor suggestions:
At the end of your "Set Sun Height and Dawn States" rule, you could use "postUpdate()" instead of "sendCommand()" as you are not really telling the sun to go down, but you rather only observe its state (or are you some kind of god, then apologies ;-)).
As we have the "autoupdate" feature, your current code works as well, as after receiving the command, the item will automatically send an update event.

Kai Kreuzer

unread,
Apr 24, 2012, 1:18:00 PM4/24/12
to ope...@googlegroups.com
Hi Ralf,

Well, openHAB simply differentiates between commands and states. If
you send a command, you simply say that some device should do
something (light should be turned on). The device can then answer with
a status update (light is on) - if the device is blocked, it could
also answer with "off", although you have sent an "on" command.

KNX does not keep these two concepts apart, everything is a GA. But
usually, you use some GAs to switch something (commands) and others to
return the current state (status GA).

> Is it the case that sendCommand would also update the KNX binding on
> the KNX bus and postUpdate would not?

No, both will be sent out to the KNX bus, so from this perspective,
there is no difference.

Hope it is clearer now.

Regards,
Kai

rak

unread,
May 30, 2012, 2:23:39 AM5/30/12
to ope...@googlegroups.com, Thomas Eichstädt-Engelen
Hi guys,

I am working with the Sun_Height to calculate an item if it is assumed to be dark or not (I dont have a weather station).

This item is used for various different things in my OH configuration. If this fails I have a significant decrease in the WAF.

The sun.rules I defined works pretty well with one exception. It is executed several times until the calculation stops without any error messages. The rule seems not to be triggert anymore.

Could this be due to a memory leakage or something similar? I have no clue how to debug this. After a restart it works fine for some hours/days and than it stops.

Any thoughts about this?

Kind regards.
Ralf

Kai Kreuzer

unread,
May 30, 2012, 7:21:11 AM5/30/12
to ope...@googlegroups.com
Hi Ralf,

Could you try to get any more logging out of it by adding the line

<logger name="org.openhab.model.rule" level="DEBUG"/>

to your logs/logback.xml file?
I don't have any other suggestions, my rules usually run without failure. Could you maybe also send your sun.rules file?

Regards,
Kai


--
You received this message because you are subscribed to the Google Groups "openhab" group.
To view this discussion on the web visit https://groups.google.com/d/msg/openhab/-/UrhuRlfSNBIJ.

Ralf Klüber

unread,
Jun 1, 2012, 5:28:20 PM6/1/12
to ope...@googlegroups.com
Hi Kai,

additional logging brought no extra insight. The rule is not triggered after a certain amount of time (unpredictable). After restarting OH it got even worse. The whole OH framework collapsed.

------ schnipp ------
19:42:00.002 DEBUG o.o.m.r.i.e.ExecuteRuleJob[:73] - Executing scheduled rule 'Set Sun Height and Dawn States'
19:42:08.422 DEBUG o.o.m.r.i.engine.RuleEngine[:310] - Executing rule 'Stromz?hler aktuallisieren'
19:42:11.665 ERROR o.o.b.v.internal.VDRConnection[:86] - Could not connect to VDR on 192.168.0.207:2001: java.net.SocketTimeoutException: connect timed out
java(11395,0x114a61000) malloc: *** error for object 0x7fd259755200: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
./start.sh: line 10: 11395 Abort trap: 6           java -Declipse.ignoreApp=true -Dosgi.noShutdown=true -Djetty.port=8080 -Djetty.port.ssl=8443 -Djetty.home=. -Dlogback.configurationFile=logs/logback.xml -Dfelix.fileinstall.dir=addons -Djava.library.path=lib -Djava.security.auth.login.config=./etc/login.conf -Dorg.quartz.properties=./etc/quartz.properties -Djava.awt.headless=true -jar $cp $* -console
noname:runtime tv$ 
------ schnipp ------

I have turned off logging (set everything to ERROR level) and now the system runs stable for more than 24h triggering the supected rule like a charm. 

I have no clue whats going on. But keepong the loging at minimum seems to help.

Kind regards.
Ralf Klüber

Kai Kreuzer

unread,
Jun 2, 2012, 11:33:29 AM6/2/12
to ope...@googlegroups.com
Hi Ralf,

You seem to have a hell of a lot of rules… Have you tried to disable some others to see if this makes any difference? Maybe the system is a bit overloaded, if reducing the logging has helped…? Not that I would think that logging could get your system down, but as it seems to help...

The JVM crash below looks frightening - seems you came across some MacOS JVM bug there.

Regards,
Kai

Ralf Klüber

unread,
Jun 2, 2012, 5:52:28 PM6/2/12
to ope...@googlegroups.com
Hi Kai,

A hell of a lot is relative.

$ cd openHAB/214/configurations/rules
$ grep rule *.rules | wc -l
      85
I will have a look and try to find out more.

Is this " a lot"? What about your others configurations?

Am 02.06.2012 um 17:33 schrieb Kai Kreuzer:

You seem to have a hell of a lot of rules… 

Kai Kreuzer

unread,
Jun 3, 2012, 12:37:46 PM6/3/12
to ope...@googlegroups.com
Is this " a lot"? What about your others configurations?

I would hope that openHAB can deal with it. I just saw in your log file that there were many rules triggered at the same time. As each rule is executed in a separate thread, I wondered if this might have had caused some issues, but that was just vague idea without having tested possible maximal loads yet myself.

Is it now still working with the reduced logging or did the problem reoccur nonetheless?

Regards,
Kai



Thomas Eichstädt-Engelen

unread,
Jul 3, 2012, 8:27:08 AM7/3/12
to ope...@googlegroups.com
Hi,

i refined the sun calculation rule a bit and added the Azimut-calculation as well.

Regards,

Thomas E.-E.



#####################################################################

import org.openhab.core.library.types.*
import java.lang.Math


// Constants
var Number K = 0.017453

// Change this reflecting your destination
var Number latitude = xx.xxxxxx
var Number longitude = yy.yyyyyy


rule "Set Sun sonnenhoehe and Dawn States"
when
Time cron "0 0/5 * * * ?"
then
var Number tageszahl
var Number deklination
var Number zeitgleichung
var Number stundenwinkel
var Number x 
var Number y
var Number sonnenhoehe
var Number azimut


var month  = now.getMonthOfYear
var day    = now.getDayOfMonth
var hour   = now.getHourOfDay
var minute = now.getMinuteOfHour


tageszahl = (month - 1) * 30 + day + hour / 24 
deklination = -23.45 * Math::cos((K * 360 * (tageszahl + 10) / 365).doubleValue)
zeitgleichung = 60.0 * (-0.171 * Math::sin((0.0337*tageszahl+0.465).doubleValue) - 0.1299 * Math::sin((0.01787*tageszahl-0.168).doubleValue))
stundenwinkel = 15.0 * (hour.doubleValue + (minute.doubleValue/60.0) - (15.0-longitude)/15.0 - 12.0 + zeitgleichung/60.0)
x = Math::sin((K * latitude).doubleValue()) * Math::sin((K * deklination).doubleValue()) + Math::cos((K * latitude).doubleValue()) * Math::cos((K * deklination).doubleValue()) * Math::cos((K * stundenwinkel).doubleValue())
y = - (Math::sin((K*latitude).doubleValue) * x - Math::sin((K*deklination).doubleValue)) / (Math::cos((K*latitude).doubleValue) * Math::sin(Math::acos(x.doubleValue)))
sonnenhoehe = Math::asin(x.doubleValue) / K


var break = hour.doubleValue + (minute.doubleValue/60.0) <= 12.0 + (15.0-longitude)/15.0 - zeitgleichung/60.0 
if (break) {
azimut = Math::acos(y.doubleValue) / K
} else {
azimut = 360.0 - Math::acos(y.doubleValue) / K
}

 

logDebug("Sun.rules", "month: " + month)
logDebug("Sun.rules", "day: " + day)
logDebug("Sun.rules", "hour: " + hour)
logDebug("Sun.rules", "minute: " + minute)
logDebug("Sun.rules", "tageszahl: " + tageszahl)
logDebug("Sun.rules", "deklination: " + deklination)
logDebug("Sun.rules", "zeitgleichung: " + zeitgleichung)
logDebug("Sun.rules", "stundenwinkel: " + stundenwinkel)
logDebug("Sun.rules", "x: " + x)
logDebug("Sun.rules", "y: " + y)
logDebug("Sun.rules", "sonnenhoehe: " + sonnenhoehe)
logDebug("Sun.rules", "azimut: " + azimut)


logDebug("Sun.rules", "Calculated new SunHeight angle '" + sonnenhoehe + "°'")
logDebug("Sun.rules", "Calculated new Azimut angle '" + sonnenhoehe + "°'")


// Send all calculations to the event bus


Sun_Height.postUpdate(sonnenhoehe)
Sun_Azimut.postUpdate(azimut)


Sun_Dawn_Solar.postUpdate( if (sonnenhoehe < 0) ON else OFF )
Sun_Dawn_Civil.postUpdate( if (sonnenhoehe < 6) ON else OFF )
Sun_Dawn_Nautical.postUpdate( if (sonnenhoehe < 12) ON else OFF )
Sun_Dawn_Astronomical.postUpdate( if (sonnenhoehe < 18) ON else OFF )
end

joeR

unread,
Sep 4, 2012, 2:42:25 AM9/4/12
to ope...@googlegroups.com
Hi Thimas,

here is a mistake in yours code:

Sun_Dawn_Solar.postUpdate( if (sonnenhoehe < 0) ON else OFF )
Sun_Dawn_Civil.postUpdate( if (sonnenhoehe < 6) ON else OFF )
Sun_Dawn_Nautical.postUpdate( if (sonnenhoehe < 12) ON else OFF )
Sun_Dawn_Astronomical.postUpdate( if (sonnenhoehe < 18) ON else OFF )

it sohould be:

Sun_Dawn_Solar.postUpdate( if (sonnenhoehe < 0) ON else OFF )
Sun_Dawn_Civil.postUpdate( if (sonnenhoehe < -6) ON else OFF )
Sun_Dawn_Nautical.postUpdate( if (sonnenhoehe < -12) ON else OFF )
Sun_Dawn_Astronomical.postUpdate( if (sonnenhoehe < -18) ON else OFF )

I get my info from Wikipedia/Dawn. I double checked it!

Regards,
joeR

Message has been deleted
Message has been deleted

pic...@gmail.com

unread,
Sep 14, 2012, 1:42:37 AM9/14/12
to ope...@googlegroups.com
joeR - you are right with negative values, yet dawn and dusk are interchanged. Dawn is the twilight before sunrise, dusk is the twilight after sunset.

Sun_Dawn_Solar.postUpdateif (sonnenhoehe > 0) ON else OFF )
Sun_Dawn_Civil.postUpdateif (sonnenhoehe > -6) ON else OFF )
Sun_Dawn_Nautical.postUpdateif (sonnenhoehe > -12) ON else OFF )
Sun_Dawn_Astronomical.postUpdateif (sonnenhoehe > -18) ON else OFF )

Sun_Dusk_Solar.postUpdateif (sonnenhoehe < 0) ON else OFF )
Sun_Dusk_Civil.postUpdateif (sonnenhoehe < -6) ON else OFF )
Sun_Dusk_Nautical.postUpdateif (sonnenhoehe < -12) ON else OFF )
Sun_Dusk_Astronomical.postUpdateif (sonnenhoehe < -18) ON else OFF )

kleina...@googlemail.com

unread,
Sep 20, 2012, 1:51:03 AM9/20/12
to ope...@googlegroups.com, pic...@gmail.com
Hi,

i have integrated the rules, but i am wondering about the times for sunrise.

Astro triggers at 4:27
Nautical at 5:07
Civil at 5:47
and Solar at 6:27

but the sunrise from wetteronline.de and google says 7:16.

i wondering why none of the openhab values are the same as the time from the websites

Thomas Eichstädt-Engelen

unread,
Sep 20, 2012, 3:49:49 AM9/20/12
to ope...@googlegroups.com
probably the location is different?

- sent from a mobile device -
--
You received this message because you are subscribed to the Google Groups "openhab" group.
To view this discussion on the web visit https://groups.google.com/d/msg/openhab/-/7o4R3z1bjOoJ.

Ralf Klüber

unread,
Sep 20, 2012, 4:26:37 AM9/20/12
to ope...@googlegroups.com
I have currently also complaints from my wife that the rollerblinds are going down to early. Could this be a summertime 1h offset issue?

--
Ralf Klüber

Am Donnerstag, 20. September 2012 um 09:49 schrieb Thomas Eichstädt-Engelen:

probably the location is different?

- sent from a mobile device -


Am 20.09.2012 um 07:51 schrieb "kleina...@googlemail.com" <kleina...@googlemail.com>:

Hi,

i have integrated the rules, but i am wondering about the times for sunrise.

Astro triggers at 4:27
Nautical at 5:07
Civil at 5:47
and Solar at 6:27

but the sunrise from wetteronline.de and google says 7:16.

i wondering why none of the openhab values are the same as the time from the websites

 


Am Freitag, 14. September 2012 07:42:37 UTC+2 schrieb (unbekannt):
joeR - you are right with negative values, yet dawn and dusk are interchanged. Dawn is the twilight before sunrise, dusk is the twilight after sunset.

Sun_Dawn_Solar.postUpdateif (sonnenhoehe > 0) ON else OFF )
Sun_Dawn_Civil.postUpdateif (sonnenhoehe > -6) ON else OFF )
Sun_Dawn_Nautical.postUpdateif (sonnenhoehe > -12) ON else OFF )
Sun_Dawn_Astronomical.postUpdateif (sonnenhoehe > -18) ON else OFF )

Sun_Dusk_Solar.postUpdateif (sonnenhoehe < 0) ON else OFF )
Sun_Dusk_Civil.postUpdateif (sonnenhoehe < -6) ON else OFF )
Sun_Dusk_Nautical.postUpdateif (sonnenhoehe < -12) ON else OFF )
Sun_Dusk_Astronomical.postUpdateif (sonnenhoehe < -18) ON else OFF )

On Tuesday, September 4, 2012 8:42:25 AM UTC+2, joeR wrote:
<Untitled>

--
You received this message because you are subscribed to the Google Groups "openhab" group.
To view this discussion on the web visit https://groups.google.com/d/msg/openhab/-/7o4R3z1bjOoJ.
To post to this group, send email to ope...@googlegroups.com.
To unsubscribe from this group, send email to openhab+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/openhab?hl=en.

Thomas Eichstädt-Engelen

unread,
Sep 23, 2012, 8:41:29 AM9/23/12
to ope...@googlegroups.com
Hi,

it could be a bug as well.

There is an Excle-Sheet available which contains the used formulars (see http://www.jgiesen.de/SME/tk/index.htm) . Does it calculate the same values?

Regards,

Thomas E.-E.



kleina...@googlemail.com

unread,
Sep 24, 2012, 7:22:14 AM9/24/12
to ope...@googlegroups.com
openhab triggers:

4:20 Astronomical (> -18  : Excel : 4:34)
5:10 Nautical (> -12 : Excel 5:15)
5:50 Civil (> -6 : Excel 5:54)
6:30 Solar (> 0 : Excel 6:32)

(5 Min +/- through cron job)

So 1 hour offset is missing for sure: 


00:00—05:32 — night
05:32—06:12 — astronomical twilight
06:12—06:51 — nautical twilight
06:51—07:25 — civil twilight
07:25—07:28 — sunrise
07:28—19:25 — daylight
19:25—19:29 — sunset
19:29—20:02 — civil twilight
20:02—20:41 — nautical twilight
20:41—21:21 — astronomical twilight
21:21—00:00 — night
 


kleina...@googlemail.com

unread,
Sep 24, 2012, 2:55:40 PM9/24/12
to ope...@googlegroups.com, kleina...@googlemail.com
my changes:

import java.util.Calendar
import java.util.GregorianCalendar

...

var GregorianCalendar gc = new GregorianCalendar()
gc.add(Calendar::MILLISECOND, (-1 * gc.get(Calendar::DST_OFFSET))  )

tageszahl = gc.get(Calendar::DAY_OF_YEAR)  

var month  = gc.get(Calendar::MONTH)+1
var day    = gc.get(Calendar::DAY_OF_MONTH)
var hour   = gc.get(Calendar::HOUR_OF_DAY)
var minute = gc.get(Calendar::MINUTE)


Thomas Eichstädt-Engelen

unread,
Sep 24, 2012, 3:14:33 PM9/24/12
to ope...@googlegroups.com
Hi,

on my system the correct time is being returned. Please find out by creating the following rule:


rule "log current time as String"
when
Time cron "0 0/1 * * * ?"
then
logDebug("Test.rules", "Time: " + now)
end


Cheers,

Thomas E.-E.

pic...@gmail.com

unread,
Sep 24, 2012, 4:16:30 PM9/24/12
to ope...@googlegroups.com, kleina...@googlemail.com

I think your change solved the problem. 

 

The "old" version calculated for lat, 52.51, lon. 13.55, time 22:10 a sun height of -32° while the your version calculates -27°.

 

The site http://www.detlefhahn.de/segeln/astro/zeitgleichung.php also returns -27°. As this site has approximately the same sunrise/sunset times as http://www.suncalc.net I assume these as the correct values.

Message has been deleted

kleina...@googlemail.com

unread,
Oct 3, 2012, 7:59:00 AM10/3/12
to ope...@googlegroups.com, kleina...@googlemail.com
i was not glad only with triggers when sunrise or sunset happens.
you are not able to do some triggers with offset... so i found a solution to calc the dates

    var J1970 = 2440588
    var J2000 = 2451545
    var deg2rad = Math::PI / 180
    var M0 = 357.5291 * deg2rad
    var M1 = 0.98560028 * deg2rad
    var J0 = 0.0009
    var J1 = 0.0053
    var J2 = -0.0069
    var C1 = 1.9148 * deg2rad
    var C2 = 0.0200 * deg2rad
    var C3 = 0.0003 * deg2rad
    var P = 102.9372 * deg2rad
    var e = 23.45 * deg2rad
    var th0 = 280.1600 * deg2rad
    var th1 = 360.9856235 * deg2rad
    var h0 = -0.83 * deg2rad //sunset angle
    var d0 = 0.53 * deg2rad //sun diameter
    var h1 = -6 * deg2rad //nautical twilight angle
    var h2 = -12 * deg2rad //astronomical twilight angle
    var h3 = -18 * deg2rad //darkness angle
    var msInDay = 1000 * 60 * 60 * 24
    var lw = -lng * deg2rad
    var phi = lat * deg2rad

    var datum = new Date()

    var J = datum.getTime() / msInDay - 0.5 + J1970
    var n = Math::round( (J - J2000 - J0 - lw/(2 * Math::PI)).doubleValue)
    var Js = (J2000 + J0 + (0 + lw)/(2 * Math::PI) + n)
    var M = ( M0 + M1 * (Js - J2000))
    var C = C1 * Math::sin(M.doubleValue) + C2 * Math::sin((2 * M).doubleValue) + C3 * Math::sin((3 * M).doubleValue)
    var Lsun = M + P + C + Math::PI
    var Jtransit = Js + (J1 * Math::sin(M.doubleValue)) + (J2 * Math::sin((2 * Lsun).doubleValue))
    var d = Math::asin((Math::sin(Lsun.doubleValue) * Math::sin(e.doubleValue)).doubleValue)
    var w0 = Math::acos(((Math::sin(h0.doubleValue) - Math::sin(phi.doubleValue) * Math::sin(d.doubleValue)) / (Math::cos(phi.doubleValue) * Math::cos(d.doubleValue))).doubleValue)
    var w1 = Math::acos(((Math::sin((h0+d0).doubleValue) - Math::sin(phi.doubleValue) * Math::sin(d.doubleValue)) / (Math::cos(phi.doubleValue) * Math::cos(d.doubleValue))).doubleValue)

    var Jset    = J2000 + J0 + (w0 + lw)/(2 * Math::PI) + n // getApproxSolarTransit(w0, lw, n)
    Jset        = Jset + (J1 * Math::sin(M.doubleValue)) + (J2 * Math::sin((2 * Lsun).doubleValue)) // getSolarTransit(JSet, M, Lsun) // getSunsetJulianDate(w0, M, Lsun, lw, n)
    var Jsetstart       = J2000 + J0 + (w1 + lw)/(2 * Math::PI) + n // getApproxSolarTransit(w1, lw, n)
    Jsetstart           = Jsetstart + (J1 * Math::sin(M.doubleValue)) + (J2 * Math::sin((2 * Lsun).doubleValue)) // getSolarTransit(Jsetstart, M, Lsun) // getSunsetJulianDate(w1, M, Lsun, lw, n),

    var Jrise           = Jtransit - (Jset - Jtransit) // getSunriseJulianDate(Jtransit, Jset),
    var Jriseend        = Jtransit - (Jsetstart - Jtransit) //  getSunriseJulianDate(Jtransit, Jsetstart)

    var sunrise_start = new Date( ( (Jrise + 0.5 - J1970) * msInDay).longValue )
    var sunrise_end   = new Date( ( (Jriseend + 0.5 - J1970) * msInDay).longValue )
    logWarn ("Calc Times", "sunrise_start : " + sunrise_start)
    logWarn ("Calc Times", "sunrise_end   : " + sunrise_end)

    var sunset_start  = new Date( ((Jsetstart + 0.5 - J1970) * msInDay).longValue )
    var sunset_end    = new Date( ((Jset + 0.5 - J1970) * msInDay).longValue)
    logWarn ("Calc Times", "sunset_start : " + sunset_start)
    logWarn ("Calc Times", "sunset_end   : " + sunset_end)

Thomas Eichstädt-Engelen

unread,
Oct 3, 2012, 8:07:07 AM10/3/12
to ope...@googlegroups.com
wow ...


--
You received this message because you are subscribed to the Google Groups "openhab" group.
To view this discussion on the web visit https://groups.google.com/d/msg/openhab/-/Q-nbHBQgDJkJ.

kleina...@googlemail.com

unread,
Oct 3, 2012, 8:55:28 AM10/3/12
to ope...@googlegroups.com
i now must figure out how to update a DateTime Item with the date value.
may be someone can help

kleina...@googlemail.com

unread,
Oct 3, 2012, 10:08:41 AM10/3/12
to ope...@googlegroups.com, kleina...@googlemail.com
var  Calendar calendar = Calendar::getInstance()
calendar.setTimeInMillis( sunrise_start.getTime() )
SunRise.postUpdate( new DateTimeType(calendar))

kleina...@googlemail.com

unread,
Oct 3, 2012, 10:28:23 AM10/3/12
to ope...@googlegroups.com, kleina...@googlemail.com
Here is the cleanup code:

import org.openhab.core.library.types.*
import java.lang.Math
import java.util.Date
import java.util.Calendar

// Change this reflecting your destination
var Number lat = 33.270699
var Number lng = 4.531302

rule "Calc Times"
when
    Time cron "0 0/5 * * * ?"
then
    var Jset            = J2000 + J0 + (w0 + lw)/(2 * Math::PI) + n + (J1 * Math::sin(M.doubleValue)) + (J2 * Math::sin((2 * Lsun).doubleValue))
    var Jsetstart       = J2000 + J0 + (w1 + lw)/(2 * Math::PI) + n + (J1 * Math::sin(M.doubleValue)) + (J2 * Math::sin((2 * Lsun).doubleValue))

    var Jrise           = Jtransit - (Jset - Jtransit)
    var Jriseend        = Jtransit - (Jsetstart - Jtransit)

    var sunrise_ms_start = ( (Jrise + 0.5 - J1970) * msInDay).longValue
    var sunrise_ms_end   = ( (Jriseend + 0.5 - J1970) * msInDay).longValue
    var sunset_ms_start  = ((Jsetstart + 0.5 - J1970) * msInDay).longValue
    var sunset_ms_end    = ((Jset + 0.5 - J1970) * msInDay).longValue

    // Items:
    // DateTime        SunRise "Sonnenaufgang [%1$tA, %1$td.%1$tm.%1$tY %1$tT]"                <calendar>
    // DateTime        SunSet  "Sonnenuntergang [%1$tA, %1$td.%1$tm.%1$tY %1$tT]"         <calendar>

    var Calendar sunrise_start = Calendar::getInstance()
    sunrise_start.setTimeInMillis( sunrise_ms_start )
    SunRise.postUpdate( new DateTimeType(sunrise_start))

    var  Calendar sunset_start = Calendar::getInstance()
    sunset_start.setTimeInMillis( sunset_ms_start )
    SunSet.postUpdate( new DateTimeType(sunset_start))


end

Thomas Eichstädt-Engelen

unread,
Oct 3, 2012, 11:26:05 AM10/3/12
to ope...@googlegroups.com

btw, which variables contains the sun height and the azimuth? Are they still available? They are necessary for blinds control.

Regards,

Thomas E.-E.


- sent from a mobile device -

Am 03.10.2012 um 16:28  "kleina...@googlemail.com" <kleina...@googlemail.com>:

--
You received this message because you are subscribed to the Google Groups "openhab" group.
To view this discussion on the web visit https://groups.google.com/d/msg/openhab/-/q8UEHZ69LcsJ.

Ralf Klüber

unread,
Oct 3, 2012, 12:22:20 PM10/3/12
to ope...@googlegroups.com
Why calculating this rule every 5min? Once a day should be enough, right?

--
Ralf Klüber

kleina...@googlemail.com

unread,
Oct 3, 2012, 1:51:09 PM10/3/12
to ope...@googlegroups.com
The 5 min are for Testing.
I now add Timers

pic...@gmail.com

unread,
Oct 23, 2012, 1:34:30 PM10/23/12
to ope...@googlegroups.com
I'm trying to use the Sun_Dusk_Civil item in a rule. OH enters the rule, but nver evaluates the if clause to true.
The command "openhab status Sun_Dusk_Civil" results to ON

rule "Haustuer Licht"
when
Item Front_Door changed from CLOSED to OPEN
then
if (Sun_Dusk_Civil == ON) {
sendCommand(Light_EG_Flur, ON)
}
end


Eichstädt-Engelen Thomas

unread,
Oct 23, 2012, 2:41:18 PM10/23/12
to ope...@googlegroups.com
try

Sun_Dusk_Civil.state == On

Regards,

Thomas
--
You received this message because you are subscribed to the Google Groups "openhab" group.
To view this discussion on the web visit https://groups.google.com/d/msg/openhab/-/B4lE33WXHyAJ.

pic...@gmail.com

unread,
Oct 23, 2012, 4:33:06 PM10/23/12
to ope...@googlegroups.com
Well thanks, that was the clue.
Reply all
Reply to author
Forward
0 new messages