After 133 postings to this thread, something like this probably should be posted into its own thread so it can be found by other readers of the forum easier. Answers to questions like these are particularly helpful.
Question: How does the motion sensor behave? By this I mean does it send an ON when motion is triggered and then send an off after a certain amount of time, just sends ON when motion is detected with no OFF, send ON immediately followed by OFF?
This is important because how the motion sensor behaves will greatly impact how the rule you want need to be written.
Let’s assume that the motion sensor either only sends ON commands or stays ON for a shorter or longer period of time than we want the light to stay on. This should cover most of the motion sensor behaviors listed above.
NOTE: I’ve made a number of other simplifications to your code below. Ask if you have any questions.
var Timer timer = null
rule "BALightOn"
when
Item BA_HUE_Motion received command ON // only trigger when motion is detected, we don't care when the motion sensor goes OFF
then
logInfo("BALightOn", "Motion detected in Bathroom")
val t6a = now.withTimeAtStartOfDay.plusHours(6) // better way to get to something to compare to now
val t11p = now.withTimeAtStartOfDay.plusHours(23) // better way to get to something to compare to now
// Calculate brightness as a separate step to make the logic simpler
var brightness = 15
if(now.isBefore(t11p) && now.isAfter(t6a) brightness = 100
logInfo("BALightOn", "Turning bathroom light to " + brightness)
bathroom_light.sendCommand(brightness)
// create a timer to turn off the light if one doesn't already exist
if(timer == null){
timer.createTimer(now.plusMinutes(3), [|
logInfo("BALightOn", "Turning bathroom light off") // if you log the ON you should log the OFF
bathroom_light.sendCommand(OFF)
timer = null
]
}
// if there is a timer, reschedule it for another 3 minutes
else {
timer.reschedule(now.plusMinutes(3))
}
end
However, this rule can be made even simpler using the Expire binding.
Item
bathroom_light "label" { hue="...", expire="3m,command=0" }
Rule
rule "BALightOn"
when
Item BA_HUE_Motion received command ON // only trigger when motion is detected, we don't care when the motion sensor goes OFF
then
logInfo("BALightOn", "Motion detected in Bathroom")
val t6a = now.withTimeAtStartOfDay.plusHours(6)
val t11p = now.withTimeAtStartOfDay.plusHours(23)
var brightness = 15
if(now.isBefore(t11p) && now.isAfter(t6a) brightness = 100
logInfo("BALightOn", "Turning bathroom light to " + brightness)
bathroom_light.sendCommand(brightness)
end
The expire binding config above will wait for three minutes after the Item is set to something and then sendCommand(0) to the light after the last update to the Light is received. The rule will sendCommand the brightness every time the motion sensor is triggered, rescheduling the expire binding.
The Rule above this does the exact same thing using Timers.
See also:
- Motion Sensor Timer for a more detailed explanation of the code above.
- Time of Day for a better way to manage time periods in your home automation so you don’t end up with checks to see if it is between certain time periods scattered all over your rules.