Rules and complex time comparisons

Hi community,

I want to create one or more rules that switch on my garden lights every morning earliest at 6 AM and switch them off at sunrise.
Obviously, there’s no need to switch on the lights, if sunrise is before 6 AM - which can be quite common in the summer months.

If I understood the documentation correctly, the rule conditions with Time are quite simple. Like, you can only use the is comparison as in Time is xyz. A comparison like Time >= xyz doesn’t work - at least for me.
Therefore I assume, I will need to do something like this and check the condition within the rule using if or the like.

rule "GardenLightMorning"
when
//             s m  h d M dow y
    Time cron "0 0 6 * * * *" 
then
    // before switching on the light, check
    // that the sun has not yet risen
    // the following is pseudo-code, since I don't know which classes and methods I can use
    // Sun_rise_StartTime is the calculated sunrise time from "astro:sun:home:rise#start"
    // GardenLight is the group of garden light switches
    if (Time < Sun_rise_StartTime) {
      GardenLight.sendCommand(ON)   
    }
end

Is this a common requirement? I did not find any examples that are similar.

Best regards

Marco

The rule that I use to turn on the light in case it is getting dark before 20:15

rule "rule triggered by sun set ( to turn on the light on sunset )"
when
    Channel 'astro:sun:home:set#event' triggered START
then
        if ( ( LocalDateTime.now().isBefore(LocalDate.now().atTime(20, 15) ) ) ) {
                logInfo( "rule triggerd by sun set", "SunSet before 20:15" )
                Rule_Aussenlicht_einschalten.postUpdate( ON )
        }
        else {
                logInfo( "rule triggerd by sun set", "SunSet after 20:15" )
        }
end
1 Like

Hi @Wolfgang_S,

thanks - that was quick and got me in the right direction, I think.
After a bit more digging, I came up with this:

rule "LimitAussenlichtMorgen"
when
//             s m  h d M dow y
    Time cron "0 0 6 * * * *" 
then
    // before switching on the light, check
    // that the sun has not yet risen
   var day_start = (Sun_rise_StartTime.state as DateTimeType).getZonedDateTime()
   if ( ( now().isBefore(day_start) ) ) {
    Aussenlicht.sendCommand(ON)
   }
end

I guess this should work; will see tomorrow.

Thanks a lot!

Cheers

Marco

This line is a little over done. It can be:

if( now.isBefore(day_start) ){

Another way you can do this is through the Astro rule Actions. If the azimuth or the elevation is 0 that means the sun hasn’t risen yet.

if(sunActions.getAzimuth() <= 0) {

or

if(sunActions.getElevation() <= 0) {
1 Like

Hi @rlkoshak ,
thanks for following up on that.
I decided using getElevation() which in my case is also reflected in the item SunElevation and finally came up with

rule "LimitAussenlichtMorgen"
when
//             s m  h d M dow y
    Time cron "0 0 6 * * * *" 
then
    if ( Sun_Elevation.state <= 0) {
        logInfo ("rule LimitAussenlichtMorgen triggered", "Sonne noch nicht aufgegangen => Licht an")
        Aussenlicht.sendCommand(ON)
    } else {
        logInfo ("rule LimitAussenlichtMorgen triggered", "Sonne bereits aufgegangen => Licht nicht an")
    }
end
Number:Angle Sun_Elevation          "Sonnenstand"          {channel="astro:sun:home:position#elevation"}