[SOLVED] Outdoor flood light rules

Hi,

I am currently running openhab 2.3 on a synology server.
I purchased a Sengled smart outdoor flood light as at Sengled smart outdoor floodlight
Since I don’t have a Sengled hub so I used a smartthings V2 hub to connect it to openhab.
It works well just switching on and off. But when I write some rules, it just didn’t turn off properly sometimes.This light has a motion sensor. So it comes on a few times a night.
Before I connected it smartthings, it came off automatically. After It was connected to smartthings, it didn’t turn off automatically so I need a rule to turn it off. I have a garage in the backyard so I want it turning on when the garage door is open(which has a xiaomi door sensor on it) or when motion is detected. The rule is as below:

var Timer timer=null

rule "floodlight auto off"
when Item SengledSwitchLevel changed
then
    if(garage_dw.state==OPEN) SengledSwitchLevel.sendCommand(ON)
	else
	 timer=createTimer(now.plusSeconds(20),[ |
	 SengledSwitchLevel.sendCommand(OFF)
	 timer=null
	 ])
end

rule "floodlight auto on"
when
 Item garage_dw received update
then
  SengledSwitchLevel.sendCommand(ON)
end

Please give me some idea why this rule not work all the time. Thanks.

Regards
Clement

you might need some spaces in your if statement, try:

if(garage_dw.state == OPEN)

No, spaces are irrelevant here.
But in fact, I’m pretty sure the rules are at least strange.

Every time the garage door is opened or closed, you turn the light on.
Every time the light has changed, you test the door contact.

Well, at least, I guess, the rule will work…

And there is no way to get information about the motion sensor?

Hi Udo,

I am not quite familiar with the codes so just made it up from other people’s code.
This light act weird after connected to smartthings hub. Previously it was auto on when detect motion then auto off after a while. But after connected to smartthings hub, it comes on after detect motion but not auto off. This is the reason why I added the “floodlight auto off” rule.
The smartthing hub didn’t find the right brand light automatically so I added it as a general bulb. There is no way to contact the motion sensor so I just leave it as it is.
Back to the rule. It was just for turning off the light after 20s if it was turned on for some reasons when the garage door was closed. The light was turned on for many reasons. It might be tree moving or Possums and cats running around. But it just can’t be turned off automatically sometimes. I don’t know if there are some error in the rules or my Synology was hanged for some reasons. But I did know my Synology was quite stable for other rules. I am so confused.

Regards
Clement

So, the light should be switched off after 20 Seconds, but not if the door is open?

// global vars at head of rules file
var Timer tLight=null                                  // use unique names

rule "floodlight auto off"
when
    Item SengledSwitchLevel changed
then
    tLight?.cancel                                     // cancel a pending timer
    if(SengledSwitchLevel.state == ON)                 // if light was switched on
        tLight = createTimer(now.plusSeconds(20), [ |  // start timer, when expired:
            if(garage_dw.state==CLOSED)                // if door is closed
                SengledSwitchLevel.sendCommand(OFF)    // switch light off
            else                                       // if door is open
                tLight.reschedule(now.plusSeconds(20)) // reschedule timer
        ])
end

rule "floodlight auto on"
when
     Item garage_dw changed to OPEN                    // if door was opened
then
    SengledSwitchLevel.sendCommand(ON)                 // switch light on
end

Hi Udo,

Thanks so much for your help. I will give it a try later and report back the result.Have a good day.

Regards
Clement

Hi Udo,

I had some fault on the item side which is kinda critical. After adjusted the item file and used your code, the flood light is working flawlessly. Now I have a smart security light protecting my yard.Thanks so much for your help. :slight_smile: Have a nice day.

Regards
Clement