ESP8266 MQTT Lock Using JSON for Home Assistant
This project adds an easy way to create DIY lock for Home Assistant, an amazing, extensible, open-source home automation system.
Installation/Configuration
To set this system up, you need to configure the MQTT lock component in Home Assistant and set up a lock to control. This guide assumes that you already have Home Assistant set up and running. If not, see the installation guides here.
The Home Assistant Side
-
In your
configuration.yaml
, add the following, depending on the supported features of the lock:- platform: mqtt name: frontdoor state_topic: "home-assistant/frontdoor" command_topic: "home-assistant/frontdoor/set" optimistic: false qos: 0 retain: true value_template: '{{ value.x }}'
-
Set the
name
,state_topic
, andcommand_topic
to values that make sense for you. -
Restart Home Assistant. Depending on how you installed it, the process differs. For a Raspberry Pi All-in-One install, use
sudo systemctl restart home-assistant.service
(or just restart the Pi).
The Lock Side
I'm using ESP8266-01 microcontrollers for my locks because they are so cheap and small. The downside of the size and price is that programming them can be a bit of a hassle. There are many sites that go into detail, so I won't do it here. You'll need an ESP set up to work with the Arduino IDE. See the readme here for instructions. Another good device to work with is the Wemos D1 Mini, which has a built-in micro-USB port and is much easier to program.
- Using the Library Manager in the Arduino IDE, install PubSubClient. You can find the Library Manager in the "Sketch" menu under "Include Library" -> "Manage Libraries..."
- Open the
mqtt_esp8266_light
project in the Arduino IDE. - Update the
config-sample.h
file with your settings for Lock type, pin numbers, WiFi settings, and MQTT settings. - Ensure that the
CONFIG_MQTT_CLIENT_ID
setting is a unique value for your network. - Set
CONFIG_MQTT_TOPIC_STATE
andCONFIG_MQTT_TOPIC_SET
to match the values you put in yourconfiguration.yaml
. - Save the configuration file as
config.h
. - Open the
.ino
file in the Arduino IDE and upload to an ESP with the correct connections.