This repo can be used to control a Dockerpi relay hat for the Raspberry Pi over MQTT, but it might work for other I2C-type relays.
To get started, even before this repo, check this nice tutorial page.
- Fill in the
config.yaml
with your settings - The script listens on a predefined MQTT topic (
command_topic
) and forwards the requests to the relays on I2C protocol. - At the same time, if a relay state is changed, it updates the
state_topic
- Configure it as a service for autostart ⚙️
- Enable HomeAssistant discovery 🏡
- Change the state of the relays based on MQTT messages
- Configurable settings for MQTT and I2C addresses
- HomeAssistant discovery
- Online state monitoring on MQTT -> could be enhanced
- Configurable service template for autostart on boot -> could be enhanced
- Last will messages on MQTT -> tbd
Are you interested? Cool, let's go:
- Clone the repo ->
git clone https://github.com/Andoramb/i2c_2_mqtt.git
- Install prerequisites with
pip install -r requirements.txt
- Fill in
config.yaml
, according to your needs (explanation in the file itself) - Run
python3 mqtt2i2c.py config.yaml
and check the output.- Upon succsessful connect, it should output
Connected with result code 0
👍 - The state topic should be
online
✅
- Upon succsessful connect, it should output
- Check MQTT so the states are reported:
- Possible values for toggling to ON state: "on", "1", "true", "ON", "TRUE", "True"
- Possible values for toggling to OFF state: "off", "0", "false", "OFF", "FALSE", "False"
- However, the app will report
"on"
or"off"
string only 💡
- Optional: HomeAsisstant should register the relay based on
device_name
from config.yaml 🏡 - Optional: copy the service template to
/etc/systemd/system
for autostart- Replace
username
and/path/to/i2c_2_mqtt/
with your data ⚙️ sudo systemctl daemon-reload
sudo systemctl enable i2c_2_mqtt.service
- 👍
- Replace
Not sure yet, seems to be working 🤷♂️