I didn’t directly make this for HomeAssistant but I figure I’d share it here since I didn’t know a better place to share home automation stuff like this. But you can add it into home assistant.
I recently inherited a Govee RGBIC LED Strip (Govee H617C - https://www.amazon.com/gp/product/B099S9DXT7/) and I wanted to control it with Alexa through Node-RED,
I tried looking for any available hubs that would convert wifi signals to bluetooth signals but the only one I came across didn’t work with Govee Lights.(https://www.amazon.com/MOES-Fingerbot-Compatible-Multiple-Fingerbots/dp/B09TVX3QX3).
So I decided to make my own.
I didn’t have a lot of background knowledge on how BLE devices communicate so I had to do some research. I’ll share some of the resources that helped me with this endeavor.
Prerequisites:
- Working Node-RED installation
- Working Mosquitto installation
- Mqtt-launcher (GitHub - jpmens/mqtt-launcher: Execute shell commands triggered by published MQTT messages) installed on the hub device you choose
- A cheap device capable of connecting to bluetooth low energy devices to use as a dedicated hub. A Raspberry Pi will work or in my case I had an Intel Compute Stick STCK1A8LFC laying around doing nothing and I installed xubuntu on it.
On your device of choice you will want to create a directory in your home dictortory to store the control scripts.
Create the following files and chmod them to be executable. Except the text file which should be readable and writable.
~/Documents/bluetooth
- keepalive.sh
- writelightsoff.sh
- writelightson.sh
- lights.txt
The contents of each file are below.
keepalive.sh - Change XX:XX:XX:XX:XX:XX to the Mac Address of your govee lights.
#!/bin/bash
reapeat=true
value="aa010000000000000000000000000000000000ab"
alive=$(gatttool -i hci0 -t random -b XX:XX:XX:XX:XX:XX --char-write-req -a 0x0014 -n $value)
while [ "$reapeat" = true ]
do
# device connected, keep alive and wait for on off command
while [ "$alive" = "Characteristic value was written successfully" ]
do
# check if lights.txt has content
if [ -s lights.txt ]
then
# read value
value=$(<lights.txt)
# make file blank
>lights.txt
else
value="aa010000000000000000000000000000000000ab"
fi
# excute command
alive=$(gatttool -i hci0 -t random -b XX:XX:XX:XX:XX:XX --char-write-req -a 0x0014 -n $value)
value="aa010000000000000000000000000000000000ab"
sleep 1
done
# keep trying to connect to device
alive=$(gatttool -i hci0 -t random -b XX:XX:XX:XX:XX:XX --char-write-req -a 0x0014 -n $value)
done
writelightsoff.sh - Be sure to change the path to point to your appropriate directory.
#!/bin/bash
echo "3301000000000000000000000000000000000032" > /home/CHANGEME/Documents/bluetooth//lights.txt
writelightson.sh - Be sure to change the path to point to your appropriate directory.
#!/bin/bash
echo "3301010000000000000000000000000000000033" > /home/CHANGEME/Documents/bluetooth//lights.txt
lights.txt - Should be empty, don’t forget to make it readable and writable.
At this point you can test if it’s working for you. You can run the script keepalive.sh then run the script writelightsoff.sh to turn off the light and writelightson.sh to turn on the light. There are other commands you can send to do other stuff as well like change color and brightness etc but for me all I wanted to do was to turn it off and on via Alexa.
Next comes the service bit. This will start the keepalive.sh automatically.
We need to create this file
sudo nano /lib/systemd/system/govee.service
And copy paste in the following (BE SURE TO CHANGE THE PATHS TO THE SCRIPTS):
[Unit]
Description=goove service
After=display-manager.service network.target syslog.target
[Service]
Type=simple
ExecStart=/home/CHANGEME/Documents/bluetooth/keepalive.sh
WorkingDirectory=/home/CHANGEME/Documents/bluetooth
Restart=on-failure
[Install]
WantedBy=multi-user.target
Then run:
systemctl daemon-reload
systemctl enable govee.service
systemctl start govee.service
Once you have mqtt-launcher setup and working. You want your launcher.conf file to look like this.
launcher.conf - Make sure to change the Mosquitto login info and PATHS as well. You can create a service to run mqtt-launcher.py in the background or run “nohup /path/to/mqtt-launcher.py &”
logfile = 'logfile'
mqtt_broker = 'CHANGEME' # default: 'localhost'. If using TLS, this must be set to the domain name signed by your TLS certificate.
mqtt_port = 1883 # default: 1883
mqtt_clientid = 'govee-mqtt'
mqtt_username = CHANGEME
mqtt_password = CHANGEME
mqtt_tls = None # default: No TLS
mqtt_tls_verify = None # Configure verification of the server hostname in the server certificate, None means to not vorifying Hostname and should not be used in production
mqtt_transport_type = 'tcp' # alternative: 'websocket', default: 'tcp'
topiclist = {
# topic payload value program & arguments
"govee/power" : {
'off' : [ '/home/CHANGEME/Documents/bluetooth/writelightsoff.sh' ],
'on' : [ '/home/CHANGEME/Documents/bluetooth/writelightson.sh' ],
},
}
At this point you can test that your mqtt messages can control you govee light by sending an “off” or “on” message payload to the topic “govee/power”
Everything else from now on is setting up Node-RED to send Mqtt commands and install the Alexa plugin.
The Alexa plugin for Node-RED I use is node-red-contrib-amazon-echo (node-red-contrib-amazon-echo (node) - Node-RED). There are many posts and tutorials on how to set this up so I won’t go into details. I will say that for my specific case where I have my Alexa devices on a separate IOT vlan, I had to put one of the alexa devices on the same vlan as my node-red install in order to discover the device (which I called chair in the image below) and then put it back on the IOT network.
The payload message from the “chair” device is already either on or off depending on your command to Alexa so just pipe that into a node-red mqtt-out network node that send messages over “govee/power” and you are all set.
You can rename your device and set up routines from within the Alexa app.
If you have any questions I’ll try my best to respond.