Just to re-re-revisit this, I’m still getting the Cannot cast org.openhab.core.types.UnDefType to org.openhab.core.library.types.DateTimeType
error.
This occurs frequently and is NOT just at startup. It seems to be when it’s trying to recalc time related ‘things’. Designer shows no conflicts…
My rules are below;
import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import org.joda.time.*
import org.java.math.*
var Timer tDownstairsLights
var Timer tUpstairsLights
var Timer tLivingRoomLight
var Timer tOnkyoPower
var Timer tSamsungPower
rule "Get time period for right now"
when
System started
then
val morning = now.withTimeAtStartOfDay.plusHours(5).millis // 5 AM
val sunrise = new DateTime((Sunrise_Time.state as DateTimeType).calendar.timeInMillis)
val twilight = new DateTime((Twilight_Event.state as DateTimeType).calendar.timeInMillis)
val night = now.withTimeAtStartOfDay.plusHours(23).millis // 11 PM
if(now.isAfter(morning) && now.isBefore(sunrise)) {
Morning.sendCommand(ON)
Day.sendCommand(OFF)
Twilight.sendCommand(OFF)
Night.sendCommand(OFF)
}
else if(now.isAfter(sunrise) && now.isBefore(twilight)) {
Morning.sendCommand(OFF)
Day.sendCommand(ON)
Twilight.sendCommand(OFF)
Night.sendCommand(OFF)
}
else if(now.isAfter(twilight) && now.isBefore(night)) {
Morning.sendCommand(OFF)
Day.sendCommand(OFF)
Twilight.sendCommand(ON)
Night.sendCommand(OFF)
}
else if(now.isAfter(night) && now.isBefore(morning)) {
Morning.sendCommand(OFF)
Day.sendCommand(OFF)
Twilight.sendCommand(OFF)
Night.sendCommand(ON)
}
end
rule "Morning Started"
when
Time cron "0 0 5 * * ? *"
then
Morning.sendCommand(ON)
Day.sendCommand(OFF)
Twilight.sendCommand(OFF)
Night.sendCommand(OFF)
end
rule "Day"
when
Item Sunrise_Event received update
then
Night.sendCommand(OFF)
Twilight.sendCommand(OFF)
Morning.sendCommand(OFF)
Day.sendCommand(ON)
end
rule "Twilight"
when
Item Twilight_Event received update
then
Night.sendCommand(OFF)
Morning.sendCommand(OFF)
Day.sendCommand(OFF)
Twilight.sendCommand(ON)
end
rule "Night Started"
when
Time cron "0 0 23 * * ? *"
then
Morning.sendCommand(OFF)
Day.sendCommand(OFF)
Twilight.sendCommand(OFF)
Night.sendCommand(ON)
end
rule "Enable Downstairs Hallway Light on Motion"
when
Item Z_Motion_GF_Hallway changed
then
if(Morning.state == ON)
{
sendCommand(Hue_Hallway_Downstairs, ON)
sendCommand(Hue_Hallway_DownstairsDim, 5)
}
else if(Twilight.state == ON)
{
sendCommand(Hue_Hallway_Downstairs, ON)
sendCommand(Hue_Hallway_DownstairsDim, 100)
}
else if(Night.state == ON)
{
sendCommand(Hue_Hallway_Downstairs, ON)
sendCommand(Hue_Hallway_DownstairsDim, 10)
}
end
rule "Enable Upstairs Hallway Light on Motion"
when
Item Z_PIR_FF_Hallway changed
then
if(Morning.state == ON)
{
sendCommand(Hue_Hallway_Upstairs, ON)
sendCommand(Hue_Hallway_UpstairsDim, 5)
}
else if(Twilight.state == ON)
{
sendCommand(Hue_Hallway_Upstairs, ON)
sendCommand(Hue_Hallway_UpstairsDim, 100)
}
else if(Night.state == ON)
{
sendCommand(Hue_Hallway_Upstairs, ON)
sendCommand(Hue_Hallway_UpstairsDim, 10)
}
end
rule "Turn Bathroom Speakers on at 6pm"
when
Time cron "0 0 18 * * ?"
then
sendCommand(Z_Socket_FF_BathroomSpeakers, ON)
end
rule "Turn Kitchen Speakers on at 6pm"
when
Time cron "0 0 13 * * ?"
then
sendCommand(Z_Socket_GF_KitchenSpeakers, ON)
end
rule "Turn Bedroom TV Off at Half past Midnight on a School night"
when
Time cron "0 30 0 ? * MON,TUE,WED,THU,SUN *" // School nights at 00:30 hours
then
sendCommand(LgTvPower, ON)
end
rule "Turn Bedroom TV Off at Half past Midnight at the weekend"
when
Time cron "0 0 1 ? * FRI,SAT" // Weekend 01:30 hours
then
sendCommand(LgTvPower, ON)
end
rule "Turn Bedroom TV down at 11pm"
when
Time cron "0 0 23 * * ?" // Every day at 23:00, turn volume down further
then
sendCommand(LgTvVolume, DECREASE)
sendCommand(LgTvVolume, DECREASE)
end
rule "Turn Bedroom TV down at Midnight"
when
Time is midnight // Every day Midnight, turn volume down even further
then
sendCommand(LgTvVolume, DECREASE)
sendCommand(LgTvVolume, DECREASE)
end
rule "Update max and min temperatures"
when
Item Weather_Temperature changed or
Time cron "0 0 0 * * ?" or
System started
then
postUpdate(Weather_Temp_Max, Weather_Temperature.maximumSince(now.toDateMidnight).state)
postUpdate(Weather_Temp_Min, Weather_Temperature.minimumSince(now.toDateMidnight).state)
logInfo("Weather", "Temperature evolved of " + Weather_Temperature.deltaSince(now.minusMinutes(2)) + " degrees.")
end
rule "Turn living room lights on when it gets dark"
when
Item Twilight_Event received update ON
then
sendCommand(Hue_LivingRoom_TallLamp, ON)
sendCommand(Hue_LivingRoom_TallLampDim, 50)
sendCommand(Hue_LivingRoom_Lamp, ON)
sendCommand(Hue_LivingRoom_LampDim, 50)
sendCommand(Hue_LivingRoom_MainLight, ON)
sendCommand(Hue_LivingRoom_MainLightDim, 50)
end
rule "Turn living room lights up when it gets darker"
when
Item Sunset_Event received update ON
then
sendCommand(Hue_LivingRoom_TallLamp, ON)
sendCommand(Hue_LivingRoom_TallLampDim, 100)
sendCommand(Hue_LivingRoom_Lamp, ON)
sendCommand(Hue_LivingRoom_LampDim, 100)
sendCommand(Hue_LivingRoom_MainLight, ON)
sendCommand(Hue_LivingRoom_MainLightDim, 100)
end
rule "Enable Living Room Main Light on Motion (cat feed)"
when
Item Z_Motion_GF_Hallway changed
then
if(Morning.state == ON)
{
sendCommand(Hue_LivingRoom_MainLightMorning, ON)
sendCommand(Hue_LivingRoom_MainLightMorningDim, 30)
}
end
rule "Turn Living Room Main Light off after time in morning"
when
Item Hue_LivingRoom_MainLightMorning received command
then
if(Twilight.state == ON)
{
if(receivedCommand==ON) {
if(tLivingRoomLight==null) {
// first ON command, so create a timer to turn the light off again
tLivingRoomLight = createTimer(now.plusSeconds(300)) [|
sendCommand(Hue_LivingRoom_MainLightMorning, OFF)
sendMail("email@address.com", "Living Room Light is off (Morning)", "Cat feed light")
]
} else {
// subsequent ON command, so reschedule the existing timer
tLivingRoomLight.reschedule(now.plusSeconds(300))
}
} else if(receivedCommand==OFF) {
// remove any previously scheduled timer
if(tLivingRoomLight!=null) {
tLivingRoomLight.cancel
tLivingRoomLight = null
}
}
}
end
rule "Turn Downstairs Hallway Light off after 5 minutes"
when
Item Hue_Hallway_Downstairs received command
then
if(receivedCommand==ON) {
if(tDownstairsLights==null) {
// first ON command, so create a timer to turn the light off again
tDownstairsLights = createTimer(now.plusSeconds(300)) [|
sendCommand(Hue_Hallway_Downstairs, OFF)
]
} else {
// subsequent ON command, so reschedule the existing timer
tDownstairsLights.reschedule(now.plusSeconds(300))
}
} else if(receivedCommand==OFF) {
// remove any previously scheduled timer
if(tDownstairsLights!=null) {
tDownstairsLights.cancel
tDownstairsLights = null
}
}
end
rule "Turn Upstairs Hallway Light off after 5 minutes"
when
Item Hue_Hallway_Upstairs received command
then
if(receivedCommand==ON) {
if(tUpstairsLights==null) {
// first ON command, so create a timer to turn the light off again
tUpstairsLights = createTimer(now.plusSeconds(300)) [|
sendCommand(Hue_Hallway_Upstairs, OFF)
]
} else {
// subsequent ON command, so reschedule the existing timer
tUpstairsLights.reschedule(now.plusSeconds(300))
}
} else if(receivedCommand==OFF) {
// remove any previously scheduled timer
if(tUpstairsLights!=null) {
tUpstairsLights.cancel
tUpstairsLights = null
}
}
end
rule "Turn things off at 10:30pm daily"
when
Time cron "0 30 22 * * ?"
then
sendCommand(Z_Socket_GF_KitchenSpeakers, OFF)
sendCommand(Z_Socket_FF_BathroomSpeakers, OFF)
end
rule "Turn everything off at Midnight"
when
Time is midnight
then
sendCommand(Z_Socket_GF_KitchenSpeakers, OFF)
sendCommand(Z_Socket_FF_BathroomSpeakers, OFF)
sendCommand(Hue_LivingRoom_Lamp, OFF)
sendCommand(Hue_LivingRoom_TallLamp, OFF)
sendCommand(Hue_LivingRoom_MainLight, OFF)
sendCommand(Z_Socket_FrontBedroom_DAB, OFF)
sendMail("email@address.com", "It's midnight!", "Everything is now off.")
end
rule "Turn Downstairs Lights off 5 minutes after TV goes off"
when
Item SamsungTV received command
then
if(receivedCommand==ON) {
if(tSamsungPower==null) {
tSamsungPower = createTimer(now.plusSeconds(300)) [|
sendCommand(Hue_LivingRoom_MainLight, OFF)
sendCommand(Hue_LivingRoom_Lamp, OFF)
sendCommand(Hue_LivingRoom_TallLamp, OFF)
sendMail("email@address.com", "TV off, Lights off! (1)", "TV off, Lights off! (1)")
]
} else {
tSamsungPower.reschedule(now.plusSeconds(300))
}
} else if(receivedCommand==OFF) {
if(tSamsungPower==null) {
tSamsungPower = createTimer(now.plusSeconds(300)) [|
sendCommand(Hue_LivingRoom_MainLight, OFF)
sendCommand(Hue_LivingRoom_Lamp, OFF)
sendCommand(Hue_LivingRoom_TallLamp, OFF)
sendMail("email@address.com", "TV off, Lights off! (2)", "TV off, Lights off! (2)")
]
} else {
tSamsungPower.reschedule(now.plusSeconds(300))
}
}
end
rule "Turn Front Bedroom Toys on"
when
Item Z_Light_FF_FrontBedroomSwitch received command ON
then
sendCommand(Z_Socket_FrontBedroom_DAB, ON)
sendCommand(Z_Socket_FrontBedroom_Morning, ON)
end
rule "Turn Front Bedroom Toys off"
when
Item Z_Light_FF_FrontBedroomSwitch received command OFF
then
sendCommand(Z_Socket_FrontBedroom_DAB, OFF)
sendCommand(Z_Socket_FrontBedroom_Morning, OFF)
sendMail("email@address.com", "Front Bedroom is OFF!", "Radio is off.")
end