I’m trying to automate light in my house and would like to create a rule that turn off any of my Hue bulbs is ON for more than X hours. I can think of 2 different ways:
- Use Cron to check every x minutes and check if any light is on for more than X hours, however I’m not sure how to check the state of something that has been ON during the past period
- Create a timer when a light is ON to run after X hours, in case the light is still on after X hours turn it off. The drawback of this approach is that the light can be turn off and on again during the X hours, in which case it will still turn off the light regardless.
Anybody who has done similar rule like this and can perhaps share your experience? Thank you
See “expire” parameter.
You can create the item to get the predefined setting after some time, easiest way to do it
Thanks, that’s exactly what I need, and it’s a very nice way to do in this way. I’m so glad that I asked before spending time on the rule myself!
Expire is definitely the right approach for these requirements. But you may encounter situations where that’s not going to work so let’s answer your questions anyway.
This is usually something you only want to do as a last resort. OH is event driven so it’s much better when you drive the behavior based on events rather than polling like this.
There are a bunch of ways you can achieve this.
If the Item is persisted you can get the last update from the Item. That will return the timestamp of the last time that Item was saved. If you are using only a
changed strategy on that Item the last update will return the timestamp when it changed to the current state. If you are using rrd4j or any other DB with a periodic strategy you’ll want to pass
true to the call to
lastupdate which will look back for the first entry in the DB where the value was different from the current Item’s state.
You can create a new DateTime Item and use the timestamp profile. When the light changes state it will update the Item’s state with the current time.
You can create a rule to store a timestamp in the
cache from a rule.
This is the more standard approach to handling something like this.
Not if you cancel the timer when the light is turned off manually.
The timer approach usually involves the following steps:
- Create the timer when the light turns ON
- If the light turns OFF while there is a timer, cancel the timer
- If something else is controlling the light, like a motion sensor, reschedule the timer if the controller triggers again but the timer already exists.
Additional approaches to handling cases like this include:
Thanks for the detailed reply!
I tried expiry and it works - had initially some issue with binding keep updating the item, but then I saw your suggestion here: [expire] Add an optional flag to have Expire reschedule on change as opposed to update · Issue #2542 · openhab/openhab-core · GitHub
Which solved the issue, thanks to you and the great community here!