I need some help building a bit Advanced rule for my motion sensor
Info on the sensor:
When the sensor see something it goes ON and after 10sec, it goes OFF again, and then ready to be triggered again.
Items the rule needs to work with:
A number item (lightlevel)to set the lightlevel so the light level is low enough
A number item (sensor_timeout) to set the sensor timeout
A number item (sensor_mode)to tell if the sensor should 1= auto turn on/off, 2=Manuel on / auto off, 3=manuel on/off (sensor disable)
So when the sensor see gets triggered it needs to see what state the sensor_mode is in, and if the light needs to turn on it should check item lightlevel if the light in the room is low enough to turn on.
and then create a timer with the sensor_timeout item.
I did try to make something, but I dont know how to insert the sensor timeout and sensor_mode to the rule
PS. the rule dosen’t work now.
//-----------------------------------------------------
//
// Items used in rule:
//
//number lightlevel
//number sensor_timeout
//number sensor_mode 1=auto turn on/off 2=Manuel on/auto off 3=manuel on/off (sensor disable)
//-----------------------------------------------------
var Timer Motion_Timer = null
rule "Hue_Motion_sensor"
when
Item PhilipsSML001_Occupancy changed to ON
then
if (PhilipsSML001_Illuminance.state < lightlevel) //If the light level is under lightlevel item
{
if (Motion_Timer !== null) //sensor time is running
{
logInfo("rules", "Reschedule timer with " + sensor_timeout + " minuts")
Motion_Timer.reschedule(now.plusMinutes(sensor_timeout))
}
else
{
logInfo("rules", "Create timer with " + sensor_timeout + " minuts")
Motion_Timer = createTimer(now.plusMinutes(sensor_timeout))
[|
if (PhilipsSML001_Occupancy.state == ON)
{
logInfo("rules", "Rescheduleing, there is still motion with " + sensor_timeout + " minuts")
Motion_Timer.reschedule(now.plusMinutes(sensor_timeout))
}
else
{
logInfo("rules", "the light is turning off")
Motion_Timer = null
}
]
}
}
end
You need to define sensor_timeout like var int sensor_timeout = 10
You also might want to use var lum = PhililpsSML001_Illiminace.state as number and use the var in the if statement.
to
if(lux < lightlevel)
I also notice your items at the top. Are these created just for the rule or are they actual values coming from a sensor to OH?
All the items are user input from the Basic UI, so if I insert a var into the rule like sensor_timeout can I then edit the timeout in the Basic UI ?
So I need to be able to change all 3 items in Basic UI.
Can you as well help me implement the sensor_mode into the rule?
I don’t think you can change a rule via BasicUI, but I could be wrong. I can try to help but I’m not the best at rules and I type slow.
I also noticed that @vzorglub is awake, if he even sleeps. He’s good with rules and can probably do the whole thing before I can finish this reply.
You may be correct. Take a look at the doc’s here and scroll down to Element type switch and Element type selection. Maybe even get your sensor_mode setup with something similar.
var Timer motionTimer = null
rule "Hue_Motion_sensor"
when
Item PhilipsSML001_Occupancy changed to ON
then
val int sensorTimeout = Integer::parseInt(sensor_timeout.state.toString)
val lightLevel = lightlevel.state as Number
val illuminance = PhilipsSML001_Illuminance.state as Number
if (illuminance < lightLevel) { //If the light level is under lightlevel item
if (motionTimer === null) { //sensor time is not running
logInfo("rules", "Create timer with " + sensor_timeout.state.toString + " minutes")
motionTimer = createTimer(now.plusMinutes(sensorTimeout) , [ |
if (PhilipsSML001_Occupancy.state == ON) {
logInfo("rules", "Rescheduling, there is still motion with " + sensor_timeout.state.toString + " minuts")
motionTimer.reschedule(now.plusMinutes(sensorTimeout))
} else {
logInfo("rules", "the light is turning off")
motionTimer = null
}
])
} else {
logInfo("rules", "Reschedule timer with " + sensor_timeout.state.toString + " minutes")
motionTimer.reschedule(now.plusMinutes(sensorTimeout))
}
}
end
I understand 1=auto on/off - That’s what we have right now
I understand 3=Manual on/off (sensor disabled) - That’s easy
I don’t understand 2=Manual on/Auto off - Could you describe this to me please?
Thanks for helping
Number 2 is ment to work like, you manually need to turn on the light using the wall switch and then the sensor keep the light on, if there is motion and the sensorTimeout run out, the light turns off.
var Timer motionTimer = null
rule "Hue_Motion_sensor"
when
Item PhilipsSML001_Occupancy changed to ON or
Item stue_ballroom changed
then
val int sensorTimeout = Integer::parseInt(sensor_timeout.state.toString)
val lightLevel = lightlevel.state as Number
val illuminance = PhilipsSML001_Illuminance.state as Number
val stueBallroom = stue_ballroom.state as Number
val sensorMode = sensor_mode.state as Number
switch sensorMode {
case 1 : { // Auto turn ON/OFF
if (illuminance < lightLevel) { //If the light level is under lightlevel item
if (motionTimer === null) { //sensor time is not running
stue_ballroom.sendCommand(100) // Turn the light ON
logInfo("rules", "Create timer with " + sensor_timeout.state.toString + " minutes")
motionTimer = createTimer(now.plusMinutes(sensorTimeout) , [ |
if (PhilipsSML001_Occupancy.state == ON) {
logInfo("rules", "Rescheduling, there is still motion with " + sensor_timeout.state.toString + " minuts")
motionTimer.reschedule(now.plusMinutes(sensorTimeout))
} else {
logInfo("rules", "the light is turning off")
stue_ballroom.sendCommand(0) // Turn the light OFF
motionTimer = null
}
])
} else {
logInfo("rules", "Reschedule timer with " + sensor_timeout.state.toString + " minutes")
motionTimer.reschedule(now.plusMinutes(sensorTimeout))
}
}
}
case 2 : { // Manual ON / Auto OFF
if (stueBallroom > 0) {
if (motiomTimer === null) {
logInfo("rules", "Create timer with " + sensor_timeout.state.toString + " minutes")
motionTimer = createTimer(now.plusMinutes(sensorTimeout) , [ |
if (PhilipsSML001_Occupancy.state == ON) {
logInfo("rules", "Rescheduling, there is still motion with " + sensor_timeout.state.toString + " minuts")
motionTimer.reschedule(now.plusMinutes(sensorTimeout))
} else {
logInfo("rules", "the light is turning off")
stue_ballroom.sendCommand(0) // Turn the light OFF
motionTimer = null
}
])
} else {
logInfo("rules", "Reschedule timer with " + sensor_timeout.state.toString + " minutes")
motionTimer.reschedule(now.plusMinutes(sensorTimeout))
}
} else {
return; // Light is OFF - Do nothing
}
}
case 3 : { // Manual ON/OFF - Sensor disabled
return; // Do nothing
}
}
end