Creating a Soft-Remote with MQTT and Home Assistant

Technology is amazing and as much as we want to use our smart phones or ask Alexa, Google and Siri to take action, nothing beats a physical button. In almost all cases they are faster and it’s a good reason why I practice passive automation. Walk into a room and its dark, then the light will automatically turn on based on a message from the alarm system or if there is no power, the room will be energised. No need to fumble with technology.

My house has a few Phillips Hue Light Strips and standalone Phillips Hue Play Light Bars to provide lighting effects in my study. Whilst I can apply the same logic to these lights, they are infinitely more configurable with dimming levels and 65K colours.

After needing to buy a Hue Remote to reassign paired devices to a non-Phillips Zigbee Gateway (ConBee II) I have found the remote to be somewhat handy. The remote and its physical buttons can all be remapped with Deconz and you can get them to do this and that.


I have these remote buttons configured to turn off / on all 3 lights and provide dimming function. Pretty vanilla.

What I noticed is after a short period of time, I found my go-to for adjust of these lights was the physical remote and that my wife would be less hesitant to use Phillips Hue LED strip (top mounted) in her sewing room.

I want to provide her the same functionality that reduces the friction, but can I do this without adding to e-waste? I have draws of gadgets that today just aren’t used. FitBits replaced by Garmin’s, a new smart phone or two every 2 years and more Raspberry Pi’s than you can poke a stick at (I have a box, for just Pi’s). There is a cost to every device, not just monetary, but a carbon cost. The Phillips Hue costs $40, but what is it’s carbon cost? This can be calculated.

The Green Software Foundation define this cost as SCI or Software Carbon Intensity.

E = The energy used by the software, expressed in kilowatt hours
I = Location based carbon emissions, or grammes of carbon
M = Embodied emissions of software – The carbon emitted during the creation and disposal of a hardware device
R = A unit of measurement such as per API call, per transaction
O = Operational Emissions (E * I)


With this being said, I decided to give my wife the Phillips Hue remote and map it to the lights in the sewing room, but I wanted the same ease of use and functionality in my-study that I enjoyed with the physical device.

There is a better way, and I type this after implementing my idea (2 weeks have past), it is even more handy than the Phillips Hue Remote.

In this post I will share with you the process of how I created a soft-remote to manage my lighting
Let’s start by showing you this in action and then working backwards in my journey.


Technical Building Blocks
As Rumsfeld once said

“…there are known knowns; there are things we know we know. We also know there are known unknowns; that is to say we know there are some things we do not know. But there are also unknown unknowns — the ones we don’t know we don’t know.”

My knowns are

  • My office has a computer that is always on
  • I run HomeAssistant
  • Home Assistant has an ability to control my Hue devices via Conbee II and Deconz (integration)
  • Home Assistant has Automations (do this when this happens)
  • Mosquitto runs as a MQTT broker
  • Windows 11

What was unknown to me, was how can I get Windows to fire off an event when a key combination is pressed. What I wanted to emulate was media-keys on my keyboard. Press ‘Play’ to start music, press ‘Volume Up/Down’ to control volume but have keys to provide control to my lights.

The Solution Blocks
The solution is to connect the Lego blocks together. This is the representation of the end-2-end solution

WinHotKeys –> Mosquitto_Pub –> Mosquitto Broker –> Home Assistant (Automation) –> Deconz Integration –> Phillips Hue Lights

Generating The Short Cut Keys
The first problem to solve is how to have the short cut keys recognized. This post covers Windows 10/11 but the same principal applies to both OSX / Linux Operating Systems. In Windows I soon found (Google) there are many ways achieve from AutoHotKey through to Win HotKeys. I chose the latter purely because it simple, I mean really simple and as an MVP (Minimum Viable Product) I wanted to provide the theory without learning a scripting language (AutoHotKey).

Sending a Message
After configuring Win HotKeys with specific keys (Windows + Alt + PgUp|PgDwn) I need to tell Win HotKeys what to do.

WindHot Keys properties. Advanced will provide parameters for Mosquitto

We will be sending a MQTT message to our broker, to which Home Assistant will subscribe to (In the Automation). In order to send a message I am using ‘mosquitto_pub‘. ‘mosquitto_pub’ is a incredibly light weight, high performing command line tool to publish MQTT messages and is often used in MQTT development.


I crafted a string where ‘h’ is my MQTT host

Button     | Windows + Alt + PgUp
Executable | C:\Program Files\Mosquitto\mosquittao_pub.exe
Parameters | -h 10.0.0.200 -m study_on -t cmnd/hue/study

Button     | Windows + Alt + PgDwn
Executable | C:\Program Files\Mosquitto\mosquittao_pub.exe
Parameters | -h 10.0.0.200 -m study_off -t cmnd/hue/study

Validating a message
You can leverage a tool such as MQTT explorer or even Mosquitto_sub to subcribe to the MQTT topic you have chosen. Send a messaage and validate the message has arrived. In my example above, I am sending a message of “study_on” or “study_off” to cmnd/hue/study.

Home Assistant Automation
In Home Assistant we need to create an automation. This can be done via the GUI but this can be summarised in yaml.
In essence, we are subscribing to the MQTT topic ‘cmnd/hue/study’ and taking action on a payload of ‘study_off’ and ‘study_on’.

- id: '1654663732274'
  alias: Study (Hue) - Lights Dim|Off
  description: ''
  trigger:
  - platform: mqtt
    topic: cmnd/hue/study
    payload: study_off
  condition: []
  action:
  - device_id: f5ae9ec3b582046a29cc5487d3d9f722
    domain: light
    entity_id: light.study
    type: brightness_decrease
  mode: single
- id: '1654735214118'
  alias: Study (Hue) - Lights Bright|On
  description: ''
  trigger:
  - platform: mqtt
    topic: cmnd/hue/study
    payload: study_on
  condition: []
  action:
  - device_id: f5ae9ec3b582046a29cc5487d3d9f722
    domain: light
    entity_id: light.study
    type: brightness_increase
  mode: single


Testing
I would suggest testing in segments. Test the MQTT message generated by mosquitto_pub arrive at the MQTT broker by first using MQTT Explorer. Secondly, test the Home Assistant Automation works in Home Assistant. For the later you can navigate to the ‘Automation’ and trigger your Hot Key, this will invoke a ‘blue’ bar stating ‘triggered’.

Summary
Before you buy your next widget or doodad, ask yourself, do you really need it?
Sustainable software and hardware does not mean a reduction in functionality, lower performance, and high cost. Far from it. In this example I find being to adjust my lights, even when the device is locked to be far more convenient than looking where on my desk the Phillips Hue remote was.

There are many advantages to building sustainable applications. They are almost always cheaper, they are often more performant, they are often more resilient.

But the primary reason we need to as builders shift towards practicing Green Software Engineering is for sustainability, everything else is an added advantage.

This was for lighting, but how easy would it be to add short cut keys in to adjust the heater / cooler and then have a push notification be sent to every member in the house (that is home)? Pretty easy.

Think big, and in order to reduce waste, ask yourself, is there a more sustainable way to do this.

Shane

Leave a Comment