Like most I have a number of Motion Sensors placed around my home and use them to trigger events. One of the more active area’s is my Kitchen, especially first thing in the morning.
At present when the Motion sensor is activated I then look for a number of conditions. For example, when the Lux level is low I switch on cabinet lights, but also check to see what time the motion sensor is activated and if it’s between 7am and 8am the Sonos starts playing the local radio station. My current rule has a number of if statements for each condition and action required. I wonder if creating a rule for each condition is a better way and have less if statements. Just looking for peoples opinions. For context here’s my current rule.
rule "KITCHEN: Motion Sensor Activation"
when
Item Kitchen_SP3102_vMotion changed
then
val vToday = Today.state.format("%1$tA")
if(Kitchen_SP3102_vMotion.state == ON) {
if(Door_Sensor_Timer.state != OFF) Door_Sensor_Timer.sendCommand(OFF)
if(Light_Level_Status.state < 6) Kitchen_Lightstrip_vSwitch.sendCommand(ON)
if(now.getHour() > 6 && now.getHour() < 9 && Study_ZW089_vDoor.state == CLOSED) {
Announcement_Location.sendCommand(1)
Thread::sleep(1000)
if(vToday == "Monday" && Sarah_Holiday.state == ON) {
if(Sarah_Holiday_Announced.state != ON) Announcement_Type.sendCommand(8)
}
// if(date.getMonth() == 7 && date.getDay() == 25) Announcement_Type.sendCommand(23)
if(Kitchen_Sonos_Stop.state == ON && Kitchen_Morning_Announcement.state == OFF) {
Landing_Blind_vSwitch.sendCommand(OFF)
Kitchen_Morning_Announcement.sendCommand(ON)
Kitchen_Sonos_Volume.sendCommand(15)
Thread::sleep(1000)
Announcement_Type.sendCommand(22)
Thread::sleep(5000)
createTimer(now.plusSeconds(5)) [|
Kitchen_Sonos_Volume.sendCommand(20)
Kitchen_Sonos_Tuneinstationid.sendCommand("6894")
Kitchen_Sonos_Control.sendCommand(PLAY)
createTimer(now.plusSeconds(30)) [|
Announcement_Type.sendCommand(9)
]
]
}
if(Announcement_Location.state == 1 && Car_Mini_Range_Announced.state == OFF) {
createTimer(now.plusSeconds(30)) [|
Announcement_Type.sendCommand(6)
logInfo(logName, "System Message: Announce remaining range in the Mini.")
logInfo(logName, "Garage: The Mini has a range of {} Miles.", Car_Mini_RangeMiles.state)
]
}
}
} else if(Kitchen_SP3102_vMotion.state == OFF && Kitchen_Lightstrip_vSwitch.state == ON) {
var int vTimeout = (new java.util.Random).nextInt(60)
vTimeout = vTimeout + 60
logInfo(logName, "System Message: Kitchen Lightstrip count down timer set to {} seconds.", vTimeout)
createTimer(now.plusSeconds(vTimeout)) [|
if(now.getHour() > 7 && Kitchen_SP3102_vMotion.state == OFF) Kitchen_Lightstrip_vSwitch.sendCommand(OFF);
]
}
end