Advice on setting up an ESP Doorbell in OH2

  • Hardware: Raspberry Pi 3 Model B Rev 1.2
    • OS: Raspbian GNU/Linux 10 (buster)
    • Java Runtime Environment: zulu
    • openHAB version: 2.5.10-1
    • Working MQTT environment that has sensors publishing data and recording onto a SQLite database

Hi,

I’ve successfully manage to program an ESP8622 HUZZAH board to wake up from deep sleep, and then posting the following to my MQTT listening on the OH2 platform. I’ve successfully verified this by connecting MQTTfx to the server and subscribing to the following topic (line in the Arduino sketch):

client.publish("Home/Ground Floor/Front Door Bell", "Ring");

when the button is pressed. That is great but now I’m wondering what the next step is. I guess I need to create a thing and item and once these are create, I need to create a rule that activates a relay or something similar on another ESP that play a tune and/or switches on a little LED to notify my family that someone is at the door. I see that this topic is similar although quite old.

What I don’t quite understand is whether I should be treating this as a switch so that my thing would look like the following:

Thing mqtt:topic:frontdoorbell “Door Bell” {
Channels:
Type switch : doorbell “Door Bell” [ stateTopic=“Home/Ground Floor/Front Door/Bell” ]
}

I would then create an item like so:

Switch FrontDoorBell “Front Door Bell” {channel=“mqtt:broker:topic:frontdoorbell:doorbell”}

I’m not quite sure but I guess the next step would be that when the MQTT broker receives the above message, OH2 would then have to have a rule that reacts to the above message? The reaction would be (in my idea) to activate another ESP device, (or even better a pi zero?) that is listening for this message and commands to play a tune. Any thoughts on how I go about from here?

I guess my two next big challenges are:
a) creating the rule upon which OH2 enacts to the pressing of the button (at the end of this post, which I copied below for convenience, a possible solution is proposed … do you think this will work?)
b) how I choose to build the listening device; looks like ESP or Pi zero wouldn’t be good options if they are sitting there waiting for a message … they would just be using lots of battery power.

rule “mechanical door bell pressed”
when
//Item MechanicalDoorBell received command or
//Item MechanicalDoorBell changed
Item MechanicalDoorBell received update
then
var msg = “The mechanical door bell was pushed.”
sendBroadcastNotification(msg)
playSound(“doorbell.mp3”)
logInfo(“SAH”, msg)
//say(msg, “voicerss:enUS”, “chromecast:audiogroup:PublicSpeakers”, new PercentType(100))
end

I’ve done this with my doorbell but I’m only playing ding-dong using sonos speakers and alexa to announce that someone is at the door.
See my smokemachine esp.integration it’s more or less the same thing you want to achieve.

You can have so much fun with this.

openHAB switch topics are ON OFF states by default so you can either change it to

client.publish("Home/Ground Floor/Front Door Bell", "ON");

or you can map the states of the thing

Thing mqtt:topic:frontdoorbell “Door Bell” {
Channels:
Type switch : doorbell "Door Bell" [ stateTopic="Home/Ground Floor/Front Door/Bell" , Ring="ON" ]
}

Now you need to stop the bell from ringing

So I think the best way to do it is in the HUZZAH code when the button is not pressed so every press can be recorded.

client.publish("Home/Ground Floor/Front Door Bell", "OFF");

Now without using openHAB you can control other devices like tasmota for lights

client.publish("cmnd/light/POWER", "ON");

With openHAB you can use it to trigger a rule and play the sound on a google home or amazon alexa or any device.

You can also use a webcam feed so when someone presses the door bell it can send a telegram message with the photo and a question “Do you want to open the door” YES/NO

It really depends on what you have setup in openHAB as to what you can do

Eg If you setup a google home named googlehome

rule "Play sound when someone rings doorbell"
when
    Item MechanicalDoorBell received command ON 
then
    playSound("chromecast:audio:googlehome", "doorbell.mp3")
end

Your switch item should look like

Switch FrontDoorBell "Front Door Bell" {channel="mqtt:topic:frontdoorbell:doorbell"}

You would also need to setup a mqtt bridge and reference that in you thing

So either

Bridge mqtt:broker:myMQTTBroker [ host ="192.168.1.148", secure =false, username ="please", password ="work" , clientID ="myMQTTClient" ]
{
 
    Thing topic frontdoorbell "Door Bell" {
    Channels:
        Type switch : doorbell "Door Bell" [ stateTopic="Home/Ground Floor/Front Door/Bell" ]
    }
}

All in one file

If you want to create a thing in a different file and reference the bridge example above it would be

Thing mqtt:topic:frontdoorbell "Door Bell" (mqtt:broker:myMQTTBroker){
  Channels:
      Type string : doorbell  "Door Bell" [ stateTopic="Home/Ground Floor/Front Door/Bell" ]
  }

You can’t have things with the same name

Thanks for your detailed response denominator.

You’re right, I hadn’t thought of the ON/OFF status of the door bell. This means that I probably have to add another client.publish to my sketch code and a delay in between so that:

a) Press the button
b) Publish ON status
c) Delay 2 seconds
d) Publish OFF status
e) Delay 10 seconds (to avoid kids pressing the button 150 times/sec)
f) Go to deep sleep mode

Regarding the things, I’m a bit confused with the points you make. I already have a mqtt.thing file in /etc/openhab2/things/ as I have all my sensors in there. So i can simply copy your code and add it to the bridge list. For my sensors, I haven’t created a seperate things files. Why do I need to create another things file? It seems like I’m duplicating the thing?

I couldn’t wait to the weekend to get this working (or at least try) so this is what I’ve done (and miserably failed):

Changed the ESP sketch so that both and ON/OFF status is transmitted to the broker:

void frontdoorbell() {
// Connect to the MQTT server on the local network and publish temperature and humidity
if (client.connect(mqttServer,mqttUser,mqttPassword)) {
Serial.print(“Door bell”);
client.publish(“Home/Ground Floor/Front Door/Bell”, “ON”);
Serial.println(“Published ON to Home/Ground Floor/Front Door/Bell”);
delay(2000);
client.publish(“Home/Ground Floor/Front Door/Bell”, “OFF”);
Serial.println(“Published OFF to Home/Ground Floor/Front Door/Bell”);
delay(8000);
}
else {
// If Arduino can’t connect to the server (your computer or web page)
Serial.println("–> connection failed\n");
}
}

Both the ON and OFF status are transmitted to the broker as I can successfully see these in MQTTfx. ESP and broker are not the issue!

I added the Thing to the mqtt.things file (which is already there doing its work nicely for my temperature sensors):

Thing mqtt:topic:frontdoorbell “Front Door Bell” {
Channels:
Type switch : frontdoorbell “Front Door Bell” [ stateTopic=“Home/Ground Floor/Front Door/Bell” ]
}

Next, I created a doorbells.items file and added the following:

Switch frontdoorbell “Front Door Bell” {channel=“mqtt:topic:frontdoorbell:doorbell”}

Last but not least, I created the file doorbells.rules with the rule as such:

rule “Play sound when someone rings Front Door Bell”
when
Item frontdoorbell received command ON
then
playSound(“doorbell.mp3”)
end

No sound! The sound system works as when I try smarthome:audio play doorbell.mp3 I can hear the ding dong on the PI(3) speaker. So I think the rule is simply not triggering.

I checked /var/log/openhab2/openhab.log and /var/log/openhab2/events.log and neither are showing and useful info. This is all I get if I try changing the rules file:

2020-12-08 23:11:26.620 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model ‘doorbells.rules’
2020-12-08 23:11:27.960 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'doorbells.rules

I also noticed that when I went to link the Item channel to the thing in PaperUI, I go this in the logs:

2020-12-08 23:16:19.790 [INFO ] [st.core.internal.thing.ThingResource] - Received HTTP PUT request for update at ‘things/mqtt:topic:frontdoorbell’ for an unmanaged thing ‘mqtt:topic:frontdoorbell’.
2020-12-08 23:16:31.470 [INFO ] [st.core.internal.thing.ThingResource] - Received HTTP PUT request for update at ‘things/mqtt:topic:frontdoorbell’ for an unmanaged thing ‘mqtt:topic:frontdoorbell’.

It’s telling me something (i.e. “unmanaged thing”), although not sure what exactly its telling me. Any thoughts? Why is OpenHab not triggering the rule and playing the sound when I press the button?

Your item channel dosent match the thing channel you create.

mqtt:topic:frontdoobell:frontdoorbell

Alright, so I change the doorbells.items to the following:

Switch frontdoorbell “Front Door Bell” {channel=“mqtt:topic:frontdoorbell:frontdoorbell”}

Still no luck! Or am I misunderstanding what you are saying?

BTW, I also did a sudo systemctl stop openhab2 and then sudo systemctl start openhab2 just to make sure OH2 started fresh.

Yes that is what I ment.

When you save your things file is there any complaint in the log files?

I did a test be removing the h from switch:

2020-12-09 08:57:47.097 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model ‘mqtt.things’
2020-12-09 08:57:47.196 [INFO ] [.transport.mqtt.MqttBrokerConnection] - Starting MQTT broker connection to ‘192.168.2.2’ with clientid 475fa111-c871-XXXX-XXXX-XXXXXXXX
2020-12-09 08:57:47.212 [ERROR] [.thing.internal.GenericThingProvider] - Channel type mqtt:switc could not be resolved.

Then corrected it:

2020-12-09 08:59:23.682 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model ‘mqtt.things’
2020-12-09 08:59:23.746 [INFO ] [.transport.mqtt.MqttBrokerConnection] - Starting MQTT broker connection to ‘192.168.70.2’ with clientid 475fa111-c871-XXXX-XXXX-XXXXXXXX

So no errors or complaints at all. Is there a way beyond the log to debug rules and understand whether they are being triggered?

Use the logging in rules so

rule "Play sound when someone rings Front Door Bell"
when
    Item frontdoorbell received command ON
then
    logInfo("Door", "The Ding Dong Rule worked")
    playSound("doorbell.mp3")
end

Tried that:

2020-12-09 09:47:49.782 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model ‘doorbells.rules’
2020-12-09 09:47:51.159 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model ‘doorbells.rules’

No other input even though I pressed the button several times and the broker is seeing these messages coming through. Its as if the bloody rule engine is not working or something like that.

There is a another annoying thing in the logs that have been bugging me for a while but I think it is unrelated:

2020-12-09 09:50:46.303 [ERROR] [core.karaf.internal.FeatureInstaller] - Failed installing 'openhab-binding-networkupstools1

The binding was uninstalled weeks ago; I refreshed the cache, start/stopped several times but this has stopped this error message. I’ll post separately for this issue. I just want to get my doorbell working :slight_smile:

Is the state change of the item showing in the event log?

mmm, maybe there is something here:

$cat events.log | grep door

give me this:

020-12-09 08:57:45.764 [hingStatusInfoChangedEvent] - ‘mqtt:topic:frontdoorbell’ changed from INITIALIZING to OFFLINE (CONFIGURATION_ERROR): Remove and recreate: mqtt:topic:frontdoorbell:frontdoorbell
2020-12-09 08:57:47.240 [hingStatusInfoChangedEvent] - ‘mqtt:topic:frontdoorbell’ changed from OFFLINE (CONFIGURATION_ERROR): Remove and recreate: mqtt:topic:frontdoorbell:frontdoorbell to OFFLINE (BRIDGE_OFFLINE)
2020-12-09 08:57:47.264 [hingStatusInfoChangedEvent] - ‘mqtt:topic:frontdoorbell’ changed from OFFLINE (BRIDGE_OFFLINE) to ONLINE
2020-12-09 08:59:22.072 [hingStatusInfoChangedEvent] - ‘mqtt:topic:frontdoorbell’ changed from ONLINE to UNINITIALIZED
2020-12-09 08:59:22.095 [hingStatusInfoChangedEvent] - ‘mqtt:topic:frontdoorbell’ changed from UNINITIALIZED to UNINITIALIZED (HANDLER_MISSING_ERROR)
2020-12-09 08:59:22.415 [hingStatusInfoChangedEvent] - ‘mqtt:topic:frontdoorbell’ changed from UNINITIALIZED to INITIALIZING
2020-12-09 08:59:22.436 [hingStatusInfoChangedEvent] - ‘mqtt:topic:frontdoorbell’ changed from INITIALIZING to ONLINE
2020-12-09 08:59:23.777 [hingStatusInfoChangedEvent] - ‘mqtt:topic:frontdoorbell’ changed from ONLINE to OFFLINE (BRIDGE_OFFLINE)
2020-12-09 08:59:23.822 [hingStatusInfoChangedEvent] - ‘mqtt:topic:frontdoorbell’ changed from OFFLINE (BRIDGE_OFFLINE) to ONLINE

Interesting to see that there is nothing in the last 1hr 20mins suggesting that the BRIDGE_OFFLINE may be the issue? Why would I have to recreate the mqtt topic as it is suggesting?

What is the name of your mqtt Bridge ?

So I wonder whether this may the the problem … the mqtt.things file is such:

Bridge mqtt:broker:mymqtt “My MQTT Broker: Mosquitto”
[
host=“192.168.20.2”,
port=1883,
secure=“AUTO”,
username=“myuser”,
password=“mypassword”
]
{
Thing mqtt:topic:frontdoorbell “Front Door Bell” {
Channels:
Type switch : frontdoorbell “Front Door Bell” [ stateTopic=“Home/Ground Floor/Front Door/Bell” ]
}

    Thing mqtt:topic:diningroom "Dining Room Air Sensor" {
    Channels:
            Type number : temperature "Dining Room Air Temperature" [ stateTopic="Home/Ground Floor/Dining Room/temperature"]
            Type number : humidity "Dining Room Air Humidity" [ stateTopic="Home/Ground Floor/Dining Room/humidity"]
            Type number : sensorbattery "Dining Room Sensor Battery" [ stateTopic="Home/Ground Floor/Dining Room/sensorbattery"]
    }

}

Is it because I’m calling my bridge mqtt:broker:mymqtt at the top and then referencing mqtt:topic:frontdoorbell instead of mqtt:broker:frontdoorbell?

If that is the problem, then I’m really confused as to why the Dining Room sensor is working.

Ok is that all your mqtt things as you have a couple of errors.

What editor are you using?

Change your thing file to

Bridge mqtt:broker:mymqtt "My MQTT Broker Mosquitto" [
host="192.168.20.2",
port=1883,
secure="AUTO",
username="myuser",
password="mypassword"
]{

   Thing topic frontdoorbell "Front Door Bell" {
   Channels:
      Type switch : frontdoorbell "Front Door Bell" [ stateTopic="Home/Ground Floor/Front Door/Bell" ]
 }
   Thing topic diningroom "Dining Room Air Sensor" {
   Channels:
      Type number : temperature "Dining Room Air Temperature" [ stateTopic="Home/Ground Floor/Dining Room/temperature"]
      Type number : humidity "Dining Room Air Humidity" [ stateTopic="Home/Ground Floor/Dining Room/humidity"]
      Type number : sensorbattery "Dining Room Sensor Battery" [ stateTopic="Home/Ground Floor/Dining Room/sensorbattery"]
    }
}

Items

Switch Frontdoorbell "Front door bell" {channel="mqtt:topic:mymqtt:frontdoorbell:frontdoorbell"}
Number   DiningRoomAirSensorTemperature     "Dining room air temperature"   {channel="mqtt:topic:mymqtt:diningroom:temperature"}
Number   DiningRoomAirSensorHumidity        "Dining room air humidity"      {channel="mqtt:topic:mymqtt:diningroom:humidity"}
Number   DiningRoomAirSensorSensorbattery   "Dining room sensor battery"    {channel="mqtt:topic:mymqtt:diningroom:sensorbattery"}

Before I make these changes, and not wanting to break my system which has working sensors that are persisting/recording data to SQLite, can I just ask what it is that you think is the cause of the problem. From what you have recommended, it looks like all the changes I need to make are in the items file.

Here is a sensor entry in the sensors.items:

// Sensor Items
Number GF_Dining_Temperature "Dining Room Air Temperature [%.1f °C]" <temperature> (gTemperatures, GF_Dining) {channel="mqtt:broker:topic:diningroom:temperature" }
Number GF_Dining_Humidity "Dining Room Air Humidity [%d %%]" <humidity> (gHumidities, GF_Dining) {channel="mqtt:broker:topic:diningroom:humidity" }
Number GF_Dining_Battery "Dining Room Air Sensor Battery [%d %%]" <battery> (gBatteries, GF_Dining) {channel="mqtt:broker:topic:diningroom:sensorbattery" }

and my doorbells.items file has:

Switch frontdoorbell "Front Door Bell" {channel="mqtt:topic:frontdoorbell:frontdoorbell"}

I think you are asking me to change the {channel="mqtt:broker:topic:diningroom:temperature" } bit to {channel="mqtt:topic:mymqtt:diningroom:temperature"}. So it looks like I need to change the mqtt:broker:mymqtt to mqtt:topic:mymqtt from what you say?

So I’ve spotted a difference between between my sensors config and my door bell config. I’ve noticed that the it’s missing the broker bit. So I’ve now change my doorbells.items to:

Switch frontdoorbell "Front Door Bell" {channel="mqtt:broker:topic:frontdoorbell:frontdoorbell"}

Not the events.log is showing the status change:

020-12-09 13:19:19.949 [hingStatusInfoChangedEvent] - 'mqtt:topic:frontdoorbell' changed from UNINITIALIZED to INITIALIZING
2020-12-09 13:19:20.137 [hingStatusInfoChangedEvent] - 'mqtt:topic:frontdoorbell' changed from INITIALIZING to ONLINE
2020-12-09 13:23:28.172 [vent.ItemStateChangedEvent] - frontdoorbell changed from NULL to ON
2020-12-09 13:23:30.172 [vent.ItemStateChangedEvent] - frontdoorbell changed from ON to OFF
2020-12-09 13:27:14.083 [vent.ItemStateChangedEvent] - frontdoorbell changed from OFF to ON
2020-12-09 13:27:16.083 [vent.ItemStateChangedEvent] - frontdoorbell changed from ON to OFF
2020-12-09 13:30:18.225 [vent.ItemStateChangedEvent] - frontdoorbell changed from OFF to ON
2020-12-09 13:30:20.227 [vent.ItemStateChangedEvent] - frontdoorbell changed from ON to OFF
2020-12-09 13:34:58.531 [vent.ItemStateChangedEvent] - frontdoorbell changed from OFF to ON
2020-12-09 13:35:00.529 [vent.ItemStateChangedEvent] - frontdoorbell changed from ON to OFF

but the ding dong is not going and there is nothing being logged in openhab.log even though I now have the logInfo() bit in the rule as you suggested. So this must be something with rules not being triggered although:

$cat openhab.log | grep door
...
2020-12-09 13:19:05.043 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'doorbells.rules'

What I have noticed is that after a stop/start of OH2, I’m getting this:

2020-12-09 13:19:15.753 [ERROR] [org.openhab.binding.mqtt] - bundle org.openhab.binding.mqtt:2.5.10     (234)[org.openhab.binding.mqtt.internal.discovery.MqttServiceDiscoveryService(280)] : The activate method has thrown an exception
java.lang.IllegalArgumentException: ID segment 'MQTT on demeter' contains invalid characters. Each segment of the ID must match the pattern [A-Za-z0-9_-]*.
    at org.eclipse.smarthome.core.common.AbstractUID.validateSegment(AbstractUID.java:97) ~[?:?]
    at org.eclipse.smarthome.core.common.AbstractUID.<init>(AbstractUID.java:75) ~[?:?]
    at org.eclipse.smarthome.core.common.AbstractUID.<init>(AbstractUID.java:58) ~[?:?]

Don’t change it if its working.

The error may be because you are using the embeded broker but it looks like its working now.

frontdoorbell changed from OFF to ON

This should trigger the rule

rule "Play sound when someone rings Front Door Bell"
when
    Item frontdoorbell received command ON
then
    logInfo("Door", "The Ding Dong Rule worked")
    playSound("doorbell.mp3")
end

I think maybe something is not quite happy. Maybe the SD card I not sure
Do a Backup of your system.

I would clear the openHAB cache

in SSH

sudo systemctl stop openhab2.service

sudo openhab-cli clean-cache

sudo systemctl start openhab2.service 

Wait a while 

sudo systemctl restart openhab2.service

I’ve tried emptying the cache at least 4 times today :frowning:
Bit disappointing that this just wont’ work. Its a simple setup. All I have are a few sensors.

Also getting this HANDLER_MISSING_ERROR every now and then. Not sure what it means.

2020-12-09 22:12:38.108 [vent.ItemStateChangedEvent] - frontdoorbell changed from NULL to ON
2020-12-09 22:12:40.100 [vent.ItemStateChangedEvent] - frontdoorbell changed from ON to OFF
2020-12-09 22:23:58.584 [hingStatusInfoChangedEvent] - ‘mqtt:topic:frontdoorbell’ changed from ONLINE to UNINITIALIZED
2020-12-09 22:23:58.607 [hingStatusInfoChangedEvent] - ‘mqtt:topic:frontdoorbell’ changed from UNINITIALIZED to ONLINE
2020-12-09 22:23:58.674 [hingStatusInfoChangedEvent] - ‘mqtt:topic:frontdoorbell’ changed from ONLINE to UNINITIALIZED (HANDLER_MISSING_ERROR)
2020-12-09 22:24:09.816 [hingStatusInfoChangedEvent] - ‘mqtt:topic:frontdoorbell’ changed from UNINITIALIZED (HANDLER_MISSING_ERROR) to INITIALIZING
2020-12-09 22:24:09.954 [hingStatusInfoChangedEvent] - ‘mqtt:topic:frontdoorbell’ changed from INITIALIZING to ONLINE
2020-12-09 23:08:18.621 [vent.ItemStateChangedEvent] - frontdoorbell changed from OFF to ON
2020-12-09 23:08:20.620 [vent.ItemStateChangedEvent] - frontdoorbell changed from ON to OFF

denominator, you were right. The setup shoudl have been working correctly. I followed these steps:

  1. Made an updated backup (zip file)
  2. Installed a new system on a new SDCard.
  3. Ran all necessary updates with sudo openhabian-config and downloaded all necessary additional packages like Mosquitto.
  4. Rebooted
  5. Restored the zip file backup and rebooted …
  6. … and bingo, the bell rings (although only on HDMI and smarthome:audio sinks is not showing any sink at all but I’ll post a different thread for that).

Thanks for all your help. I haven’t checked my previous SDCard yet but suspect its faulty. I need to set up SDCard cloning soon to avoid this issue in the future.