Fire for TIME IS Rule

Here is a topic that you may find helpful with dates and times. DateTime Conversion

Here is a rule example for certain times of the day:

//Rule for using time events
rule "Calculate time of day state"
when
    Time cron "0 * * ? * *" 
//    System started or
//    Time cron "0 30 8 * * ? *" or
//    Time cron "0 0 0 * * ? *" or
//    Channel 'astro:sun:local:civilDawn#event' triggered START or
//    Channel 'astro:sun:local:civilDusk#event' triggered END 
then
    val String curr_time = now.toString("HH:mm")
    val String night_start = "00:00"
    var String morning_start = "06:00"
    val String day_start = (Sun_Dawn_Start.state as DateTimeType).format("%1$tH:%1$tM")
    val String evening_start = (Sun_Dusk_End.state as DateTimeType).format("%1$tH:%1$tM")
    val String midnight_time = "24:00"

    var new_val = "UNKNOWN"

    if (day_start < morning_start) {
        morning_start = day_start
    }
    switch true {
        case night_start <= curr_time && curr_time < morning_start:   new_val = "NIGHT"
        case morning_start <= curr_time && curr_time < day_start:     new_val = "MORNING"
        case day_start <= curr_time && curr_time < evening_start:     new_val = "DAY"
        case evening_start <= curr_time && curr_time < midnight_time: new_val = "EVENING"
    }
//  logInfo("Time_Of_Day", "curr_time=" + curr_time + ", night_start=" + night_start + ", morning_start=" + morning_start
//      + ", day_start=" + day_start + ", evening_start=" + evening_start + ", midnight_time=" + midnight_time
//      + ", new_val=" + new_val)


    if (Time_Of_Day.state.toString != new_val) {
        logInfo("Time_Of_Day", "Current time of day is now " + new_val)
        Time_Of_Day.sendCommand(new_val)
    }
    
end

And this is a rule to control a light using the Astro binding to turn it on and a cron time to turn it off.

rule "Front Porch Lights ON"

when

Channel 'astro:sun:5ed19d26:civilDusk#event' triggered START

then

	FrontPorch_Light.sendCommand(ON)

end


rule "Front Porch light OFF"

when

	Time cron "0 30 21 ? * *"             //OFF at 9:30 pm everyday

then

	FrontPorch_Light.sendCommand(OFF)

end

Between the link and both rule examples I hope you found something that helps.

2 Likes