[SOLVED] Smart doorbell


i try to configure m smart doorbell.
i use a esp8266 with homie an this code:

#include <Homie.h>
#include "FS.h"

int readDelay = 50;
int buttonPressDelay = 3000;
int openingDelay = 3000;
int inputPin = 0; //D3;
const int relayPin = 4; //D1=5,D6=12,D2=4;
int inputValue = 0;
bool opened = false;
int timeOpened = 0;

const int PIN_RELAY = 14; //14

HomieNode doorbellNode("button", "button");
HomieNode openerNode("door", "switch");
HomieNode switchNode("plug", "switch");

//----------Klingel lautlos---------
bool lightOnHandler(HomieRange range, String value) {
    if (value == "true") {
        digitalWrite(PIN_RELAY, HIGH);
    } else if (value == "false") {
        digitalWrite(PIN_RELAY, LOW);
    } else {
        return false;
    return true;

boolean openerHandler(const HomieRange& range, const String& value) {
  if(value != "true" && value != "false") return false;

  if(value == "true") {
    digitalWrite(relayPin, HIGH);
    Serial.println("Opener started ...");
    opened = true;
    timeOpened = millis();

  return true;

void loopHandler() {
  inputValue = digitalRead(inputPin);

  digitalWrite(BUILTIN_LED, inputValue);

  if(inputValue == 0) {
    Serial.println("Doorbell pressed ...");


void setup() {

  pinMode(inputPin, INPUT_PULLUP);

  digitalWrite(relayPin, LOW);
pinMode(relayPin, OUTPUT); //Pin connected to relay
  digitalWrite(relayPin, LOW);

digitalWrite(PIN_RELAY, LOW);

  Homie_setFirmware("Doorbell", "1.0.0");




void loop() {
  int timeNow = millis();

  if(timeOpened > 0 && (timeNow - timeOpened) > 3000) {
    Serial.println("Close ...");
    timeOpened = 0;

    digitalWrite(relayPin, LOW);
    Serial.println("Opener finished ...");


But every time i resart openhab oder my ESP8266 the Relais at Pin 4 is shot active high in between the boot sequence.
Do you know how to solve the problem?

I didn’t look much at the code but have you tried setting rules to load after OH starts via the openhabian-config tool?

i use openhab not openhabian, so i don’t thing i have this config option.

after i change on my esp

HomieNode openerNode("doorr", "switch");

everything works fine, but after i add it in my paperui *.item and *.sitemap i got this startup problem :frowning:

Is that a typo here on the post “doorr” or is that what you have in the file on esp?

this is the id which i use in my mqtt command.

so my mqtt command looks like:


You’ve tested and verified that command works? I ask b/c it’s odd that “doorr” is nowhere in the code above.

that’s true, because it is only the id which i use for my mqtt command.
you can find the “openerNode” linked name.

i don’t know why, but the other relays doesn’t have any problem

HomieNode switchNode("plug", "switch");

What are you running OH on and how did you install OH?

i use Raspbian on my RPI3.
On Raspbian i have installed OP.

To install openhabian:

Other Linux Systems (add openHABian just like any other software)

openHABian also supports general Debian/Ubuntu based systems on different platforms. Starting with a fresh installation of your operating system, install git, then clone the openHABian project and finally execute the openHABian configuration tool:

# install git
sudo apt-get update
sudo apt-get install git

# download and link
sudo git clone https://github.com/openhab/openhabian.git /opt/openhabian
sudo ln -s /opt/openhabian/openhabian-setup.sh /usr/local/bin/openhabian-config

# execute
sudo openhabian-config

You’ll see the openHABian configuration menu and can now select all desired actions. The “Manual/Fresh Setup” submenu entry is the right place for you.

The doc’s full description here:

Install and use openhabian-config tool to set rules to run after OH starts. I do not know if that will solve the issue but other suggestion is changing the code in OP so pin 4 does not go high on restart of OH.

is there another option, because i thing i got a lot oft problems if i install openhabian over openhab.

i think this should be the same:

rule "On system start"
		System started
		val actions = getActions("mqtt","mqtt:mqtt:topic:4635ef43:Tueroffner_new")

My problem was that i thought autoupdate=“false” in my *.item change the status to the old one.

Now i create a rule to change ON status to OFF after pressing the button.

Now everything works without problem