Help creating complex rule

Hi all,

Looking for some help in creating a rule which doesn’t seem possible within PaperUI (I’m much more a GUI person than coding person!)

Ultimately I would like to turn on lights within the house based on presence, lux level, and time for security reasons.

Lights are currently operated using Sonoff Basics via MQTT commands from basic timers on OH but I would like this to appear more natural based on how the light is fading.

I think what I need is code along the lines of:

IF thing1 AND thing2 not present
IF LUX01 value < x
IF time is between 15:00 and 23:00
Change item state to ON

I currently have the lux sensor persisting data in OH via MQTT, and presence detection is done via the Unify binding searching for both mine and partner’s mobile.

If we are at home I don’t want to turn the lights on, as if we’re watching a movie will want it to be intentionally dark!

Any help to point me in the right direction will be gratefully received, if I have missed any important info please let me know and I’ll post what I can

I do not know the Unify binding, but assuming it has for each Thing an Item that tracks presence by means of a switch, you would be looking at something like this:

rule "Turn lights on based on presence, time and lux"
    Item Thing1 changed or
    Item Thing2 changed or
    Item LUX01 changed or
    Time cron "0 0 15 ? * *"
    if(Thing1.state == OFF && Thing2.state == OFF && LUX01.state as NumberItem < x && now.isAfter(now.withTimeAtStartOfDay.plusHours(15)) && now.isBefore(now.withTimeAtStartOfDay.plusHours(23))){

Thanks for your reply Rolf.

There is an Item as type “switch” for each mobile device, and I have these displaying on HABPanel same as an on/off switch button would.

Am I right in thinking that the “Time cron” part means the code runs every 15 minutes and will activate if all conditions meet?

No, the cron "0 0 15 ? * *" means that at the time of 0 seconds, 0 minutes, 15 hours, at any day of the month, every month, every day of the week, the rule runs. So basically, what I assumed is that you want to change the state of your lights if any of the following occurs:

  • Thing1 changes presence
  • Thing2 changes presence
  • The light intensity of LUX01 changes,
  • It becomes 15:00

If that is not true, I thought the easiest way for you would be just to remove conditions for the rule to run. :slight_smile:


Good, then use those Items as Thing1 and Thing2.

I suppose what may be the best way to look at it is to monitor the lux level, and once the drops below a certain value, the rule then checks to see if anyone is home and the time is after 15:00 it then turns on the lights.

The reason I have the time element in there is say there’s a bad storm or such that cloud cover may drop the lux at an earlier time of the day.

Does this mean the rule runs once only at 15:00? Should the lux not drop below the level until say 18:00 the rule then wouldn’t trigger?

No, the rule runs when it’s 15:00, or when the lux change or when the presence of Thing1 changes, or if the presence of Thing2 changes.

In that case, you can limit the when part to:

    Item LUX01 changed

So the Rule runs every time something changes to LUX01. Then it checks if Thing1 and Thing 2 are not present, and the lux value is below a certain threshold, and it is between 15:00 and 23:00, and if all that is true, it turns on the lights.

Brilliant, I think I’m getting the hang of the syntax now.

As said I’m not really a coder, but once I’ve got it in front of me and had it explained I can make sense and then make adjustments.

Would I be right in saying that by only using LUX01 in the when section, this will cause the rule to run almost constantly as the light level is always changing throughout the day. Will this put much load on the OH server?

That depends on the settings of your Thing. Typically you can set how often a change is reported, or how a big a change needs to be before it gets reported. And next to that, typically this doesn’t put a high load on the server.
To check this, you could (just for testing purposes) add a logging statement, and check how often that ends up in the openhab.log file, and with which value:

logInfo("lux","LUX01 changed to " + LUX01.state)