Motion sensor During the Day and socket timmer Triggered from Light switch

Hello. I am new at the openhab and my English level is not very good so I have someone help me with the language. I am trying to Create Two Rules but something is wrong with them and I need some help to Make them work. The first One is; from 8:00 till 22:00 I want the Sled_kitchen to be on and from 22:00 till 8:00 to on/off with the motion sensor and after 8:00 to be on. That is the code that I am trying to do. :

rule " Time Led kitchen 7:50 on"
when
Time cron "0 50 7 ? * * " or
then
if ( now.getMinuteOfDay >= (60
7+42) || now.getMinuteOfDay <= (60*21+32) ) {
SLed_white_Kitchen.sendCommand(“ON”)
SLed_white_Kitchen.postUpdate(“ON”)
}
end

// Motion Sensor No:1
rule " Motion Sensor No:1"
when
Item SLed_white_Kitchen2 changed
then
if ( now.getMinuteOfDay >= (6021+30) || now.getMinuteOfDay <= (607+40) && SLed_white_Kitchen2.state == ON) {
SLed_white_Kitchen.sendCommand(“ON”)
SLed_white_Kitchen.postUpdate(“ON”)
} else {
SLed_white_Kitchen.sendCommand(“OFF”)
SLed_white_Kitchen.postUpdate(“OFF”)
}
end

The second; I have Created a socket at the kitchen which I want to be on timer 4 minutes from 7:00 till 10:00 which turn on from the switch Sbathroom. The other hours of the Day I want to be on without timer. What I want to achieve with that is for examble; At the morning I want my toaster to turn on when I Open the Light of the bathroom and count 4 minutes and then turn off so as my bread does not burn.

import org.openhab.core.library.types.*

import org.openhab.core.persistence.*

import org.openhab.model.script.actions.*

var Timer mytimers = null

rule “TOST TIMER”

when

 Item Bathroom_Light changed from OFF to ON

then

if ((Bathroom_Light.state == ON)) {

if (mytimers == null) {

  mytimers = createTimer(now.plusSeconds(4)) [|

    postUpdate(Switch_Tost, OFF)

    sendCommand(Switch_Tost, OFF)

  ]

}

}

else if(mytimers != null) {

mytimers.cancel()

mytimers = null

}

end

You are looking at very old openHAB1 examples, and should not be using this code with openHAB2

Rules are event based. So your “Time Led kitchen 7:50 on” Rule runs once at exactly (or at least really really close) to 07:50:00 every day. Why test what minute of the day it is? It can and only does ever run at 07:50:00.

So your test makes no sense because you are checking if it’s after ~11:00 or before 21:32. 07:50:00 isn’t between around ~11:00 and 21:32. Get rid of the if statement, it doesn’t do anything and it doesn’t make sense.

In Motion Sensor No:1, you are asking if the minute of the day is greater than 6051. A day only has 1440 minutes in it so that will never be true.

I recommend looking at and applying the [Deprecated] Design Pattern: Time Of Day for a better way to centralize and manage these sorts of time checks.

This one is relatively simple and it pretty much already does what you are saying what you want it to do except for not actually turning on the toast switch and checking the time before deciding whether it should turn on the toast switch and then set the timer to turn it off.

Hello
I did something that works.
I don’t know if that’s the right way.
i found a code here but don’t remember the link. Time of Day Events
Astro Things :


     
     
      
      // Local Ελλάδα 
      
      astro:sun:local  [ geolocation="40.2748869,23.5065873", interval=300 ]
      
      astro:moon:local [ geolocation="40.2748869,23.5065873", interval=300 ]
      
      // Plus 2 -1:00 Ισπανία
      
      astro:sun:plus2 [ geolocation="40.433166,-3.689845", interval=300 ]
      
      astro:moon:plus2 [ geolocation="40.433166,-3.689845", interval=300 ]
     
     
     


Time Of Day Local rules




```csv

import java.util.Date
val String RFN = "time-of-day.rules"

rule "Get time period for right now"
when
    System started
then
    val now = new Date()
    val dawn = new Date((DawnStart_Time.state as DateTimeType).calendar.timeInMillis)
    val day = new Date((DayStart_Time.state as DateTimeType).calendar.timeInMillis)
    val dusk = new Date((DuskStart_Time.state as DateTimeType).calendar.timeInMillis)
    val night = new Date((NightStart_Time.state as DateTimeType).calendar.timeInMillis)

    val String initStr = "Initializing time period. The time of day is "

    if(now.after(dawn) && now.before(day)) {
        logInfo(RFN, initStr + "Dawn: " + now)
        Dawn.sendCommand(ON)
        Day.sendCommand(OFF)
        Dusk.sendCommand(OFF)
        Night.sendCommand(OFF)
        TimePeriodOfDay.postUpdate("Dawn")
    }
    else if(now.after(day) && now.before(dusk)) {
        logInfo(RFN, initStr + "Day: " + now)
        Dawn.sendCommand(OFF)
        Day.sendCommand(ON)
        Dusk.sendCommand(OFF)
        Night.sendCommand(OFF)
        TimePeriodOfDay.postUpdate("Day")
    }
    else if(now.after(dusk) && now.before(night)) {
        logInfo(RFN, initStr + "Dusk: " + now)
        Dawn.sendCommand(OFF)
        Day.sendCommand(OFF)
        Dusk.sendCommand(ON)
        Night.sendCommand(OFF)
        TimePeriodOfDay.postUpdate("Dusk")
    }
    else {
        logInfo(RFN, initStr + "Night: " + now)
        Dawn.sendCommand(OFF)
        Day.sendCommand(OFF)
        Dusk.sendCommand(OFF)
        Night.sendCommand(ON)
        TimePeriodOfDay.postUpdate("Night")
    }
end

rule "Generate Time of Day Events"
when
    Item CurrentTime received update
then
    val now = new Date()
    val dawn = new Date((DawnStart_Time.state as DateTimeType).calendar.timeInMillis)
    val day = new Date((DayStart_Time.state as DateTimeType).calendar.timeInMillis)
    val dusk = new Date((DuskStart_Time.state as DateTimeType).calendar.timeInMillis)
    val night = new Date((NightStart_Time.state as DateTimeType).calendar.timeInMillis)
    val sunset = new Date((SunsetStart_Time.state as DateTimeType).calendar.timeInMillis)
    val sunrise = new Date((SunriseStart_Time.state as DateTimeType).calendar.timeInMillis)

    if((now.getTime-(now.getTime%60000)) == (dawn.getTime-(dawn.getTime%60000))) {
        logInfo(RFN, "Transitioning to Dawn!!!")
        DawnStart_Event.postUpdate(ON)
    }
    else if((now.getTime-(now.getTime%60000)) == (day.getTime-(day.getTime%60000))) {
        logInfo(RFN, "Transitioning to Day!!!")
        DayStart_Event.postUpdate(ON)
    }
    else if((now.getTime-(now.getTime%60000)) == (dusk.getTime-(dusk.getTime%60000))) {
        logInfo(RFN, "Transitioning to Dusk!!!")
        DuskStart_Event.postUpdate(ON)
    }
    else if((now.getTime-(now.getTime%60000)) == (night.getTime-(night.getTime%60000))) {
        logInfo(RFN, "Transitioning to Night!!!")
        NightStart_Event.postUpdate(ON)
    }

    if((now.getTime-(now.getTime%60000)) == (sunrise.getTime-(sunrise.getTime%60000))) {
        logInfo(RFN, "Start of Sunrise!")
        SunriseStart_Event.postUpdate(ON)
    }
    else if((now.getTime-(now.getTime%60000)) == (sunset.getTime-(sunset.getTime%60000))) {
        logInfo(RFN, "Start of Sunset!")
        SunsetStart_Event.postUpdate(ON)
    }
end

rule "Dawn Started"
when
    Item DawnStart_Event received update ON
then
    val now = new Date()
    val dawn = new Date((DawnStart_Time.state as DateTimeType).calendar.timeInMillis)
    val day = new Date((DayStart_Time.state as DateTimeType).calendar.timeInMillis)

    if(now.after(dawn) && now.before(day)) {
        logInfo(RFN, "Its Dawn: " + now)
        Dawn.sendCommand(ON)
        Day.sendCommand(OFF)
        Dusk.sendCommand(OFF)
        Night.sendCommand(OFF)
        TimePeriodOfDay.postUpdate("Dawn")
    }
end

rule "Day Started"
when
    Item DayStart_Event received update ON
then
    val now = new Date()
    val day = new Date((DayStart_Time.state as DateTimeType).calendar.timeInMillis)
    val dusk = new Date((DuskStart_Time.state as DateTimeType).calendar.timeInMillis)

    if(now.after(day) && now.before(dusk)) {
        logInfo(RFN, "Its Day: " + now)
        Dawn.sendCommand(OFF)
        Day.sendCommand(ON)
        Dusk.sendCommand(OFF)
        Night.sendCommand(OFF)
        TimePeriodOfDay.postUpdate("Day")
    }
end

rule "Dusk started"
when
    Item DuskStart_Event received update ON
then
    val now = new Date()
    val dusk = new Date((DuskStart_Time.state as DateTimeType).calendar.timeInMillis)
    val night = new Date((NightStart_Time.state as DateTimeType).calendar.timeInMillis)

    if(now.after(dusk) && now.before(night)) {
        logInfo(RFN, "Its Dusk: " + now)
        Dawn.sendCommand(OFF)
        Day.sendCommand(OFF)
        Dusk.sendCommand(ON)
        Night.sendCommand(OFF)
        TimePeriodOfDay.postUpdate("Dusk")
    }
end

rule "Night started"
when
    Item NightStart_Event received update ON
then
    val now = new Date()
    val morningNightStart = new Date((MorningNightStart_Time.state as DateTimeType).calendar.timeInMillis)
    val morningNightStop = new Date((MorningNightStop_Time.state as DateTimeType).calendar.timeInMillis)
    val eveningNightStart = new Date((EveningNightStart_Time.state as DateTimeType).calendar.timeInMillis)
    val eveningNightStop = new Date((EveningNightStop_Time.state as DateTimeType).calendar.timeInMillis)

    if((now.after(morningNightStart) && now.before(morningNightStop)) || 
       (now.after(eveningNightStart) && now.before(eveningNightStop))) {
        logInfo(RFN, "Its Night: " + now)
        Dawn.sendCommand(OFF)
        Day.sendCommand(OFF)
        Dusk.sendCommand(OFF)
        Night.sendCommand(ON)
        TimePeriodOfDay.postUpdate("Night")
    }
end

rule "Sunrise started"
when
    Item SunriseStart_Event received update ON
then
    val now = new Date()
    logInfo(RFN, "Its Sunrise : " + now)
end

rule "Sunset started"
when
    Item SunsetStart_Event received update ON
then
    val now = new Date()
    logInfo(RFN, "Its Sunset : " + now)
end

Time Of Day : Plus 2 -1:00 Ισπανία


rule "Get time period for right now 2"
when
    System started
then
    val now = new Date()
    val dawn = new Date((DawnStart_Time2.state as DateTimeType).calendar.timeInMillis)
    val day = new Date((DayStart_Time2.state as DateTimeType).calendar.timeInMillis)
    val dusk = new Date((DuskStart_Time2.state as DateTimeType).calendar.timeInMillis)
    val night = new Date((NightStart_Time2.state as DateTimeType).calendar.timeInMillis)

    val String initStr = "Initializing time period 2. The time of day is "

    if(now.after(dawn) && now.before(day)) {
        logInfo(RFN, initStr + "Dawn2: " + now)
        Dawn2.sendCommand(ON)
        Day2.sendCommand(OFF)
        Dusk2.sendCommand(OFF)
        Night2.sendCommand(OFF)
        TimePeriodOfDay2.postUpdate("Dawn2")
    }
    else if(now.after(day) && now.before(dusk)) {
        logInfo(RFN, initStr + "Day2: " + now)
        Dawn2.sendCommand(OFF)
        Day2.sendCommand(ON)
        Dusk2.sendCommand(OFF)
        Night2.sendCommand(OFF)
        TimePeriodOfDay2.postUpdate("Day2")
    }
    else if(now.after(dusk) && now.before(night)) {
        logInfo(RFN, initStr + "Dusk2: " + now)
        Dawn2.sendCommand(OFF)
        Day2.sendCommand(OFF)
        Dusk2.sendCommand(ON)
        Night2.sendCommand(OFF)
        TimePeriodOfDay2.postUpdate("Dusk2")
    }
    else {
        logInfo(RFN, initStr + "Night2: " + now)
        Dawn2.sendCommand(OFF)
        Day2.sendCommand(OFF)
        Dusk2.sendCommand(OFF)
        Night2.sendCommand(ON)
        TimePeriodOfDay2.postUpdate("Night2")
    }
end

rule "Generate Time of Day Events 2"
when
    Item CurrentTime received update
then
    val now = new Date()
    val dawn = new Date((DawnStart_Time2.state as DateTimeType).calendar.timeInMillis)
    val day = new Date((DayStart_Time2.state as DateTimeType).calendar.timeInMillis)
    val dusk = new Date((DuskStart_Time2.state as DateTimeType).calendar.timeInMillis)
    val night = new Date((NightStart_Time2.state as DateTimeType).calendar.timeInMillis)
    val sunset = new Date((SunsetStart_Time2.state as DateTimeType).calendar.timeInMillis)
    val sunrise = new Date((SunriseStart_Time2.state as DateTimeType).calendar.timeInMillis)

    if((now.getTime-(now.getTime%60000)) == (dawn.getTime-(dawn.getTime%60000))) {
        logInfo(RFN, "Transitioning to Dawn2!!!")
        DawnStart_Event2.postUpdate(ON)
    }
    else if((now.getTime-(now.getTime%60000)) == (day.getTime-(day.getTime%60000))) {
        logInfo(RFN, "Transitioning to Day2!!!")
        DayStart_Event2.postUpdate(ON)
    }
    else if((now.getTime-(now.getTime%60000)) == (dusk.getTime-(dusk.getTime%60000))) {
        logInfo(RFN, "Transitioning to Dusk2!!!")
        DuskStart_Event2.postUpdate(ON)
    }
    else if((now.getTime-(now.getTime%60000)) == (night.getTime-(night.getTime%60000))) {
        logInfo(RFN, "Transitioning to Night2!!!")
        NightStart_Event2.postUpdate(ON)
    }

    if((now.getTime-(now.getTime%60000)) == (sunrise.getTime-(sunrise.getTime%60000))) {
        logInfo(RFN, "Start of Sunrise2!")
        SunriseStart_Event2.postUpdate(ON)
    }
    else if((now.getTime-(now.getTime%60000)) == (sunset.getTime-(sunset.getTime%60000))) {
        logInfo(RFN, "Start of Sunset2!")
        SunsetStart_Event2.postUpdate(ON)
    }
end

rule "Dawn Started 2"
when
    Item DawnStart_Event2 received update ON
then
    val now = new Date()
    val dawn = new Date((DawnStart_Time2.state as DateTimeType).calendar.timeInMillis)
    val day = new Date((DayStart_Time2.state as DateTimeType).calendar.timeInMillis)

    if(now.after(dawn) && now.before(day)) {
        logInfo(RFN, "Its Dawn2: " + now)
        Dawn2.sendCommand(ON)
        Day2.sendCommand(OFF)
        Dusk2.sendCommand(OFF)
        Night2.sendCommand(OFF)
        TimePeriodOfDay2.postUpdate("Dawn2")
    }
end

rule "Day Started 2"
when
    Item DayStart_Event2 received update ON
then
    val now = new Date()
    val day = new Date((DayStart_Time2.state as DateTimeType).calendar.timeInMillis)
    val dusk = new Date((DuskStart_Time2.state as DateTimeType).calendar.timeInMillis)

    if(now.after(day) && now.before(dusk)) {
        logInfo(RFN, "Its Day2: " + now)
        Dawn2.sendCommand(OFF)
        Day2.sendCommand(ON)
        Dusk2.sendCommand(OFF)
        Night2.sendCommand(OFF)
        TimePeriodOfDay2.postUpdate("Day2")
    }
end

rule "Dusk started 2"
when
    Item DuskStart_Event2 received update ON
then
    val now = new Date()
    val dusk = new Date((DuskStart_Time2.state as DateTimeType).calendar.timeInMillis)
    val night = new Date((NightStart_Time2.state as DateTimeType).calendar.timeInMillis)

    if(now.after(dusk) && now.before(night)) {
        logInfo(RFN, "Its Dusk2: " + now)
        Dawn2.sendCommand(OFF)
        Day2.sendCommand(OFF)
        Dusk2.sendCommand(ON)
        Night2.sendCommand(OFF)
        TimePeriodOfDay2.postUpdate("Dusk2")
    }
end

rule "Night started 2"
when
    Item NightStart_Event2 received update ON
then
    val now = new Date()
    val morningNightStart = new Date((MorningNightStart_Time2.state as DateTimeType).calendar.timeInMillis)
    val morningNightStop = new Date((MorningNightStop_Time2.state as DateTimeType).calendar.timeInMillis)
    val eveningNightStart = new Date((EveningNightStart_Time2.state as DateTimeType).calendar.timeInMillis)
    val eveningNightStop = new Date((EveningNightStop_Time2.state as DateTimeType).calendar.timeInMillis)

    if((now.after(morningNightStart) && now.before(morningNightStop)) || 
       (now.after(eveningNightStart) && now.before(eveningNightStop))) {
        logInfo(RFN, "Its Night2: " + now)
        Dawn2.sendCommand(OFF)
        Day2.sendCommand(OFF)
        Dusk2.sendCommand(OFF)
        Night2.sendCommand(ON)
        TimePeriodOfDay2.postUpdate("Night2")
    }
end

rule "Sunrise started 2"
when
    Item SunriseStart_Event2 received update ON
then
    val now = new Date()
    logInfo(RFN, "Its Sunrise2 : " + now)
end

rule "Sunset started 2"
when
    Item SunsetStart_Event2 received update ON
then
    val now = new Date()
    logInfo(RFN, "Its Sunset2 : " + now)
end

The first One is;


val String filename = "XIAOMI MOTION SENSOR.rules"
 // logInfo(filename, xxxx )
 



rule " Time -1:00 ispania Led kitchen "
when
  Item CurrentTime  changed or
  Item Motion_Sensor_4_LUMI_Presence changed
then // Day2 ispania 09:40 ON Dusk2 ispania 19:14 ON ( OFF 20:50 Dight2 ispania)
    if ( Day2.state == ON || Dusk2.state == ON ) {
        SLed_white_Kitchen.sendCommand("ON")
        SLed_white_Kitchen.postUpdate("ON")
        logInfo(filename, " ( Day2.state == ON || Dusk2.state == ON ) , SLed_white_Kitchen.sendCommand(ON) " )
    }
end

// Motion Sensor No:1
rule " Motion Sensor  No:1 -1:00 ispania"
when
    Item Motion_Sensor_4_LUMI_Presence  changed
then
    if ( Motion_Sensor_4_LUMI_Presence.state == ON) {
        SLed_white_Kitchen.sendCommand("ON")
        SLed_white_Kitchen.postUpdate("ON")
        logInfo(filename, "( Motion_Sensor_4_LUMI_Presence.state == ON) , SLed_white_Kitchen.sendCommand(ON)" )
    } else if (Night2.state == ON){
        SLed_white_Kitchen.sendCommand("OFF")
        SLed_white_Kitchen.postUpdate("OFF")
        logInfo(filename, "(Night2.state == ON) , SLed_white_Kitchen.sendCommand(OFF)" )
    }
end

The second;


val String filename = "TOST TIMER SWITCH.rules"
// logInfo(filename, xxxx )
var Timer mytimers = null

rule "Tost Power OFF 7:05"
when 
Time cron "0 5 7 ? * * *"
then 
    Switch_Tost.sendCommand(OFF)
    logInfo(filename, "Tost Power OFF 7:05" )
end



rule "Tost Timer 6 Minutes ON"
when
    Item Bathroom_Light changed
then
  if ( Timetost.state == OFF ) {
      Switch_Tost.sendCommand(ON)
    if (mytimers == null) {
      mytimers = createTimer(now.plusSeconds(360)) [|
        postUpdate(Switch_Tost, OFF)
        sendCommand(Switch_Tost, OFF)
        logInfo(filename, "Tost Timer 6 Minutes ON" )
      ]
    }
  }
  else if(mytimers != null) {
    mytimers.cancel()
    mytimers = null
  }
end
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
rule " Time ON 10:46 to 7:05 Tost kitchen  "
when
 Item Bathroom_Light changed
     
then 
    if ( Timetost.state == ON ) {
        Switch_Tost.sendCommand("ON")
        Switch_Tost.postUpdate("ON")
        logInfo(filename, " Time ON 10:46 to 7:05 Tost kitchen  " )
    } 
end
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
rule "Time Tost switch Day || Night2"
when
    Item Bathroom_Light changed 
then      
    val command = if ( Day.state == ON || Night2.state == ON ) "ON" else "OFF"
    Timetost.sendCommand(command)
    logInfo(filename ,"Time Tost switch Day || Night2 - {}", command)
end
// createTimer(now.plusSeconds(360))
// createTimer(now.plusMinutes(6))

Please How to use code fences instead of quotes for posting code.

The Rules above look like a much more drawn out and complicated way to implement Design Pattern: Time Of Day. You could cut a couple of hundred lines of duplicated code if you follow that DP

Thanks.
The first time I couldn’t make it work your rule.
I do not know English.
Try with Google translate.
Can the rule to include ( Quiet Hours)?

A quiet hour is just another time of day. You have a switch for each time of day so just add another one to represent quite time.

Can you please give me an example?
It will help me a lot to understand
how it works
Thanx

You have four examples in your code. I have six examples in the Time of Day DP.

In your code, just create another Switch, call it Quiet and set it just like you set all your other Items. For Time of Day, just define the start time for Quiet Time and add it just like MORNING, DAY, AFTERNOON, etc. Call it QUIET.


val quiet_day_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(9) // 15:00

   vQuiet_day_start_Time.postUpdate(quiet_day_start.toString)

   val quiet_day_end = now.withTimeAtStartOfDay.plusDays(1).minusHours(7) //  17:00

   vQuiet_day_end_Time.postUpdate(quiet_day_end.toString)

switch now {

        case now.isAfter(morning_start)   && now.isBefore(day_start):       curr = "MORNING"      // 06:00

        case now.isAfter(day_start)       && now.isBefore(afternoon_start): curr = "DAY"          // 07:09

        case now.isAfter(afternoon_start) && now.isBefore(evening_start):   curr = "AFTERNOON"    // 17:33

        case now.isAfter(evening_start)   && now.isBefore(night_start):     curr = "EVENING"      // 16:28

        case now.isAfter(night_start):                                      curr = "NIGHT"        // 23:00

        case now.isAfter(bed_start)       && now.isBefore(morning_start):   curr = "BED"          // 00:00

        case now.isAfter(quiet_day_start) && now.isBefore(day_start):       curr = "QUIET_START"  // 15:00

        case now.isAfter(quiet_day_end)   && now.isBefore(day_start):       curr = "QUIET_END"    // 17:00

  }

I think so that I did .
I have a question ?
how should that be

case now.isAfter(quiet_day_start):       curr = "QUIET_START"  // 15:00
case now.isAfter(quiet_day_start) && now.isBefore(day_start):       curr = "QUIET_START"  // 15:00


 case now.isAfter(quiet_day_end):       curr = "QUIET_END"    // 17:00 
 case now.isAfter(quiet_day_end)   && now.isBefore(day_start):       curr = "QUIET_END"    // 17:00

thank you

maybe this ?



var curr = "UNKNOWN"

  switch now {

        case now.isAfter(morning_start)   && now.isBefore(day_start):           curr = "MORNING"      // 06:00

        case now.isAfter(day_start)       && now.isBefore(afternoon_start):     curr = "DAY"          // 07:09

        case now.isAfter(quiet_day_start) && now.isBefore(quiet_day_end):       curr = "QUIET_START"  // 15:00

        case now.isAfter(quiet_day_end)   && now.isBefore(afternoon_start):     curr = "QUIET_END"    // 17:00

        case now.isAfter(afternoon_start) && now.isBefore(evening_start):       curr = "AFTERNOON"    // 17:33

        case now.isAfter(evening_start)   && now.isBefore(night_start):         curr = "EVENING"      // 16:28

        case now.isAfter(night_start):                                          curr = "NIGHT"        // 23:00

        case now.isAfter(bed_start)       && now.isBefore(morning_start):       curr = "BED"          // 00:00

  }

Assuming your comments are correct regarding when the time starts than yes, this looks OK. The important thing is to list the cases in chronological order.

here it is all
it is οκ ?

val logName = "Calc tod-state" 

rule "Calculate time of day state"

when

  System started or // run at system start in case the time changed when OH was offline

  Channel 'astro:sun:home:rise#event'    triggered START or

  Channel 'astro:sun:home:set#event'     triggered START or

  Channel 'astro:sun:minus90:set#event'  triggered START or

  Time cron "0 1 0 * * ? *" or // one minute after midnight so give Astro time to calculate the new day's times

  Time cron "0 0 6 * * ? *" or

  Time cron "0 0 23 * * ? *"

then

  logInfo(logName, "Calculating time of day...")

  // Calculate the times for the static tods and populate the associated Items

  // Update when changing static times

  // Jump to tomorrow and subtract to avoid problems at the change over to/from DST

  // val morning_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(13) 11:00

  // vMorning_Time.postUpdate(morning_start.toString)

   val quiet_day_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(9) // 15:00

   vQuiet_day_start_Time.postUpdate(quiet_day_start.toString)

   val quiet_day_end = now.withTimeAtStartOfDay.plusDays(1).minusHours(7) //  17:00

   vQuiet_day_end_Time.postUpdate(quiet_day_end.toString)

  val morning_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(18) // 6:00

  vMorning_Time.postUpdate(morning_start.toString)

  val night_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(1) // 23:00

  vNight_Time.postUpdate(night_start.toString)

  val bed_start = now.withTimeAtStartOfDay // 00:00

  vBed_Time.postUpdate(bed_start.toString)

  // Convert the Astro Items to Joda DateTime

  val day_start = new DateTime(vSunrise_Time.state.toString)

  val evening_start = new DateTime(vSunset_Time.state.toString)

  val afternoon_start = new DateTime(vEvening_Time.state.toString)

  // Calculate the current time of day

  var curr = "UNKNOWN"

  switch now {
        case now.isAfter(morning_start)   && now.isBefore(day_start):           curr = "MORNING"      // 06:00
        case now.isAfter(day_start)       && now.isBefore(afternoon_start):     curr = "DAY"          // 07:09
        case now.isAfter(quiet_day_start) && now.isBefore(evening_start):       curr = "QUIET_START"  // 15:00
        case now.isAfter(evening_start)   && now.isBefore(quiet_day_end):       curr = "EVENING"      // 16:28
        case now.isAfter(quiet_day_end)   && now.isBefore(afternoon_start):     curr = "QUIET_END"    // 17:00
        case now.isAfter(afternoon_start) && now.isBefore(evening_start):       curr = "AFTERNOON"    // 17:33
        case now.isAfter(evening_start)   && now.isBefore(night_start):         curr = "EVENING"      // 16:28
        case now.isAfter(night_start):                                          curr = "NIGHT"        // 23:00
        case now.isAfter(bed_start)       && now.isBefore(morning_start):       curr = "BED"          // 00:00
  }

  // Publish the current state

  logInfo(logName, "Calculated time of day is " + curr)

  vTimeOfDay.sendCommand(curr)

end

thank you

I don’t know, does it work? Do you see errors in openhab.log when the .rules file loads?

What I do see missing are triggers to cause the Rule to run at the start of quiet time and quiet end time.

it’s not fault openhab.log
you need a switch Time cron
for every hour I want to add ?

If it’s a fixed time of day than yes, use Time cron. If it’s based on an event like Astro times, you need to trigger on those events.

1 Like

Thank you very much !!!

hello
because it does this
after restarting


2020-01-29 19:23:01.017 [INFO ] [rthome.model.script.Calc tod-state 2] - Calculated time of day 2 is DAY1900

2020-01-29 19:23:01.019 [WARN ] [rthome.model.script.actions.BusEvent] - Cannot convert 'DAY1900' to a command type which item 'vTimeOfDay2' accepts: [OnOffType, RefreshType].

The error is pretty clear. vTimeOfDay2 is a Switch Item that only accepts ON or OFF as a command. You are trying to send it the command “DAY1900”. That’s not a valid command for a Switch Item. You either need to change the command to ON or OFF or change the Item Type to be a String.

I’ve made some changes
to the rule
so I have switch every 30 minutes
but I can not
which is my mistake.

items



DateTime    TimeRuleTriger      " %1$tA %1$tB  %1$td %1$tY]"    <calendar>                  { channel="ntp:ntp:local:dateTime" }


Group       vTimeOfDay          "TIME OF DAY 1.rules"                                       
Group       vTimeOfDay2         "TIME OF DAY 2.rules"                                       
                                                    
Switch      NightState          "Night"                         <time>                      
Switch      NightTime           "Night Started"                 <time>      (vTimeOfDay)    
Switch      Time_06_00_TO_12_00 "06:00 >>> 12:00"               <time>      (vTimeOfDay2)   
Switch      Time_07_00_TO_11_00 "07:00 >>> 11:00"               <time>      (vTimeOfDay2)   
Switch      Time_09_00_TO_20_30 "09:00 >>> 20:30"               <time>      (vTimeOfDay2)   
Switch      Time_09_30_TO_21_30 "09:30 >>> 21:30"               <time>      (vTimeOfDay2)   
Switch      Time_12_00_TO_07_00 "12:00 >>> 07:00"               <time>      (vTimeOfDay)    
Switch      Time_12_30_TO_05_30 "12:30 >>> 05:30"               <time>      (vTimeOfDay2)   
Switch      Time_14_30_TO_17_30 "14:30 >>> 17:30"               <time>      (vTimeOfDay2)   
Switch      Time_23_00_TO_09_00 "23:00 >>> 09:00"               <time>      (vTimeOfDay2)   
Switch      Time_23_00_TO_09_30 "23:00 >>> 09:30"               <time>      (vTimeOfDay2)   
Switch      Time_23_30_TO_08_30 "23:30 >>> 08:30"               <time>      (vTimeOfDay2)   

// 2 ΩΡΕΣ
Switch      Time_00_00_TO_02_00 "00:00 >>> 02:00"               <time>      (vTimeOfDay2)   
Switch      Time_02_00_TO_04_00 "02:00 >>> 04:00"               <time>      (vTimeOfDay2)   
Switch      Time_04_00_TO_06_00 "04:00 >>> 06:00"               <time>      (vTimeOfDay2)   
Switch      Time_06_00_TO_08_00 "06:00 >>> 08:00"               <time>      (vTimeOfDay2)   
Switch      Time_08_00_TO_10_00 "08:00 >>> 10:00"               <time>      (vTimeOfDay2)   
Switch      Time_10_00_TO_12_00 "10:00 >>> 12:00"               <time>      (vTimeOfDay2)   
Switch      Time_12_00_TO_14_00 "12:00 >>> 14:00"               <time>      (vTimeOfDay2)   
Switch      Time_14_00_TO_16_00 "14:00 >>> 16:00"               <time>      (vTimeOfDay2)   
Switch      Time_16_00_TO_18_00 "16:00 >>> 18:00"               <time>      (vTimeOfDay2)   
Switch      Time_18_00_TO_20_00 "18:00 >>> 20:00"               <time>      (vTimeOfDay2)   
Switch      Time_20_00_TO_22_00 "20:00 >>> 22:00"               <time>      (vTimeOfDay2)   
Switch      Time_22_00_TO_00_00 "22:00 >>> 00:00"               <time>      (vTimeOfDay2)   

// 1 ΩΡΑ
Switch      Time_00_00_TO_01_00 "00:00 >>> 01:00"               <time>      (VtimeOfDay2)   
Switch      Time_01_00_TO_02_00 "01:00 >>> 02:00"               <time>      (VtimeOfDay2)   
Switch      Time_02_00_TO_03_00 "02:00 >>> 03:00"               <time>      (VtimeOfDay2)   
Switch      Time_03_00_TO_04_00 "03:00 >>> 04:00"               <time>      (VtimeOfDay2)   
Switch      Time_04_00_TO_05_00 "04:00 >>> 05:00"               <time>      (VtimeOfDay2)   
Switch      Time_05_00_TO_06_00 "05:00 >>> 06:00"               <time>      (VtimeOfDay2)   
Switch      Time_06_00_TO_07_00 "06:00 >>> 07:00"               <time>      (VtimeOfDay2)   
Switch      Time_07_00_TO_08_00 "07:00 >>> 08:00"               <time>      (VtimeOfDay2)   
Switch      Time_08_00_TO_09_00 "08:00 >>> 09:00"               <time>      (VtimeOfDay2)   
Switch      Time_09_00_TO_10_00 "09:00 >>> 10:00"               <time>      (VtimeOfDay2)   
Switch      Time_10_00_TO_11_00 "10:00 >>> 11:00"               <time>      (VtimeOfDay2)   
Switch      Time_11_00_TO_12_00 "11:00 >>> 12:00"               <time>      (VtimeOfDay2)   
Switch      Time_12_00_TO_13_00 "12:00 >>> 13:00"               <time>      (VtimeOfDay2)   
Switch      Time_13_00_TO_14_00 "13:00 >>> 14:00"               <time>      (VtimeOfDay2)   
Switch      Time_14_00_TO_15_00 "14:00 >>> 15:00"               <time>      (VtimeOfDay2)   
Switch      Time_15_00_TO_16_00 "15:00 >>> 16:00"               <time>      (VtimeOfDay2)   
Switch      Time_16_00_TO_17_00 "16:00 >>> 17:00"               <time>      (VtimeOfDay2)   
Switch      Time_17_00_TO_18_00 "17:00 >>> 18:00"               <time>      (VtimeOfDay2)   
Switch      Time_18_00_TO_19_00 "18:00 >>> 19:00"               <time>      (VtimeOfDay2)   
Switch      Time_19_00_TO_20_00 "19:00 >>> 20:00"               <time>      (VtimeOfDay2)   
Switch      Time_20_00_TO_21_00 "20:00 >>> 21:00"               <time>      (VtimeOfDay2)   
Switch      Time_21_00_TO_22_00 "21:00 >>> 22:00"               <time>      (VtimeOfDay2)   
Switch      Time_22_00_TO_23_00 "22:00 >>> 23:00"               <time>      (VtimeOfDay2)   
Switch      Time_23_00_TO_00_00 "23:00 >>> 00:00"               <time>      (VtimeOfDay2)   

// 30 ΛΕΠΤΑ
Switch      Time_00_00          "00:00"                         <time>      (VtimeOfDay2)   
Switch      Time_00_30          "00:30"                         <time>      (VtimeOfDay2)   
Switch      Time_01_00          "01:00"                         <time>      (VtimeOfDay2)   
Switch      Time_01_30          "01:30"                         <time>      (VtimeOfDay2) 
Switch      Time_02_00          "02:00"                         <time>      (VtimeOfDay2)   
Switch      Time_02_30          "02:30"                         <time>      (VtimeOfDay2) 
Switch      Time_03_00          "03:00"                         <time>      (VtimeOfDay2)   
Switch      Time_03_30          "03:30"                         <time>      (VtimeOfDay2) 
Switch      Time_04_00          "04:00"                         <time>      (VtimeOfDay2)   
Switch      Time_04_30          "04:30"                         <time>      (VtimeOfDay2) 
Switch      Time_05_00          "05:00"                         <time>      (VtimeOfDay2)   
Switch      Time_05_30          "05:30"                         <time>      (VtimeOfDay2) 
Switch      Time_06_00          "06:00"                         <time>      (VtimeOfDay2)   
Switch      Time_06_30          "06:30"                         <time>      (VtimeOfDay2) 
Switch      Time_07_00          "07:00"                         <time>      (VtimeOfDay2)   
Switch      Time_07_30          "07:30"                         <time>      (VtimeOfDay2) 
Switch      Time_08_00          "08:00"                         <time>      (VtimeOfDay2)   
Switch      Time_08_30          "08:30"                         <time>      (VtimeOfDay2) 
Switch      Time_09_00          "09:00"                         <time>      (VtimeOfDay2)   
Switch      Time_09_30          "09:30"                         <time>      (VtimeOfDay2) 
Switch      Time_10_00          "10:00"                         <time>      (VtimeOfDay2)   
Switch      Time_10_30          "10:30"                         <time>      (VtimeOfDay2) 
Switch      Time_11_00          "11:00"                         <time>      (VtimeOfDay2)   
Switch      Time_11_30          "11:30"                         <time>      (VtimeOfDay2) 
Switch      Time_12_00          "12:00"                         <time>      (VtimeOfDay2)   
Switch      Time_12_30          "12:30"                         <time>      (VtimeOfDay2) 
Switch      Time_13_00          "13:00"                         <time>      (VtimeOfDay2)   
Switch      Time_13_30          "13:30"                         <time>      (VtimeOfDay2) 
Switch      Time_14_00          "14:00"                         <time>      (VtimeOfDay2)   
Switch      Time_14_30          "14:30"                         <time>      (VtimeOfDay2) 
Switch      Time_15_00          "15:00"                         <time>      (VtimeOfDay2)   
Switch      Time_15_30          "15:30"                         <time>      (VtimeOfDay2) 
Switch      Time_16_00          "16:00"                         <time>      (VtimeOfDay2)   
Switch      Time_16_30          "16:30"                         <time>      (VtimeOfDay2) 
Switch      Time_17_00          "17:00"                         <time>      (VtimeOfDay2)   
Switch      Time_17_30          "17:30"                         <time>      (VtimeOfDay2) 
Switch      Time_18_00          "18:00"                         <time>      (VtimeOfDay2)   
Switch      Time_18_30          "18:30"                         <time>      (VtimeOfDay2) 
Switch      Time_19_00          "19:00"                         <time>      (VtimeOfDay2)   
Switch      Time_19_30          "19:30"                         <time>      (VtimeOfDay2) 
Switch      Time_20_00          "20:00"                         <time>      (VtimeOfDay2)   
Switch      Time_20_30          "20:30"                         <time>      (VtimeOfDay2) 
Switch      Time_21_00          "21:00"                         <time>      (VtimeOfDay2)   
Switch      Time_21_30          "21:30"                         <time>      (VtimeOfDay2) 
Switch      Time_22_00          "22:00"                         <time>      (VtimeOfDay2)   
Switch      Time_22_30          "22:30"                         <time>      (VtimeOfDay2) 
Switch      Time_23_00          "23:00"                         <time>      (VtimeOfDay2)   
Switch      Time_23_30          "23:30"                         <time>      (VtimeOfDay2) 

TimeOfDay2.rules


val logName = "Calc tod-state 2"

rule "Calculate time of day2 state"

 

when

   System started or

   Time cron "0 0,30 * ? * * *" or

   Item TimeRuleTriger received update or

   Channel 'astro:sun:home:rise#event'    triggered START 

then

   logInfo(logName, "Calculating time of day2...")

   val day00_00_start = now.withTimeAtStartOfDay                                                       // 00:00

   vDay00_00_start_Time.postUpdate(day00_00_start.toString)

   val day00_30_start = now.withTimeAtStartOfDay.plusMinutes(30)                                       // 00:30

   vDay00_30_start_Time.postUpdate(day00_30_start.toString)

   val day01_00_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(23)                            // 01:00

   vDay01_00_start_Time.postUpdate(day01_00_start.toString)

   val day01_30_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(23).plusMinutes(30)           // 01:30

   vDay01_30_start_Time.postUpdate(day01_30_start.toString)

   val day02_00_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(22)                           // 02:00

   vDay02_00_start_Time.postUpdate(day02_00_start.toString)

   val day02_30_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(22).plusMinutes(30)           // 02:30

   vDay02_30_start_Time.postUpdate(day02_30_start.toString)

   val day03_00_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(21)                           // 03:00

   vDay03_00_start_Time.postUpdate(day03_00_start.toString)

   val day03_30_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(21).plusMinutes(30)           // 03:30

   vDay03_30_start_Time.postUpdate(day03_30_start.toString)

   val day04_00_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(20)                           // 04:00

   vDay04_00_start_Time.postUpdate(day04_00_start.toString)

   val day04_30_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(20).plusMinutes(30)          // 04:30

   vDay04_30_start_Time.postUpdate(day04_30_start.toString)

   val day05_00_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(19)                            // 05:00

   vDay05_00_start_Time.postUpdate(day05_00_start.toString)

   val day05_30_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(19).plusMinutes(30)            // 05:30

   vDay05_30_start_Time.postUpdate(day05_30_start.toString)

   val day06_00_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(18)                            // 06:00

   vDay06_00_start_Time.postUpdate(day06_00_start.toString)

   val day06_30_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(18).plusMinutes(30)            // 06:30

   vDay06_30_start_Time.postUpdate(day06_30_start.toString)

   val day07_00_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(17)                            // 07:00

   vDay07_00_start_Time.postUpdate(day07_00_start.toString)

   val day07_30_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(17).plusMinutes(30)            // 07:30

   vDay07_30_start_Time.postUpdate(day07_30_start.toString)

   val day08_00_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(16)                             // 08:00

   vDay08_00_start_Time.postUpdate(day08_00_start.toString)

   val day08_30_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(16).plusMinutes(30)             // 08:30

   vDay08_30_start_Time.postUpdate(day08_30_start.toString)

   val day09_00_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(15)                             // 09:00

   vDay09_00_start_Time.postUpdate(day09_00_start.toString)

   val day09_30_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(15).plusMinutes(30)              // 09:30

   vDay09_30_start_Time.postUpdate(day09_30_start.toString)

   val day10_00_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(14)                              // 10:00

   vDay10_00_start_Time.postUpdate(day10_00_start.toString)

   val day10_30_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(14).plusMinutes(30)               // 10:30

   vDay10_30_start_Time.postUpdate(day10_30_start.toString)

   val day11_00_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(13)                             // 11:00

   vDay11_00_start_Time.postUpdate(day11_00_start.toString)

   val day11_30_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(13).plusMinutes(30)               // 11:30

   vDay11_30_start_Time.postUpdate(day11_30_start.toString)

   val day12_00_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(12)                                // 12:00

   vDay12_00_start_Time.postUpdate(day12_00_start.toString)

   val day12_30_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(12).plusMinutes(30)             // 12:30

   vDay12_30_start_Time.postUpdate(day12_30_start.toString)

   val day13_00_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(11)                               // 13:00

   vDay13_00_start_Time.postUpdate(day13_00_start.toString)

   val day13_30_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(11).plusMinutes(30)               // 13:30

   vDay13_30_start_Time.postUpdate(day13_30_start.toString)

   val day14_00_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(10)                               // 14:00

   vDay14_00_start_Time.postUpdate(day14_00_start.toString)

   val day14_30_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(10).plusMinutes(30)               // 14:30

   vDay14_30_start_Time.postUpdate(day14_30_start.toString)

   val day15_00_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(9)                                // 15:00

   vDay15_00_start_Time.postUpdate(day15_00_start.toString)

   val day15_30_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(9).plusMinutes(30)                // 15:30

   vDay15_30_start_Time.postUpdate(day15_30_start.toString)

   val day16_00_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(8)                                // 16:00

   vDay16_00_start_Time.postUpdate(day16_00_start.toString)

   val day16_30_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(8).plusMinutes(30)                // 16:30

   vDay16_30_start_Time.postUpdate(day16_30_start.toString)

   val day17_00_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(7)                                // 17:00

   vDay17_00_start_Time.postUpdate(day17_00_start.toString)

   val day17_30_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(7).plusMinutes(30)                // 17:30

   vDay17_30_start_Time.postUpdate(day17_30_start.toString)

   val day18_00_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(6)                                // 18:00

   vDay18_00_start_Time.postUpdate(day18_00_start.toString)

   val day18_30_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(6).plusMinutes(30)                // 18:30

   vDay18_30_start_Time.postUpdate(day18_30_start.toString)

   val day19_00_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(5)                                 // 19:00

   vDay19_00_start_Time.postUpdate(day19_00_start.toString)

   val day19_30_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(5).plusMinutes(30)                 // 19:30

   vDay19_30_start_Time.postUpdate(day19_30_start.toString)

   val day20_00_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(4)                                  // 20:00

   vDay20_00_start_Time.postUpdate(day20_00_start.toString)

   val day20_30_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(4).plusMinutes(30)                   // 20:30

   vDay20_30_start_Time.postUpdate(day20_30_start.toString)

   val day21_00_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(3)                                   // 21:00

   vDay21_00_start_Time.postUpdate(day21_00_start.toString)

   val day21_30_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(3).plusMinutes(30)                   // 21:30

   vDay21_30_start_Time.postUpdate(day21_30_start.toString)

   val day22_00_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(2)                                   // 22:00

   vDay22_00_start_Time.postUpdate(day22_00_start.toString)

   val day22_30_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(2).plusMinutes(30)                   // 22:30

   vDay22_30_start_Time.postUpdate(day22_30_start.toString)

   val day23_00_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(1)                                   // 23:00

   vDay23_00_start_Time.postUpdate(day23_00_start.toString)

  

   val day23_30_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(1).plusMinutes(30)                   // 23:30

   vDay23_30_start_Time.postUpdate(day23_30_start.toString)

  // Convert the Astro Items to Joda DateTime

  // Calculate the current time of day

  var curr = "UNKNOWN"

  switch now {

        case now.isAfter(day00_00_start)      &&  now.isBefore(day00_30_start):           curr = "DAY0000" // 00:00

        case now.isAfter(day00_30_start)      &&  now.isBefore(day01_00_start):           curr = "DAY0030" // 00:30

        case now.isAfter(day01_00_start)      &&  now.isBefore(day01_30_start):           curr = "DAY0100" // 01:00

        case now.isAfter(day01_30_start)      &&  now.isBefore(day02_00_start):           curr = "DAY0130" // 01:30

        case now.isAfter(day02_00_start)      &&  now.isBefore(day02_30_start):           curr = "DAY0200" // 02:00

        case now.isAfter(day02_30_start)      &&  now.isBefore(day03_00_start):           curr = "DAY0230" // 02:30

        case now.isAfter(day03_00_start)      &&  now.isBefore(day03_30_start):           curr = "DAY0300" // 03:00

        case now.isAfter(day03_30_start)      &&  now.isBefore(day04_00_start):           curr = "DAY0330" // 03:30

        case now.isAfter(day04_00_start)      &&  now.isBefore(day04_30_start):           curr = "DAY0400" // 04:00

        case now.isAfter(day04_30_start)      &&  now.isBefore(day05_00_start):           curr = "DAY0430" // 04:30

        case now.isAfter(day05_00_start)      &&  now.isBefore(day05_30_start):           curr = "DAY0500" // 05:00

        case now.isAfter(day05_30_start)      &&  now.isBefore(day06_00_start):           curr = "DAY0530" // 05:30

        case now.isAfter(day06_00_start)      &&  now.isBefore(day06_30_start):           curr = "DAY0600" // 06:00

        case now.isAfter(day06_30_start)      &&  now.isBefore(day07_00_start):           curr = "DAY0630" // 06:30

        case now.isAfter(day07_00_start)      &&  now.isBefore(day07_30_start):           curr = "DAY0700" // 07:00

        case now.isAfter(day07_30_start)      &&  now.isBefore(day08_00_start):           curr = "DAY0730" // 07:30

        case now.isAfter(day08_00_start)      &&  now.isBefore(day08_30_start):           curr = "DAY0800" // 08:00

        case now.isAfter(day08_30_start)      &&  now.isBefore(day09_00_start):           curr = "DAY0830" // 08:30

        case now.isAfter(day09_00_start)      &&  now.isBefore(day09_30_start):           curr = "DAY0900" // 09:00

        case now.isAfter(day09_30_start)      &&  now.isBefore(day10_00_start):           curr = "DAY0930" // 09:30

        case now.isAfter(day10_00_start)      &&  now.isBefore(day10_30_start):           curr = "DAY1000" // 10:00

        case now.isAfter(day10_30_start)      &&  now.isBefore(day11_00_start):           curr = "DAY1030" // 10:30

        case now.isAfter(day11_00_start)      &&  now.isBefore(day11_30_start):           curr = "DAY1100" // 11:00

        case now.isAfter(day11_30_start)      &&  now.isBefore(day12_00_start):           curr = "DAY1130" // 11:30

        case now.isAfter(day12_00_start)      &&  now.isBefore(day12_30_start):           curr = "DAY1200" // 12:00

        case now.isAfter(day12_30_start)      &&  now.isBefore(day13_00_start):           curr = "DAY1230" // 12:30

        case now.isAfter(day13_00_start)      &&  now.isBefore(day13_30_start):           curr = "DAY1300" // 13:00

        case now.isAfter(day13_30_start)      &&  now.isBefore(day14_00_start):           curr = "DAY1330" // 13:30

        case now.isAfter(day14_00_start)      &&  now.isBefore(day14_30_start):           curr = "DAY1400" // 14:00

        case now.isAfter(day14_30_start)      &&  now.isBefore(day15_00_start):           curr = "DAY1430" // 14:30

        case now.isAfter(day15_00_start)      &&  now.isBefore(day15_30_start):           curr = "DAY1500" // 15:00

        case now.isAfter(day15_30_start)      &&  now.isBefore(day16_00_start):           curr = "DAY1530" // 15:30

        case now.isAfter(day16_00_start)      &&  now.isBefore(day16_30_start):           curr = "DAY1600" // 16:00

        case now.isAfter(day16_30_start)      &&  now.isBefore(day17_00_start):           curr = "DAY1630" // 16:30

        case now.isAfter(day17_00_start)      &&  now.isBefore(day17_30_start):           curr = "DAY1700" // 17:00

        case now.isAfter(day17_30_start)      &&  now.isBefore(day18_00_start):           curr = "DAY1730" // 17:30

        case now.isAfter(day18_00_start)      &&  now.isBefore(day18_30_start):           curr = "DAY1800" // 18:00

        case now.isAfter(day18_30_start)      &&  now.isBefore(day19_00_start):           curr = "DAY1830" // 18:30

        case now.isAfter(day19_00_start)      &&  now.isBefore(day19_30_start):           curr = "DAY1900" // 19:00

        case now.isAfter(day19_30_start)      &&  now.isBefore(day20_00_start):           curr = "DAY1930" // 19:30

        case now.isAfter(day20_00_start)      &&  now.isBefore(day20_30_start):           curr = "DAY2000" // 20:00

        case now.isAfter(day20_30_start)      &&  now.isBefore(day21_00_start):           curr = "DAY2030" // 20:30

        case now.isAfter(day21_00_start)      &&  now.isBefore(day21_30_start):           curr = "DAY2100" // 21:00

        case now.isAfter(day21_30_start)      &&  now.isBefore(day22_00_start):           curr = "DAY2130" // 21:30

        case now.isAfter(day22_00_start)      &&  now.isBefore(day22_30_start):           curr = "DAY2200" // 22:00

        case now.isAfter(day22_30_start)      &&  now.isBefore(day23_00_start):           curr = "DAY2230" // 22:30

        case now.isAfter(day23_00_start)      &&  now.isBefore(day23_30_start):           curr = "DAY2300" // 23:00

        case now.isAfter(day23_30_start)      &&  now.isBefore(day00_00_start):           curr = "DAY2330" // 23:30

  }

  // Publish the current state

  logInfo(logName, "Calculated time of day 2 is " + curr)

  vTimeOfDay2.sendCommand(curr)

end