Linux service to collect and transfer Xiaomi Mi BLE sensor data via MQTT, with mass-cluster support. Mirrored from GitHub https://github.com/outlyer-net/misensor-mqtt-daemon
Go to file
Thomas Dietrich 5c1dc642af Replace demo.gif by updated version 2017-08-08 20:46:43 +02:00
.gitignore Clean up README and config 2017-08-02 14:41:45 +02:00
AUTHORS Add functionality, improve error handling 2017-07-25 18:26:08 +02:00
LICENSE Added README and LICENSE 2017-02-09 21:18:45 +01:00
README.md Remove warning about skipping plants to from README 2017-08-08 15:48:18 +02:00
config.ini Clean up README and config 2017-08-02 14:41:45 +02:00
demo.gif Replace demo.gif by updated version 2017-08-08 20:46:43 +02:00
miflora-mqtt-daemon.py Add stats for success rate of sensor pollers 2017-08-08 18:02:20 +02:00
requirements.txt Add Mi params metadata, improve OH function 2017-08-02 02:11:43 +02:00
template.service Add bluez and remove syslog as requirement 2017-07-27 01:42:42 +02:00

README.md

Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon

A simple Linux python script to query arbitrary Mi Flora plant sensor devices and send the data to an MQTT broker, e.g., the famous Eclipse Mosquitto. After data made the hop to the MQTT broker it can be used by home automation software, like openHAB or Home Assistant.

Demo gif for command line execution

The program can be executed for a single run or in daemon mode to run continuously in the background.

Features

  • Support for Xiaomi Mi Flora sensors (tested with firmware v2.6.2, v2.6.4, v2.6.6, v3.1.4, others anticipated)
  • Build on top of open-homeautomation/miflora
  • Highly configurable
  • Data publication via MQTT
  • JSON encoded or following the Homie Convention
  • Announcement messages to support auto-discovery services
  • MQTT authentication support
  • Daemon mode (default)
  • Systemd service, sd_notify messages generated
  • MQTT-less mode, printing data directly to stdout/file
  • Automatic generation of openHAB items and rules
  • Reliable and intuitive
  • Tested on Raspberry Pi 3 and 0W

Promotional image

Readings

The Mi Flora sensor offers the following plant and soil readings:

Name Description
temperature Air temperature, in [°C] (0.1°C resolution)
light Sunlight intensity, in [lux]
moisture Soil moisture, in [%]
conductivity Soil fertility, in [µS/cm]
battery Sensor battery level, in [%]

Prerequisites

An MQTT broker is needed as the counterpart for this daemon. Even though an MQTT-less mode is provided, it is not recommended for normal smart home automation integration. MQTT is huge help in connecting different parts of your smart home and setting up of a broker is quick and easy.

Installation

On a modern Linux system just a few steps are needed to get the daemon working. The following example shows the installation under Debian/Raspbian below the /opt directory:

sudo apt install git python3 python3-pip bluetooth bluez

git clone https://github.com/ThomDietrich/miflora-mqtt-daemon.git /opt/miflora-mqtt-daemon

cd /opt/miflora-mqtt-daemon
sudo pip3 install -r requirements.txt

Configuration

To match personal needs, all operation details can be configured using the file config.ini.

vim /opt/miflora-mqtt-daemon/config.ini

Attention: You need to add at least one sensor to the configuration. Scan for available Mi Flora sensors in your proximity with the command:

sudo hcitool lescan

Interfacing your Mi Flora sensor with this program is harmless. The device will not be modified and will still work with the official Xiaomi app.

Execution

A first test run is as easy as:

python3 /opt/miflora-mqtt-daemon/miflora-mqtt-daemon.py

With a correct configuration the result should look similar to the the screencap above. Pay attention to communication errors due to distance related weak BLE connections.

The extensive output can be reduced to error messages:

python3 /opt/miflora-mqtt-daemon/miflora-mqtt-daemon.py > /dev/null

Continuous Daemon/Service

You most probably want to execute the program continuously in the background. This can be done either by using the internal daemon or cron.

Attention: Daemon mode must be enabled in the configuration file (default).

  1. Systemd service - on systemd powered systems the recommended option

    sudo cp /opt/miflora-mqtt-daemon/template.service /etc/systemd/system/miflora.service
    
    sudo systemctl daemon-reload
    
    sudo systemctl start miflora.service
    sudo systemctl status miflora.service
    
    sudo systemctl enable miflora.service
    
  2. Screen Shell - Run the program inside a screen shell:

    screen -S miflora-mqtt-daemon -d -m python3 /path/to/miflora-mqtt-daemon.py
    

Integration

In the "mqtt-json" reporting mode, data will be published to the MQTT broker topic "miflora/sensorname" (e.g. miflora/petunia, names configurable). An example:

{"light": 5424, "moisture": 30, "temperature": 21.4, "conductivity": 1020, "battery": 100}

This data can be subscribed to and processed by other applications, like openHAB.

Enjoy!

openHAB

The following code snippet shows a simple example of how a Mi Flora openHAB Items file could look like for the above example:

// miflora.items

// Mi Flora specific groups
Group gBattery "Mi Flora sensor battery level elements" (gAll)
Group gTemperature "Mi Flora air temperature elements" (gAll)
Group gMoisture "Mi Flora soil moisture elements" (gAll)
Group gConductivity "Mi Flora soil conductivity/fertility elements" (gAll)
Group gLight "Mi Flora sunlight intensity elements" (gAll)

// Mi Flora "Big Blue Petunia" (C4:7C:8D:60:DC:E6)
Number Balcony_Petunia_Battery "Balcony Petunia Sensor Battery Level [%d %%]" <text> (gBalcony, gBattery) {mqtt="<[broker:miflora/petunia:state:JSONPATH($.battery)]"}
Number Balcony_Petunia_Temperature "Balcony Petunia Air Temperature [%.1f °C]" <text> (gBalcony, gTemperature) {mqtt="<[broker:miflora/petunia:state:JSONPATH($.temperature)]"}
Number Balcony_Petunia_Moisture "Balcony Petunia Soil Moisture [%d %%]" <text> (gBalcony, gMoisture) {mqtt="<[broker:miflora/petunia:state:JSONPATH($.moisture)]"}
Number Balcony_Petunia_Conductivity "Balcony Petunia Soil Conductivity/Fertility [%d µS/cm]" <text> (gBalcony, gConductivity) {mqtt="<[broker:miflora/petunia:state:JSONPATH($.conductivity)]"}
Number Balcony_Petunia_Light "Balcony Petunia Sunlight Intensity [%d lux]" <text> (gBalcony, gLight) {mqtt="<[broker:miflora/petunia:state:JSONPATH($.light)]"}

The daemon includes a function to generate these items definitions for you! The function is finished but not yet available in the stable version. Please contact me for details.


Xiaomi and Mi Flora are registered trademarks of BEIJING XIAOMI TECHNOLOGY CO., LTD.

This project is a community project not for commercial use. The authors will not be held responsible in the event of device failure or withered plants.

This project is in no way affiliated with, authorized, maintained, sponsored or endorsed by Xiaomi or any of its affiliates or subsidiaries.