Advanced motion sensor rule

Hi

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

What did the log show as an error?

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?

Tip: check out the expire binding. Makes life with timers easier.

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.:roll_eyes:
I also noticed that @vzorglub is awake, if he even sleeps.:rofl: He’s good with rules and can probably do the whole thing before I can finish this reply.

On my phone. So I can’t do rules right now. I’ll be home later.

I knew you didn’t sleep!:wink: Thanks for jumping in.

I’m thinking I can change in item in Basic UI, and this item depending on the state makes changes to the rule.

So let’s hope @vzorglub can help :smiley:, I can definitely see he is a fast writer, as he did reply before I did :+1:

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.

Yes that is what I was thinking, I do have some items with this selection function in my installation, and works perfectly :sunglasses:

I just can’t see how I build the rule with the sensor_mode

Ok,
Rule tidied up…
Without the sensor mode yet:

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
1 Like

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?

Hi

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.

Do you have an item for the switch or the light to turn on?

Yes

Item dimmer stue_ballroom

@zamzon

Rough draft:

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
1 Like

Thanks,
really god work, I will test it in my installation.