Hello, comunity!
A month ago I started with OpenHAB, and during this time I have been learning and asking a lot of questions in this forum. I really appreciated all the help that I received from the members of this community, so here is my small contribution.
Based on solutions and suggestions received to my questions, I would like to share the implementation of some automations using both Rules DSL and Jython. These solutions are not final or complete but can help new members to start with OpenHAB (at least that’s my objective )
Automation #1 - Doorbell notification
Send me a notification when someone presses the doorbell, but don’t notify me after every press. Each notification must have an interval of at least 30 seconds.
Items
String Doorbell { channel="mqtt:topic:MyBroker:Home:Doorbell"}
Rules DSL Implementation
var lastRing = now.minusSeconds(35) // keep track of the last notification
rule "(DSL) Doorbell notification"
when
Item Doorbell changed to PRESSED
then
// check if the last alert was more than 30 secs ago
if(lastRing.isBefore(now.minusSeconds(30))) {
// send the doorbell notification
lastRing = now
}
end
Jython implementation
from core.rules import rule
from core.triggers import when
from java.time import ZonedDateTime as DT
lastRing = DT.now().minusSeconds(35)
@rule("(Py) Doorbell notification")
@when("Item Doorbell changed to PRESSED")
def doorbell_notification(event):
global lastRing
if(lastRing.isBefore(DT.now().minusSeconds(30))):
# send the doorbell notification
lastRing = DT.now()
For this example, I’m considering a single item Doorbell that can have two states: PRESSED or UNPRESSED. Both implementations start by initializing the lastRing variable to 35 secs before the actual file holding the rule is loaded into the system, by doing that the automation will always send a notification the first time the doorbell is pressed.
I hope these solutions can help someone
Humberto
Note:
- Suggestions or recommendations to improve the implementation are welcome!
- Do you have more complex automations that shares the same logic of this example? Please share it
Other automation examples
- Automation #2: Bathroom Smart light
- Automation #3: Smart Radiator
- Automation #4: Smart Radiator (Generic Rule)
- Automation #5: Window Alert
- Automation #6: Boiler Failure Alert
- Automation #7: Detect a particular sequence of events
Related Posts: