Adjustable brightness multi-light MQTT 'thing'

I was asking for some assistance for how to get part of this working with a HA automation, so I thought I should share this project here for context, and also to share it. Where to begin… (this may take a while)…

I got a bunch of 12v 50cm ‘under kitchen counter’ LED light strips from AliExpress, and thought I’d put five of them together to light one of the rooms of the house, as they are exceptionally bright, and are a pleasant neutral white colour. Instead of just wiring them up to a 12v power supply, and a switch ('cause that would be so simple, I naturally had to work out how to control them via an ESP8266, make them adjustable brightness, and controllable via Home Assistant. Thus the need for some MOSFETs to do the power switching, and a PCA9685 to control the brightness of the individual lights/channels. Neither the MOSFET module and PCA9685 are fully utilised, as there are 8 free channels on the PCA9685 and three unused mosfets on the second MOSFET module, but this just means there’s room for more stuff to be added :wink:

Thus, the following were needed for the project

  • 5x 12VDC 50cm Cabinet Light (2835 120led 4000K) - Link
  • 2x 4 Channel MOSFET module - Link
  • 1x 16 Channel 12-bit PWM 2C interface PCA9685 module - Link
  • Various wire, 12V PSU, etc

Basically, there is a light going to each channel of the MOSFET module, and because there are five lights, I’m using two modules. I could have just as easily used a single MOSFET board to do that last light, since I’m using the PCA9685 to do the actual PWM brightness control, but since it has 16 channels, I might as well make use of it at a later time. I like the PCA9685 for this sort of job because it works over I2C, and can control 16 channels all by itself, leaving the ESP8266 free to mainly just do WiFi and control stuff.

The code of this is nothing special, and I’ll put it up on github shortly, after I’ve finished prettifying it and repeating myself less in the code. I feel the only ‘novel’ bit in the code is that it leverages HA’s MQTT discovery to make itself visible to HA, rather than having to manually configure it.

To control this outside of HA, I’m trying a Sonoff RM433 RF Remote Controller, which has eight buttons. In this scenario, I’m using one for the master enable of all the lights, five to control individual lights, and two for global brightness up and down. I have a Sonoff RF Bridge 433 RF Bridge running Tasmota and am using NodeRed to grab the button presses and publish them to MQTT. Thus, I wanted to use an automation to tell HA what to do with the buttons when they were pressed.

I have only done basic automation stuff before, and this was stumping me, as I could get HA to detect the button presses (MQTT events), but didn’t have the foggiest how to make it toggle the light related to the keypress, let alone control brightness. After some searching, and trawling of HA forum posts, I stumbled upon someone with a vaguely similar issue to my stumbling block… how to use MQTT messages/topics as triggers. Even better, it was only a month old, so wasn’t a stale topic, so I promptly threw my question into the ring, and Taras was kind enough to create an all in one automation that does everything I wanted (and even better yet, explain how it worked).

TODO:

  • Pictures (once it’s not a spaghetti monsters ratsnest of wires hanging precariously from the ceiling)
  • Code
  • Less rambling?