How to create timer-like or polling rule?

  • Platform information:
    • Hardware: RPI4
    • OS: bullseye RPIOS running within Docker
    • Java Runtime Environment: no clue
    • openHAB version: 3.2.0

I’m still relatively new to OH and can’t get around how to do the following:

I have a lamp, which I can turn on with a switch, a second switch and several motion detectors. On the device, which actually turns on the lamp I created an expire-timer which turns of the lamp after 5 minutes.

This is a kludge.

What I really want is that after 5 minutes after the lamp has been turned on a check is repeatedly done (every minute or so) which might turn off the light depending on some other conditions.

How to create such a rule with OH 3.2?

While writing I got the idea that a delayed cronjob-like-rule could do it I.

Generally speaking, in OH polling like this is not the best way to go about things. Because OH is event driven, for most events you don’t have to constantly check for them; you just setup a rule that responds to that event when it happens.

If you supply a few more specifics about these “other conditions” someone can probably help you get to a solution that doesn’t include the polling.

Of course, it’s also possible that this is one of those few exceptions, in which case there a several options. Here are a couple:

  1. The most programmatic would be a rule that starts 5 minutes after the lamp is switched on with a timer that runs your check and reschedules itself to check again in one minute if the conditions are not met.
  2. If you are using UI rules then it is fairly easy for one rule to enable/disable another. You could have one rule that runs your checks on the cron schedule but which is disabled except for when the 5 minute after lamp rule enables it.
  3. Easiest to set up but probably least desirable would be a UI rule that runs on your cron schedule, but with a rule condition that only executes the script actions if the lamp is on.
1 Like

The “other conditions” are actually that there are no event coming from the things. One of them is that there no repeating of a motion event if the motion is continuous for the detector: it will stay on motion ON.

I have not yet written a rule outside of the UI. And I must say I’m struggling to find a good HowTo of how to do it with OH 3.2.

OK, so a cron-rule which is enabled by the expire-mechanism and then checks every minute until all conditions are good, turns off the lamp and itself. I like this.

Can’t use transient events as conditions. Hopefully you’ve got this motion detector represented as an Item state. It’s pedantic, but it helps to structure solutions if you think clearly about this stuff.

So restructuring…
It seems to me you’d want to turn on the light when motion begins. No timer. When motion stops, start the off-timer. That’s it.
To add a refinement, you would probably want the motion-on action to cancel any existing timer, to take account of motion restarting within the timer period.

No kludgy cron at all, just a rule that triggers whenever motion changes and decides what to do about it.
The hard part is making a timer that therule can find again if is triggered again. You’ll need to choose what scripting language to work with before going further.

And how to integrate the fact that I have other events (buttons) which can turn on the same light?

No idea. What do you want to happen, its your decision? There are a lot of edge cases. If a button was used to turn off the light, do you want any motion to turn it on? If a button was used to turn on the light, do you want any off-timing at all? What about if a user uses the GUI to turn it on?

Basicly, you decide when you want the timer to be started or cancelled.

Try something simple, get it working. Refine or replace later.

1 Like

In OH a device changing from ON to OFF is also an event and therefore is just as easy to work with as a device changing from OFF to ON. I don’t know what motion sensor you are using, but most will allow you to configure the amount of time it will remain in the ON mode after it has detected motion. If it detects motion again in that time it will stay ON and reset the window for more detection. If you can find a way to configure this for your sensor, then you might not even need the timer within OH at all. You just need a rule that turns on the light when the motion sensor changes to ON and turns off the light when the motion sensor finally changes to OFF again after the configured amount of time.

I completely agree with rossko57, assemble this one small step at a time. If you can get something like the above outlined rule working first, then additional components are fairly easy. For example, any rule can have more than one trigger event. If the rule works with the motion sensor turning ON then just add a second trigger to that rule so that a button event also starts the rule (rule triggers are always OR so a rule with more than one trigger will start when any one of those trigger events happens). Presumably, if the button turns on the light before the motion sensor does, the motion sensor is soon going to be triggered by some related activity. Then you will still have an ON light and an ON motion sensor and the sensor going back OFF again will turn the light off just as before.

1 Like