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

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))