UPDATE 20 Mar 2021 - Added Docker container option in the Github repository.
The SwitchBot integration for Home Assistant does not support SwitchBot Meter Temperature & Humidity Sensors. sbm2mqtt is an easy way to integrate your Meters with Home Assistant using MQTT instead.
sbm2mqtt scans for BLE advertisements from Switchbot Meters, grabs temperature, humidity & battery information from them and then publishes the data to an MQTT topic for easy integration with Home Assistant.
sbm2mqtt cannot be run using the python_script component or under non-“Core” Home Assistant (was Hassio), because it requires extra Python libraries to be imported. Instead run it on a separate pi or similar, or in a separate container.
sbm2mqtt works like this:
Scan for Bluetooth Low Energy devices, looking for SwitchBot Meters.
For each SwitchBot Meter, grab the MAC address, temperature, humidity and battery level from the BLE advertisement.
Publish the temperature, humidity and battery level for each SwitchBot Meter separately to an MQTT topic that includes the MAC address.
Print the values to the terminal (if not running in the background) and save them to a log.
Based on your configuration of Home Assistant with MQTT sensors for the sbm2mqtt MQTT topics, HA updates the states of those sensors with every new MQTT message.
I am planning to purchase the Switchbot Meter Temp & Humidity from Amazon. Looking at the installation, I am confuse as how HA will read the temperature data from another rpi device that is running the sbm2mqtt.py script?
What details do I input here? The ip address and mqtt user credential where Hassio is installed?
You need to install mosquitto broker on the rpi and then put the rpi as host, 1883 as port (mosquitto user / passwd if used) and then set up an MQTT integration in HA and it will auto appear within minute or two. Hass.io should come with it’s own MQTT broker addon iirc.
I have setup the switchbox meter device with success. On the second rpi, I only had to run these lines as stated in the github. I didn’t have to install mosquitto broker on this rpi.
Many thanks for the docker container, I previously tried to make one myself but got in way over my head. Here’s a docker-compose.yml I use the image with after building it.
I haven’t tried it, but I think that you could add the Docker container on HA Blue using, for example, the Portainer add-on.
If you’ve got a spare Pi Zero W lying around, it’s simpler to dedicate that to sbm2mqtt with the added bonus that you can locate it near the Switchbot Meters if they are far from your HA Blue box.
Just did the pull req, also have an issue concerning timezone, the container reports the time hour off as opposed to running locally. I think the container is in UTC while I’m in GMT.
Thanks for the reply and the idea. It seems like an interesting project. But it also seems like too much work to integrate a simple device into Home Assistant when so many other devices can easily do the same thing.
I’m not savvy enough to do it myself, but I cannot help but wonder why someone isn’t working on an integration for all of their products and hub.
Switchbot now has a cloud API for the Hub for which it should be possible to error write an integration for Home Assistant. I’m not aware of any such project, though. The API didn’t exist when I wrote sbm2mqtt–and of course you have to be OK with dependency on the cloud.
There is another project for local control of Switchbot devices including Meters that uses an ESP32 board instead of a Pi. You might want to check it out.
If anyone’s interested I built the docker image for Pi (docker pullghcr.io/hugalafutro/sb2mqtt:latest). I built it on Pi Zero, but I tested it on Rpi3 too and it works ok there too.
I’ve set this up on a pi zero, but in my mosquitto log on HA it’s filled with
1623190208: New connection from 192.168.*.* on port 1883.
1623190208: New client connected from 192.168.*.* as sbm2mqtt (p2, c1, k60, u'sbm').
1623190208: Socket error on client sbm2mqtt, disconnecting.
Almost everything I search points to an authentication error, but I have tried a lot, including creating a local mqtt user inside the mosquitto config, or using HA users. I’ve double checked and tripple checked to make sure the usernames and passwords all match. Not sure what else I can try (I also got the same error when trying to run it locally on a HA Blue via portainer, and when running it in a docker on unraid)