Tech News
← Back to articles

My Lights Run on Bash – Tomasz Kramkowski

read original related products more articles

My Lights Run on Bash

Among the many hobbies that modern nerds pick up, one has recently become incredibly popular: making everything in your house dangerously unreliable by inserting a bunch of software where previously simple wires had sufficed. In this post, I describe how I replaced the wires between my lights and my light switches with some Bash.

Bash as critical infrastructure? Am I alright? Do I need to see a psychiatrist? These are all good questions, but let's leave them for another post.

To start with, some background. I liked the idea of smart lights, not for random colours, but for the ability to control them remotely, to dim them, and to change their colour temperature. I had the dream of lights that would change in temperature over the course of the day, so that during winter you could get more daylight-feeling light for the duration of waking hours.

The first choice on this journey was the hardware, but that's not the focus of this post. In short, I went with Zigbee. This meant I needed software that could interact with a Zigbee coordinator and expose some kind of API. For this purpose, I begrudgingly picked Zigbee2MQTT. I had considered writing my own interface, but upon investigation, it became clear that this wouldn't be a very easy task.

Zigbee2MQTT manages the Zigbee coordinator hardware and exposes your Zigbee network over the MQTT protocol.

So, now I had my Zigbee devices exposed over MQTT. This was already powerful, as I could now use mosquitto_pub to turn things on and off in my house. But the problem was that I couldn't react to events in complicated ways.

Most people would at this point install some off-the-shelf home automation software on their personal Kubernetes cluster. But I don't have a Kubernetes cluster, and if your software's installation page starts with two options, one being an entire OS and the other a container, forgive me if I get the strong urge to try something else first.

I spent some time brainstorming ideas, including visions of a simple web application to manage Lua scripts. But I kept digging away at layers of complexity, and questioning the need for certain features, until I realised that all I really needed was an easy way to launch arbitrary programs in response to MQTT messages. This could have been done with mosquitto_sub and Bash, but for a number of reasons that would have been somewhat tricky and potentially unreliable. Instead, I wrote a small program.

The project is called MQTTR for MQTT router (boring name, I know). It's the backbone of the operation. You can read a bit more about it on its project page.

... continue reading