Need rules to get daylight time of today and less/more for yesterday and tomorrow

Raspberry openHab 2.5

Hi there
I’m looking (if it’s possible) a rules wich will calculate the time of daylight of the day and tells me how many minutes more or less from yesterday and tomorrow.

Has anyone this kind of rules?
I’m not sure to be able to this kind of rules.

Thanks in adavance for any help.

Best regards


Take a look into the documentation of the ASTRO binding.


Funny enough, I did this two days ago :

@rule("Astro : Synthese")
@when("Item Sun_Rise changed")
@when("Item Sun_Set changed")
@when("Item Mode changed")
@when("System started")
def astroAdjustRadiationLevel(event):
	sunrise = ir.getItem("Sun_Rise").getState()
	sunset = ir.getItem("Sun_Set").getState()
	mode = ir.getItem("Mode").getState()
	if isinstance(sunrise,UnDefType) or isinstance(sunset, UnDefType) or isinstance(mode, UnDefType):

	previousDuree = ir.getItem("dayLength").getState()
	duree = minutes_between(sunrise,sunset)
	deltaDuree = 0
	if not isinstance(previousDuree,UnDefType):
		deltaDuree = duree - previousDuree
	events.postUpdate("dayLength", str(duree))
	chaine = u"{} - " + SUNRISE	 + u" {} " + SUNSET + u" {} - Durée : {} minutes (Variation : {})"
	chaine = chaine.format(mode,sunrise.format("%1$tR"),sunset.format("%1$tR"), duree, deltaDuree)

Hi glhopital

Thanks for your reply. I’m not used to see rules like this. Always something new:)
But I don’t understand all of it.
What for is Mode and deltaDuree?
And is there a possibility to get the rise and set for tomorrow?

Best regards

Use the OH 1.astro action…

Here are some details for how to do this using scripted automation…

Mode as nothing to do with your initial question. I just posted my rule as is. Sun_Rise and Sun_Set comes from Astro binding directly. I would not recommend using Astro action as it is a v1 OH package that is likely to disappear in the future.

Please be aware that this is not rules DSL but jython. You will have to install additional stuff to use this “new rules”.

You can use the Astro Binding (V2) to get the information, at least for yesterday (save the data before midnight to the item) and today. I’m not aware of an option to use astro actions with v2 binding, but maybe the author of the binding has some tips.

HI there

Thanks all of you and your replys.
I did it like this. Shurly not the best possibility but it works :slight_smile:

I used Astro Binding to get daylength. To calculate difference between yesterday/today and today/tomorrow I used a Python script.
This gave me the hint: Astro binding - Enhancement for tomorrow's sunrise and sunset
I didn’t use Astro Binding for today since it didn’t give me same time of sunrise and sunset.

My Items:

DateTime Astro_Sunrise_Time          "Sunrise [%1$tH:%1$tM]"          <sunrise>           (gAstroSun)                                    {channel="astro:sun:local:rise#start"}
DateTime Astro_Sunset_Time           "Sunset [%1$tR]"              <sunset>            (gAstroSun)                                    {channel="astro:sun:local:set#start"}
String Astro_Daylength               "Daylength [%s]"                   <sun>               (gAstroSun)
String Astro_Daylength_Y             "+/- minutes with yesterday [%s]"                     <sun>               (gAstroSun)
String Astro_Daylength_T             "+/- minutes with tomorrow [%s]"                   <sun>               (gAstroSun)

My Rules:

rule "Astro_DayLength"
    System started or
    Time cron "0 5 * * * ? *" // Every hour at 5 past
        var sunrise = new DateTime(Astro_Sunrise_Time.state.toString)
        var sunset = new DateTime(Astro_Sunset_Time.state.toString)
        var duree=Math.abs(new Duration(sunrise, sunset).getStandardMinutes())
        var hrs = (duree / 60) % 60
        var min = (duree - (hrs*60))
        Astro_Daylength.postUpdate(String::format("%02dh%02d",  hrs, min))

rule "tomorrow's less_more daylength"
    System started or
    Time cron "0 5 * * * ? *" // Every hour at 5 past
    var String minString = executeCommandLine("python /etc/openhab2/scripts/", 5000)
    logInfo("Tomorrow's more/less minutes: ", minString)

rule "yesterday's less_more daylength"
    System started or
    Time cron "0 5 * * * ? *" // Every hour at 5 past
    var String min1String = executeCommandLine("python /etc/openhab2/scripts/", 5000)
    logInfo("Yesterday's more/less minutes: ", min1String)

My Python script: (one off two)


import datetime
import astral
import datetime as dt

my = astral.Location(info=("City", "Country", 4x.xxxxxx, x.xxxxx, "Europe/xxxxx", 574))
my.solar_depression = 'civil'
FMT = '%H:%M:%S'

yesterday = + datetime.timedelta(-1)
sun = my.sun(date=yesterday, local=True)

y1 = str(sun['sunrise'].isoformat())[11:19]
y2 = str(sun['sunset'].isoformat())[11:19]

diff1 = dt.datetime.strptime(y2,FMT) -  dt.datetime.strptime(y1,FMT)
min1 = diff1.seconds/60

today =
sun = my.sun(date=today, local=True)

t1 = str(sun['sunrise'].isoformat())[11:19]
t2 = str(sun['sunset'].isoformat())[11:19]

diff2 = dt.datetime.strptime(t2,FMT) -  dt.datetime.strptime(t1,FMT)
min2 = diff2.seconds/60

print (min1-min2)

Best regards and thanks again

Please use code fences

You can edit old posts and add code fences to make it more readable.