Automation #1: Doorbell notification

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 :slightly_smiling_face:)

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 :slightly_smiling_face:

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 :slightly_smiling_face:

Other automation examples

Related Posts:

9 Likes

@rhumbertgz great example and thank you for promoting use of the Helper Libraries! My only suggestion would be to use java.time.ZonedDateTime instead of org.joda.time, as joda is depreciated and may disappear when openHAB moves to a newer Java version. Check out the docs for java.time.ZonedDateTime for more information on its use, though it is very similar to joda, and the core module date.py for some helper functions to convert various date types.

In your example, all of the methods of joda.time you are using are found in java.time as well, so it can be used as a drop-in replacement.

1 Like

Hi @CrazyIvan359, thanks for your suggestions!

I updated my post :+1:

Great example. Thank you!

1 Like

Hi. These are very helpful so thank you for the code and the direct request to complete the poll which pointed these out to me!

I’m interested in the physical kit you used to create the doorbell and send the MQTT notification?

Cheers

Fraser

Hi @scootaash!

My DIY doorbell implementation is based on this video https://www.youtube.com/watch?v=xCQoOZNdaGY

Regards