Doorbell connected to openHAB

Introduction

Our son of 2 takes afternoon naps. Because of that we pull the doorbell plug every time we expect someone to come to the door.As I am using openHAB for home automation, I have been looking to find a better solution for this “problem”.

So I kept looking for wireless solutions, connected solution, etc that could be incoorperated into openHAB. Sofar I had not really find one. And from the discussions on the openHAB forum, I was not the only one looking for such a solution.

So my  article describes a “openHAB connected doorbell”. Given that I could reuse our old doorbell ringer, the wiring and existing button, the additional price for this solution is roughly $10,-

Not much of this doorbell is my original work. It is bits and pieces taken from the internet, mostly the work of Theo Arends and of course the fine people of openHAB.

The hardware

Programming Sonoff firmware

I re-programmed the firmware with what is mentioned here: https://github.com/arendst/Sonoff-Tasmota. All information on how to set-up the arduino environment, how to connect and programm the Sonoff, etc. Can also be found on their. The Wiki page is really helpful too .

I will not repeat what it says there, only that I used the Arduino environment, not PlatformIO.
Please do not hesitate to contact me if you do have any questions.

Connecting the doorbell button

An extra button can be connected to the sonoff GPIO14, that is available on the header as pin 5

Looking at the Sonoff picture above Pin 1 (square) is at the bottom top Pin 5 at the top.

PinFunction
1Vcc
2Rx
3Tx
4GND
5GPIO14

The bell button is connected between pin 4 & pin 5.

 From old to new

The silly pictures below show how the wiring changes before and after adding the Sonoff.
Before: pressing the doorbell would close the loop between actual ringer and transfomer.
After: Ringer is connected to transformer, so powering the transformer  makes the bell ring. Pressing the doorbell notifies the Sonoff, Sonoff powers the transfomer.

Before
After

The software

I already had openHAB 2 running so will not describe how to install that.
Because the new firmware uses MQTT I installed mosquitto on my system. Did not change much of the settings of mosquitto.

Configure Sonoff

Configuration of the Sonoff can be done in it’s web interface, or in the console also found in the web interface.

  • Add external button in the module settings
    (I selected Switch1
  • Set MQTT server name/ip-address
  • Change MQTT topic (eg: sonoff-bel)
  • Make sure button press has it’s own MQTT Topic
    (In console type: ButtonTopic1 Klik)
  • Change Blinkcount to 1
    (In console type: BlinkCount 1)
  • Change BlinkTime  to 2 seconds
    (In console type: BlinkTime 200)
  • Change Switchmode to inverted FOLLOW
    (In console type: SwitchMode1 2)

I don’t want to run into a situation where the bell starts ringing and never stops. So instead of switching it on/off from openHAB, I just send a Blink signal, setting the blink count to 1 and duration to 2 seconds. In that way it just ring for 2 seconds if someone presses the button.

Configure openHAB2

  • Configure MQTT binding
  • Add the sonoff as an item to openHAB
    (MQTT binding is a v1 binding, no automatic detection of things)
  • Create a map file for the Blink signal
  • Create a rule for the bell to ring
  • Add selection to sitemap
    I can choose to: ring the bell, flash my hue lights or both
mqtt.cfg
# URL to the MQTT broker, e.g. tcp://localhost:1883 or ssl://localhost:8883
oh2.url=tcp://localhost:1883

# Optional. Client id (max 23 chars) to use when connecting to the broker.
# If not provided a default one is generated.
oh2.clientId=OpenHAB2_MQTT
default.sitemap
Switch item=BelMode label="Bel" icon="bell" mappings=[ 0="[ B ]", 1="[ L ]", 2="[B+L]" ]
sonoff.item
Number BelMode "Bel Modus"
Switch Bel  "Deurbel" 
{ mqtt=">[oh2:cmnd/sonoff-bel/power:command:*:MAP(sonoff.map)],
            <[oh2:stat/sonoff-bel/POWER:state:default]" }
Switch BelKnop  "Deurbel Knop"
{ mqtt=">[oh2:cmnd/Klik/power:command:*:default],
            <[oh2:cmnd/Klik/POWER:state:default]" }
sonoff.map
OFF=0
ON=3
bel.rules
rule "Aangebeld"
when
    Item BelKnop changed from OFF to ON
then
    switch(BelMode.state) {
	case 1 : {
	    sendNotification("my@email.bogus", "Someone pushed the button")
	    sendCommand(LivingAlert, "LSELECT")
	    sendCommand(SchemerAlert, "LSELECT")
	    sendCommand(TKamerAlert, "LSELECT")
	    sendCommand(TRaamrAlert, "LSELECT")
	}
	case 2 : {
	    sendCommand(LivingAlert, "LSELECT")
	    sendCommand(SchemerAlert, "LSELECT")
	    sendCommand(TKamerAlert, "LSELECT")
	    sendCommand(TRaamrAlert, "LSELECT")
	    sendNotification("my@email.bogus", "Someone pushed the button")
	    sendCommand(BelKnop, OFF)
	    sendCommand(Bel, OFF)
	    sendCommand(Bel, ON)
 	}
	default : { 
	    sendNotification("my@email.bogus", "Someone pushed the button")
	    sendCommand(BelKnop, OFF)
	    sendCommand(Bel, OFF)
	    sendCommand(Bel, ON)
	}
end

12 Comments


  1. How did you connect the doorbel to the pins? Can you help me in reproducing this setup?

    Reply

    1. The doorbel button is connected to GND and GPIO14 (Pin4 & 5 as described above)
      The doorbel itself is not connected to the Sonoff. The sonoff’s output powers the doorbell transformer, which makes the bell ring..

      Reply

      1. Thx, I’m all new to this board n wires n stuff. What I meant was indeed, how to connect the doorbel button to pin 4 and 5. What kind of connector did you use or did you just rotate the wire around the 2 pins?

        Reply

      2. I got a lot of false positives (randomly ringing the doorbell :(). I have added a 1k resistor between 3V3 and GPIO14. I am now testing out if this removes the false positives.
        Strange thing is, my setup worked ok for a few hours and then it suddenly started toggling every now and then.
        I will let you know if this is my final solution.

        Reply

    2. I would not advise to just “rotate the wires around the 2 pins”. Low voltages are used (3,3V) which means “static”and other disturbances picked up might easily been seen as “a signal” there for if the wire to the doorbell button is more than just a few centimeters I would use shielded wire, soldered to a female connector that fits the header pins of the sonoff.

      Reply

      1. So you soldered 2 wires with female connectors to the one doorbell button wire?

        Reply

  2. I started with this approach but I did not like having to depend on a working MQTT server; if the network is flaky or the server not available, the bell wouldn’t ring. Plus I use Home Assistant so not tied to OpenHAB specifically.

    Thinking it through, I had these requirements:
    – By default, doorbell should ring (e.g. after bootup or power loss)
    – With no working network or MQTT, doorbell should still ring
    – Must be possible to disable doorbell relay with tiny button on SonOff
    – Must be possible to disable doorbell relay through MQTT
    – Must be possible to check doorbell relay state with SonOff LED

    So I flashed ESPEasy and set it up this way:

    – Unit name ‘doorbell’
    – OpenHAB MQTT controller
    – Device 1: Switch input, name ‘relay’, GPIO-12 (the relay on the Sonoff Basic), switch type Switch, Normal Switch, send to controller checked, Value 1 set to ‘state’
    – Device 2: Switch input, name ‘button’, GPIO-14 (extra pin on Sonoff Basic, switch type Switch, Inversed Logic checked, Normal Switch, send to controller checked, Value 1 set to ‘state’. This goes to the actual doorbell.
    – Device 3: MQTT Import, name ‘doorbelldisable’, MQTT Topic 1 ‘doorbell/disable’, value 1 name ‘state’
    – Device 4: Switch input, name ‘sonoffbutton’, GPIO-0, value 1 name ‘state’
    – Rules enabeld
    – Rule set 1:

    On doorbelldisable#state=0 do
    gpio,13,0 // Toggle led on when doorbell sound enabled
    endon

    On doorbelldisable#state=1 do
    gpio,13,1 // Toggle led off when doorbell sound disabled
    endon

    On button#state=1 do // When button pushed (GPIO14)
    if [doorbelldisable#state]=0
    gpio,12,1 // Toggle relay on
    delay,1000 // Delay for 1 second
    gpio,12,0 // Toggle relay off
    delay,1500 // Delay for 1.5 second
    gpio,12,1 // Toggle relay on
    delay,1000 // Delay for 1 second
    gpio,12,0 // Toggle relay off
    endif
    endon

    On sonoffbutton#state=1 do // When sonoff button pushed
    if [doorbelldisable#state]=0
    TaskValueSet 3,1,1 // set state to disabled
    Publish doorbell/disable,1 // Publish disable to MQTT
    else
    TaskValueSet 3,1,0 // set state to enabled
    Publish doorbell/disable,0 // Publish enable to MQTT
    endif
    endon

    This setup will write 1 or 0 to MQTT topic doorbell/button/state to indicate if the button is pushed, and will toggle the doorbell relay twice for 1 second (time and duration can be adjusted at will obviously).

    It also allows you to disable the relay by publishing ‘1’ to ‘doorbell/disable’, or 0 to enable it. The led on the SONOFF reflects the current state (led on = doorbell on).

    Lastly you can toggle the state with the little button on the sonoff which will both update it internally as well as update the MQTT topic, so this should work even with no wifi or no MQTT server.

    Reply

    1. Oh and as a final point, Pin mode 13 should be set to Output Low for default.

      Reply

  3. I have implemented this design and have it working well with Home Assistant.
    But I do get some false triggers, especially (mostly) at night.
    Is there any way to prevent this from happening?
    I do have the wires connected to the Sonoff with female header wires on both ends.

    Reply

  4. After placing the 3.3k resistor at the cable op gpio14 i get still a lot of false alarms.
    Anyone a solution?

    Reply

  5. I have implemented the same setup. I’m using an unshielded solid core twin copper wire, which is normally used as doorbell wire.

    I have connected the wires to GPIO 0 and ground by soldering the wires to the base of the pushbutton on the sonoff basic.

    I’ve flashed ESPurna instead of ESPeasy. This firmware is more user friendly and seems more stable then ESPeasy. I have defined GPIO as a switch button i.o. of the push button.
    ESPurna has native support for Domoticz and Hass using MQTT.

    I’m running this setup for over a month, without any false triggering or missing doorbell presses.

    Don’t forget to setup a auto switch off after a few seconds, this has cost me a doorbellringer. (Keeping the ringer powered for over 2 days in a row doesn’t seem to be very good for a ringer)

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.