Hi everyone, I wanted to share my recent experience setting up an MQTT-controlled dimmable LED lamp for HomeKit. While the process was mostly smooth, I encountered a small hiccup that I managed to resolve, and I thought it might be helpful to document it here for others who might run into similar issues.
So, the setup: I built a simple dimmable LED lamp using an ESP32 module and wanted to integrate it with HomeKit. The lamp works perfectly when turning it on by adjusting the brightness, but I noticed an issue when trying to turn it off. Specifically, when I set the brightness to 0% in the Lovelace UI, the slider wouldn’t go all the way down to 0%—it would stop at 1%. As a result, the MQTT command to turn the lamp off wasn’t being sent. Instead, I saw messages arriving with a brightness value of 2% and the command topic set to “ON”.
This was a bit puzzling, but after spending a good amount of time troubleshooting, I realized the issue was with how the brightness values were being interpreted. The ESP32 was receiving a brightness value of 2% when the slider was set to 0%, which wasn’t the intended behavior. I needed the slider to send a brightness value of 0% and trigger the “OFF” command.
After some research and experimentation, I adjusted the MQTT configuration in my .yaml file to ensure that setting the brightness to 0% would correctly send the “OFF” command. Here’s the corrected configuration I ended up using:
yaml
light:
- platform: mqtt
name: “Warm Light”
state_topic: “living_room/light/state”
command_topic: “living_room/light”
brightness_state_topic: ‘living_room/warm_light/brightness’
brightness_command_topic: ‘living_room/warm_light/brightness’
payload_on: “ON”
payload_off: “OFF”
optimistic: true
With this adjustment, the lamp now turns off correctly when the slider is set to 0%, and the “OFF” command is properly sent. I’m really happy with how this turned out, and it’s been working smoothly ever since!
If anyone else is working on similar projects or runs into similar issues, I’d love to hear about your experiences or any tips you might have. It’s always great to learn from others in the community. Thanks for reading, and happy tinkering! ![]()