Soma blind control via MQTT

A few hours of installing and deleting NPM packages later, I’ve got it working on Debian Buster (10). The steps below are the ones I have determined to work on my hardware and software, your mileage may vary.

Step 1: Install the pre-requisites for bluetooth to work:
apt-get install bluetooth bluez libbluetooth-dev libudev-dev

Step 2: Download the correct version of node,js from NodeSource (I went with 8.9.4 x64 because it’s still supported as v8 was an LTSR release)

THIS DOES NOT WORK WITH NODE 10x

cd /tmp
wget https://deb.nodesource.com/node_8.x/pool/main/n/nodejs/nodejs_8.9.4-1nodesource1_amd64.deb

Step 3: install the aforementioned package:
sudo dpkg -i nodejs_8.9.4-1nodesource1_amd64.deb

Step 4: install soma control with the necessary elevation to install the package globally (for all users):
sudo npm install --unsafe-perm -g [email protected]
Note: I went with 1.4.0 as I couldn’t get 1.4.1 to work

Step 5: check soma control works:
somactrl

If you seen an error message here, best of luck. It took me ~6 hours of digging around and learning about npm to get this bastard working. I had the most trouble with bluetooth-hci-socket when installing 1.4.1. Installing with 1.4.0 got me past this issue.

Step 5: Set somactrl to connect to your MQTT server
somactrl --mqtt-url mqtt://192.168.1.X -u homeassistant -p pAs$w0Rd

Step 6: Watch as your devices turn up in the integrations page in HomeAssistant! :slight_smile: You should be seeing output similar to this to indicate successfully connecting to Soma blind motors:

  soma* No device names supplied, will stop scanning after 30 seconds +0ms
  soma* scanning for as many devices until timeout +4ms
  soma* discovered c6######### +129ms
  soma* discovered ef######### +87ms
  soma* stopping scan after timeout +30s

As for how to get somactrl to start when the server starts? I don’t know yet. I’m thinking about a cron job that executes the command above using cron’s @reboot timing. I’ll update my post when I have a working solution.

a few moments later
Autostarting Soma Control
Yep, crontab works!

Swap to an elevated user - you want this to run as root, otherwise you start encountering issues with some of the dependencies and whether they see your BT hardware as “authorised” or not.

Step 1: change directory to where you want to keep the script that the system is going to execute - I chose the home directory for the root account, which also hosts my backup scripts.
cd ~

Step 2: Use a text editor to create the script
nano soma-control.sh

Step 3: Add in the line you used to test somactrl in Step 5 in the section above:
somactrl --mqtt-url mqtt://192.168.1.X -u homeassistant -p pAs$w0Rd

Step 4: Save and close the file. Control+X usually does this.

Step 5: Make the file executable
chmod +x soma-control.sh

Step 6: Edit your crontab file to run the script soon after reboot
crontab -e
Add this at the bottom. Edit the sleep value to change how long after boot to wait to execute the script:
@reboot sleep 60 && /root/somactrl.sh

Step 7: Save and close the file. Control+X usually does this.

You’re done! :slight_smile:

Miscellaneous related stuff:

  • The bluetooth adapter I’m using is this one from Amazon UK, £11 at time of writing
  • After this you’ll want to mark nodejs to not get upgraded, or you’ll lose the ability to use soma-ctrl until it works with Node 10.x
    echo "nodejs hold" | sudo dpkg --set-selections
    or
    apt-mark hold nodejs
  • Support for bluetooth hardware varies between kernels. If you’re having difficulties after following the steps above, a kernel upgrade may help. It may also break other things. You know your system better than I do, exercise caution.

That seems strange as I don’t think there’s any difference between 1.4.0 and 1.4.1 for that particular dependency.
Haven’t been able to reproduce the same problem trying a fresh install of 1.4.1 --though that is on raspbian not Debian – was it similar to the issues recently reported here, out of interest?

In case you haven’t seen them already, there’s instructions for automatically starting somactrl with systemd here: Starting automatically with systemd · andersonshatch/soma-ctrl Wiki · GitHub

Yep, that’s me and one of the issues I had at 1.4.1. It really felt like I was going round in circles with some of the errors I was getting so after a while I started fresh on a seperate box. When things went screwy there too, I tried a clean start again but with 1.4.0 this time and things worked.

Admittedly my testing was not entirely rigorous or scientific. Lots of bumbling through errors and not being able to find X or Y in Z location. I have a monster of a headache now but my blinds are working :smiley:

All works so far except the move instruction in mqtt. Can someone please correct me?

e.g. …/move 50 or is it …/move -50 …

I have tried many variants in node red etc to no avail…

It should be homeassistant/cover/RISEnnn/move 50 for half-way open. 100 is fully open and 0 is fully closed. If you specify your own base topic, homeassistant may differ.

Used mosquitto_pub -d -t CompSue18/cover/RISEnnn/move -m 50. This worked. I was then attempting to use node red however I now know my mistake so will try tomorrow.
BTW many thanks for your excellent soma-ctrl.

1 Like

The system works well however I seem to be getting a lot of these errors:-

node -v = V8.11.1
npm -v = 1.4.21

(node:12212) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 characteristicsDiscover listeners added. Use emitter.setMaxListeners() to increase limit.

After a while all comms stops on the Raspberrypi B3+

Have moved the MQTT broker off this sbc.

What am I missing?

I am having troubles installing it on a raspberry pi zero W.

When i run somactrl i get the following error message:

module.js:545
throw err;
^
Error: Cannot find module ‘…/build/Release/binding.node’
at Function.Module._resolveFilename (module.js:543:15)
at Function.Module._load (module.js:470:25)
at Module.require (module.js:593:17)
at require (internal/module.js:11:18)
at Object. (/usr/local/lib/node_modules/soma-ctrl/node_modules/bluetooth-hci-socket/lib/native.js:3:15)
at Module._compile (module.js:649:30)
at Object.Module._extensions…js (module.js:660:10)
at Module.load (module.js:561:32)
at tryModuleLoad (module.js:501:12)
at Function.Module._load (module.js:493:3)

It’s been bugging me for almost a day and i can’t figure out what it is…

I have node version 9.9.0 en NPM version 6.10.1. Now i am trying to install noble but somehow it gives me a bunch of errors. I still continue trying to install it but in the mean time if anybody has any idea what it can be? All the help is highly appreciated!

Best regards

Hmm, maybe for some reason the right bindings for noble were not installed.
Can you uninstall and share the output from running the npm install again?

I managed to get it working by moving the modules by hand into the correct positions. It looks like the modules were installed in one place and somactrl into another. The bluetooth-hci-socket folder was missing a release folder and after I installed bluetooth-hci-socket manual plus read-sync it was all fine.

Now I have everything showing up in Hassio but the connection gets lost everytime i close the smartty/putty screen. Any advise on that?

ps: thanks for the awesome work!

Best regards

Nice, glad you got it installed properly.
There are instructions here on how to have it started with systemd: https://github.com/andersonshatch/soma-ctrl/wiki/Starting-automatically-with-systemd

I got it working including systemmd. I wrote a small instruction of what I did to fix the issue that somactrl couldn’t find the correct modules. I think during the installation of somactrl the node-modules folder is missing some dependencies. Anyhow, I made a small instruction list for the ones that might run into the same issue and don’t feel like banging their heads couple hours… :wink:

OS: Hassbian
Hardware: Raspberry Pi 3 Model B Plus Rev 1.3

Update OS
sudo apt-get update
sudo apt-get upgrade

Test bluetooth
bluetoothctl
power on
agent on
scan on

Install node 8.9.0
wget https://nodejs.org/dist/v8.9.0/node-v8.9.0-linux-armv6l.tar.gz
tar -xzf node-v8.9.0-linux-armv6l.tar.gz
cd node-v8.9.0-linux-armv6l/
sudo cp -R * /usr/local/

Install Noble
npm init -y
sudo apt install python-dev build-essential -y
sudo npm install noble --unsafe-perm
sudo ln -s /usr/bin/nodejs /usr/bin/node

Install soma ctrl
sudo npm install -g soma-ctrl --unsafe-perm

Installing extra modules
sudo npm install readline-sync
sudo sudo npm i yargs
sudo npm install mqtt --save
sudo npm install debug
sudo npm install express

Find the folder where node modules are installed, mine was in /home/pi/node_modules

copy
/home/pi/node_modules
to
/usr/local/lib/node_modules/soma-ctrl
with
sudo cp -avr /home/pi/node_modules /usr/local/lib/node_modules/soma-ctrl

Test somactrl
somactrl

you should see something like this:

Options:
–help Show help [boolean]
–version Show version number [boolean]
-t, --discovery-timeout Number of seconds to scan for devices for
(ignored if -e or explicit list of names is
etc…

That’s it, now you can follow the instruction to setup the parameters for somactrl and don’t forget to follow the instructions to automatically start somactrl!

1 Like

Weird that you had to install a bunch of stuff manually… npm install should’ve taken care of that.
Is that a reproducible problem? I try fresh installs and everything seems to get put into place with one step :thinking:

Yes it is reproducible for me, first I installed it on a raspberry pi zero (fresh image), because I was using HASSIO. With HASSIO i couldn’t install somactrl so that’s why i used the zero. Then I decided to use Hassbian instead so i can install somactrl on same raspberry as my home assistant is running. In both cases fresh OS images on the SD card gave me same problems.

Now I got it running but within 24 hours somactrl stops. When I check the status it says:

MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11

systemd[1]: Stopping somactrl…
systemd[1]: somactrl.service: Main process exited, code=killed, status=15/TERM
systemd[1]: somactrl.service: Succeeded.
systemd[1]: Stopped somactrl.

Any idea what this could be? I am not really a programmer in daily life just for hobby so I dont know much about it.

Hmm, if that happens to me I never notice I guess because systemd restarts the process.

Did you add the Restart=always line to your systemd script?

I looked into those leak messages a long while back and the majority of them are issues in noble itself, not soma-ctrl so not really in my control to fix.

I am having similar issues as well. I have it set up with systemd, but regularly I get the MaxListeners issue and my shades become unavailable in home assistant. To get around this I have an automation that when any of the shades become unavailable I send a shell command to the raspberry pi controlling them to restart the somactrl service. This seems to work, however the shades are still unreliable where they may take up to 5 or more minutes to respond to a MQTT call to open or close.

5 minutes!? Distance or noise maybe?

I don’t think its distance, I tried moving one of the blinds to a closer raspberry pi and I still got the massive lag time. 5 minutes is generous, sometimes it can be 30 to an hour. It could be noise - but i dont know how to eliminate that. my next attempt is to put somactrl on a pi zero and have it as close to the shades as possible.

Any chance this project will work with:

Not tried it as I don’t have one. If they used the same Bluetooth services and characteristics it may work, otherwise there may be some additional work to get these going (PRs welcome in that case!)