The goal

I put up IKEA closet in a bedroom in my apartment together with IKEA smart lights system. To those who are not aware, IKEA offers an ecosystem of different smart devices communicating over Zigbee protocol. I got 6 off these which are driven by Tradfri power supply. Then there's a remote that you can pair with the PSU. 

It all works smoothly and reliably, but there's one feature that I believe is a huge drawback in this particular use case. There's no way to turn them on when doors of the closet open. And that's the main purpose of the lights installed on a closet. Every time you open the door you need to remember where you put the remote, go there, press the button and go back. Yes, you can stick the remote to the side of a closet, but it doesn't help that much. 

So my first thought was to cut the cable (it's 24V), install magnetic door switch, ditch the remote and move on with my life. But here comes my wife - she already got used to the remote and the possibility to control these lamps while she's in bed.

What don't you do for love? 

How to do it?

Ideally, there would be an input in the PSU that I could connect some kind of switch to. But I don't want to hack any device connected to mains. And not that I have any skills to hack it anyway.  And even if I knew how to, there's still a matter of firmware. So I had to find some other way.

Hijack communication

As I mentioned at the beginning, these lights use Zigbee to communicate with the remote. Turns out people have already managed to hijack this communication and get it to work. There's zigbee2mqtt project which lets you translate zigbee protocol into more manageable MQTT (and back). Of course then you need some MQTT server running as well. Luckily, I already had a small Raspberry Pi automation server running in my apartment.

Except I didn't have any Zigbee adapter in it. Thanks to great step-by-step description I bought all I needed, flashed it and had it up and running. You can find all the necessary information about supported devices, pairing etc. on the projects website. There's also this neat trick to use groups for pairing the remote directly with the PSU while still hijacking communication, which saves you from implementing remote on the server side and prevents unnecessary lag. I won't go into details here, but you can find it all there.

The last problem was that communication with the server wasn't very reliable, because of thick walls in my apartment and the distance. So I bought one of IKEA smart plugs, which act as Zigbee repeaters, and plugged it in midway.

So now I was at the point where I could control my lights either from the remote or from my home automation server. Neat!

Door events

To be able to put this to use I needed some way to report door opened/closed events to the server. 

Door open state detection is done by using magnetic door switches. I used 3 of these - one for each closet piece, installed on the right door wing. They are NC type and connected in series, so opening any of the 3 door will break the circuit and generate an event as a result.

To make it super simple and cheap I chose ESP01 module together with USB add-on, which you can buy together for $2-3 on AliExpress or eBay. I installed Tasmota firmware and configured it to connect to my MQTT server. Switches are connected to GPIO-02 that is exposed on the module and Tasmota lets you choose it as a switch.

The only thing left was how to power the module. I already wrote that I bought it together with USB add-on. This can be used to power the module using an old phone charger. But where do I plug in the charger on top of the closet, where my module is? Turns out Tradfi PSU has a power output for daisy-chaining multiple PSUs to one plug. And they sell a cable for it. I cut it at one end and attached a power outlet instead. This allowed me to plug in my phone charger with ESP module without...

Read more »