DateTime calculations in rules for Daylight switch

I wish to determine if it is Daylight by setting a switch ON/OFF. I have created the following items:

Switch Daylight "Daylight" <switch>
Number Daylight_Offset_Evening "Daylight Offset (Evening)" <time>
Number Daylight_Offset_Morning "Daylight Offset (Morning)" <time>

I know about the Astro bindings’ events, but I wish to accomplish this by running a cron-based rule every minute, to determine the Daylight state. The rule should work as follows:

rule "Daylight_Update"
    when 
        Time cron "* 0/1 * * * ?"
    then
        logInfo("Daylight_Update","Is it Daylight?")

        val currentTime = now
        val daylightStart = Astro_Sun_Rise.state.plus( Daylight_Offset_Morning.state )
        val daylightEnd = Astro_Sun_Set.state.plus( Daylight_Offset_Evening.state )

        if ( currentTime < daylightStart || currentTime > daylightEnd ) { 
            logInfo("Daylight_Update","It is NOT daylight.")          

            if ( Daylight.state != OFF ) {
                logInfo("Daylight_Update","Send command: Daylight = OFF")
                Daylight.sendCommand(OFF)
            }
        }
        else {
            logInfo("Daylight_Update","It is daylight.")

            if ( Daylight.state != ON ) {
                logInfo("Daylight_Update","Send command: Daylight = ON")
                Daylight.sendCommand(ON)
            }
        }

    end

Could someone kindly help me, making the rule above “correct”?

(It is currently just my draft, as I do not really know Java/Xtend that well…)

Design Pattern

Take a look at the link above and see if that offers you some help.

Thanks for the link. I must have missed this, because it is very similar to what I want to do. I have now made my rule work. For curious ones, see below.

Items:

Switch Daylight "Dagslys lige nu" <sun> (Astronomy, Settings)
DateTime Daylight_Start "Dagslys starter [kl. %1$tH:%1$tM]" <time> (Astronomy)
DateTime Daylight_End "Dagslys slutter [kl. %1$tH:%1$tM]" <time> (Astronomy)
Number Daylight_Start_Offset "Dagslys forskudt ift. solopgang [%d min.]" <time> (Astronomy, Settings)
Number Daylight_End_Offset "Dagslys forskudt ift. solnedgang [%d min.]" <time> (Astronomy, Settings)

Rule:

rule "Daylight_Update"
    when 
        Time cron "0 0/1 * * * ?" or
        Item Daylight_Start_Offset changed or
        Item Daylight_End_Offset changed or
        Item Astro_Sun_Rise changed or
        Item Astro_Sun_Set changed
    then
        // logInfo("Daylight_Update", "Starting rule.")

        val sunrise = new DateTime( Astro_Sun_Rise.state.toString )
        val sunset = new DateTime( Astro_Sun_Set.state.toString )

        val daylightStartOffset = (Daylight_Start_Offset.state as DecimalType).intValue
        val daylightEndOffset = (Daylight_End_Offset.state as DecimalType).intValue

        val daylightStart = sunrise.plusMinutes( daylightStartOffset )
        val daylightEnd = sunset.plusMinutes( daylightEndOffset )

        Daylight_Start.postUpdate( daylightStart.toString )
        Daylight_End.postUpdate( daylightEnd.toString )

        if ( now.isAfter(daylightStart) && now.isBefore(daylightEnd) ) {
            
            // logInfo("Daylight_Update", "It is daylight." )

            if ( Daylight.state != ON ) {
                Daylight.sendCommand(ON)
            }
        }
        else {

            // logInfo("Daylight_Update", "It is not daylight." ) 

            if ( Daylight.state != OFF ) {
                Daylight.sendCommand(OFF)
            }
        }        
        
    end
2 Likes

You should lookfor the design pattern Time of Day

Please mark the thread as solved, thanks