Help With Hue Rules

Hi Everyone im hoping for some help with a couple of rules

rule 1) how do i create a rule that says something like if motion sensor is activated and stays on for say two seconds do something

rule "Motion Detection (FULL)"
when
Item HUE_Motion changed from OFF to ON or //Kitchen Sensor 
Item HUE_Motion2 changed from OFF to ON or //Upstairs Sensor
Item HUE_Motion3 changed from OFF to ON //Front Door Sensor
then
if(Security_System_FULL.state ==ON) {
if(HUE_Motion.state ==ON ) {
sendMail ("abc@live.com;abc2@hotmail.co.uk", "Downstairs Motion Detected!", "Motion has been detected at home OPENHAB needs your attention please visit http://myopenhab.org/login")    
}
if(HUE_Motion2.state ==ON  ) {
sendMail ("abc@live.com;abc2@hotmail.co.uk", "Upstairs Motion Detected!", "Motion has been detected at home OPENHAB needs your attention please visit http://myopenhab.org/login")    
}
if(HUE_Motion3.state ==ON ) {
sendMail ("abc@live.com;abc2@hotmail.co.uk", "Front Door Motion Detected!", "Motion has been detected at home OPENHAB needs your attention please visit http://myopenhab.org/login")    
}    
}
end

is it possible to send a on command to my hue motion sensor so that it triggers the lights associated with it ?

Hi,

add the below line to halt the rule for 2sec and then do your state checks:

if(Security_System_FULL.state ==ON) {
Thread::sleep(2000)
if(HUE_Motion.state ==ON ) {

Note, that the expire binding would work very well.

Not sure I understand the second part of your question though. You mean to trigger lights, based on motion detection?

Kurt

Thanks for the input, I have thought about using sleep or a timer myself but it only checks if it’s still on after the timer or sleep delay not that it’s been continuously on for the period any more suggestions?

The second part was just something I was thinking about doing when away from home, have openhab change the sensor to on to turn the lights on and off when away from home but it would just be simpler to send the commands directly too the lights and just cut out the motion sensor

I’m on my phone and only have short breaks to type so it’s hard to code.

First of all, avoid long sleeps. Anything longer than 500 msec should be avoided. I need to write a post I can link to. This had come up at least three times today. The tl;Dr if there are only 5 threads available for rules to run. When you run out no rules can run until a running rule exits.

The approach to take is to set a timer when the motion sensor goes to ON. When the motion sensor goes to OFF check to see if the timer is still running. If it is cancel the timer. Put the code you want to execute after 2 seconds in the body of the timer.

I was thinking that you had to use the persistence data or something like that not use timers

That’s one of the first peices of advice you ever gave me I took you seriously and stopped using sleep and just try to stick to timers as you said to me ages ago

Canceling a timer is still something I have not worked out how to do I still have alot too learn about openhab

Dont blame you my posts are normally boringly simple lol :smiley:

They are completely independent.

myTimer.cancel

I haven’t been naming my timers just using the create timer now command

Use a global var initialized to null then assign the result of the call to creatTimer to it.

var Timer myTimer = null
...
  
    // Create timer
    myTimer = createTimer( now.plusSeconds(2), [ |
        // Do stuff
        myTimer = null
    ]

...

    // Is timer running?
    if(myTimer != null) // timer is running

...

    // Cancel timer
    myTimer?.cancel
    myTimer = null

Or, even better, use Design Pattern: Expire Binding Based Timers

1 Like

Out of experience based on my setup, setting a timer will take some time (more than 2sec?) so what I get in reality is a timeframe longer than that I defined / planned for, when setting the timer.

:astonished: - that one is news to me.

Kurt

That is incorrect behavior and I’ve never seen that reported before. You should file an issue, though if you have a lot of sleeps that could be the source of the problem.

Why not just use c++ to do the what if’s and and ifs and just use OpenHab to report what it sees … if your using a Raspi you can use the Particle Pi application and have direct access to to raspi io Pins …Use Node Red to interconnect with OpenHab (Super easy ) then you can control and monitor effortlessly without worrying about rules

I will look into this more and try too add names too my timers it would require alot of editing of my rules i will keep this post for reference