I had a conversation on Twitter recently that made me write this post, and so here we are.
How did my creative HomeKitting begin? It began with me having a Raspberry Pi B+, some relays and a set of remote sockets. This situation led me to the idea that I could modify the sets to be able to control my lights from anywhere in my room – no matter if I’d lay in my bed, sit on my desk or just have entered the room – in short: it came from my laziness.

So I began to search the interwebs for a solution to my problem and quickly I came across the open source project homebridge on GitHub. homebridge was created to make several devices support HomeKit, no matter if the vendor planned to make it support HomeKit or not. homebridge includes some kind of API which you can write plugins for if you know NodeJS. I’m not that much of a pro in JavaScript, as the greatest I’ve done with it was some dynamic webpage layout, but I’m quite good at improvising with existing plugins and configuring them according to my ideas.

Step one: basic setup.

The first, a rather basic project was, as I already said, wiring up my remote for my sockets to the Raspberry Pi’s relay card which I wanted to use to simulate pressing a button on the remote. Quite easy, isn’t it? Well… sort of.
As it was my first real project with the Raspberry, I needed to find out how I had to control the relays. If you’ve already done it, it’s either a relatively easy python script or some simple commands in a shell script. By now I’d prefer the latter because I hate python and shell is just so easy to use. 😅 At the time, I didn’t know about the shell method and found many examples for the python way on the web, so I applied these, and they worked quite fine. I also had to solder some cables to the right points on my remote that I’d be able to wire it up to the Pi at all. After some testing, I worked out how to do that and it was done quickly.
So far, so good. I was able to turn my sockets on and off from the command line – but how would I do this from HomeKit on my iPhone?
As I mentioned before there are many (and by many I mean a couple thousand) plugins for homebridge – more added daily – and of course there was the probably most simple plugin, but in this case also the most useful one: homebridge-script (by now I use homebridge-script2, but it’s mainly the same thing, forked one more time). It’s as simple as it sounds – one script to turn the device on and another to turn it off.

Step one in my smart home evolution was done!

Of course, when you work hard to get things going and after some time it’s still working fine you want to go on and be able to control even more devices with your phone (or your voice via HomePod in my case or even Alexa). Yes, homebridge supports Alexa (and I think Google Home) too! I tried to use it with Alexa once, and I was able to add most accessories, but I didn’t really use it, because the only Echo Dot we have is in our bathroom and I don’t need to control the devices in my room from there…

Step two: adding even more devices!

As you grow your experience, you also want to improve your smart home. Make it support more, make it more fun. So I did. I ordered some cheap RGB LED stripes on eBay from China, 15 m for about $17 I think, some controllers for the LEDs, these were about $8 per piece and some Wi-Fi relays to include even more lamps in my system which I got for about $6 each. If you know my room, maybe from my Instagram Stories (follow me: @dieser_niko 😝) or from anywhere else, you may know the LEDs are my main source of light, they are everywhere.
The controllers I used are running a specific smart home protocol for which a helpful person made a plugin to use it with homebridge. It’s called homebridge-magichome and doesn’t need a lot of configuration to work as expected.
You set a name for the device, enter the IP address (it should be a fixed IP or a static DHCP lease, so it doesn’t change randomly over time), configure the color mode and whether the stripe has a dedicated channel for pure white light (which most don’t) or not.
These controllers were the easy bit. The standalone Wi-Fi relays aren’t as easy to set up. They are made to phone home every few seconds, and they want you to control your lights via their Chinese server, which also happens to be offline at least twice a day. That’s unpleasant because every time the Chinese can’t properly run their server, you aren’t able to toggle your lights! Sigh. Alternative one: hack the firmware on your relay. That’s complicated because you need an extra adapter to access the device and you risk bricking it in your hacking attempt. Alternative two: simulate the Chinese server locally on your Pi and make the relay think it’s still phoning home. Sounds good? I thought so too. So I installed this server on my Pi, simulating the Chinese server (also available on GitHub by the way), configuring my relays to my server’s address and being able to control them even if my ISP decides to be too stupid to make my internet work. 😂😂
They can now be controlled via an HTTP GET request which you may do with the aforementioned script2 plugin, or you do it with a plugin dedicated to the job: homebridge-http-switch. You enter the on, off and status URLs and you are ready to go!
Step two was done and by that time I was able to control all my lights, including the LED stripes’ colors and even my fixed ceiling lamp, as well as two of my room’s outlets.

Step three: the devil’s in the details.

Now that almost all of my devices were controlled via homebridge, I was able to take a close look at the details and refine the functions of my devices. The first thing that has bothered me since ever was how my outlets were labeled: as a switch. It’s a tiny little detail, but I never liked it to have my outlets show up as a switch in the Home App. Sadly there was no option to change that, as Apple doesn’t enable you to change an accessory from a switch to an outlet and the script2 plugin doesn’t have that option either. Luckily I’m not the only one out there who was bothered by these limiting options, and someone created the (by now) beloved plugin homebridge-cmd4, which I guess is based on script2.
It has the option to create any kind of accessory supported by HomeKit, but it’s quite complicated to configure, mainly because the developer doesn’t give many detail-rich examples and it’s not only on script, off script, status script. This plugin uses one script with different arguments for on, off, speed and all other possible options for that type of accessory. Now, after I figured out how to create the accessories and how it tries to speak to them, I really love this plugin because you can do almost anything with it. No matter if your real accessory is a lamp, an outlet, a fan, a window sensor or any other kind of device – as long as you can speak to it via network or the GPIO pins of the Raspberry Pi, it will work.

My (currently) “newest” accessory, which got a kind of big overhaul from on/off to specific speed, is my fan. I now control my fan with cmd4, and depending on the speed percentage range, it turns on speed 1, 2 or 3. The controlling script always rounds the percentage to the corresponding value, so the right relay gets triggered. Sounds easy, doesn’t it? The process to get here wasn’t that easy.
I replaced the speed buttons in the fan’s stand with another relay module. This module is connected to a Raspberry Pi Zero W, which sits inside my fan as well. It is connected via Wi-Fi, gets its power from a USB power brick next to the fan, and runs its own homebridge instance. Now, if it receives a speed between 1% and 33%, it turns on speed 1 on the fan, if it receives 34% to 66%, it switches to speed 2, and from 67% to 100% it triggers speed 3. 0% speed or the off command will release all three relays.

What’s to come?

I’m planning to add a new window next to my bed. If everything goes right, I’m gonna add the shutter to HomeKit, no idea how I’m going to do this, but I’m sure I’ll get it done. Another idea would be to make my system redundant. If homebridge on one Pi crashes another one kicks in and takes the service for it… enough Pis would be there, but I don’t know how I should implement that… we’ll see what the future brings and maybe I’ll update then.

Until then, thanks for reading and see ya! ☺️


I'm Niko, 18-year-old dude from southern Germany. I'm working in IT at a company nearby. In my free time, I'm an Apple enthusiast and Smart Home fan as well as anything else that can take work from me ?

%d bloggers like this: