Xiaomi Mi Plants Monitor Flower

[Guide] Xiaomi Mi Plants Monitor Flower


So I bought myself a set of these sensors and though I’d share how I integrated them into Home Assistant if anyone is interested: Friendly warning: poorly written code ahead.

Start by installing this Python-module created by Daniel Matuschek (edit: apparently @openha here on the forum):

The idea here is to use the miflora-module to read the plant tester, write the output to a txt-file and then have Home Assistant read the txt-file and put the result into a sensor. For those wondering, I started by having a shell_command-sensor read the output from the sensor directly but I had connection problems and took to the belowmentioned plan B instead.

I’ve created a file, write_file.py, that contains the code below. It will basically just dump output from a sensor into a txt-file. The python-file is located in /home/hass/.homeassistant/extraconfig/python_code.


import sys
import time

from miflora.miflora_poller import MiFloraPoller, \

myArgument = str(sys.argv[1])

# To find the MAC-addresses, run sudo hcitool lescan
# The sensors will show up as Flower mate
if myArgument == "nr1":

poller = MiFloraPoller(myMAC)

f = open( myArgument +".txt","w")

f.write(myArgument + "\n")
f.write(time.strftime("%Y-%m-%d %H:%M:%S")+ "\n")
f.write("Temperature:{}".format(poller.parameter_value("temperature")) + "\n")
f.write("Moisture:{}".format(poller.parameter_value(MI_MOISTURE)) + "\n")
f.write("Light:{}".format(poller.parameter_value(MI_LIGHT)) + "\n")
f.write("Fertility:{}".format(poller.parameter_value(MI_FERTILITY)) + "\n")


I then added two lines to my crontab, by invoking

sudo crontab -e

and then adding:

*/15 * * * * cd /home/hass/.homeassistant/extraconfig/python_code && sudo /usr/bin/python write_file.py nr1 > /tmp/listener.log 2>&1
*/17 * * * * cd /home/hass/.homeassistant/extraconfig/python_code && sudo /usr/bin/python write_file.py nr2 > /tmp/listener.log 2>&1

If this works, you will (after 20 minutes or so) have two txt-files looking something like this:

2016-08-29 20:45:04

Next up is to add a file named read_file.py, containing the following:

import sys


sensor = str(sys.argv[1])
variable = str(sys.argv[2])

filename = "/home/hass/.homeassistant/extraconfig/python_code/" + sensor + ".txt"

for line in open(filename):
  if variable in line:
    s_split = line.split(':')
    print s_split[1].rstrip("\n\r")

Lastly, add some sensor-configuration to Home Assistant like so:

- platform: command_line
  command: /usr/bin/python /home/hass/.homeassistant/extraconfig/python_code/read_file.py nr1 Temperature
  name: flower_nr1_temp
  state_topic: "sensor/temperature"
  unit_of_measurement: "ºC"

- platform: command_line
  command: /usr/bin/python /home/hass/.homeassistant/extraconfig/python_code/read_file.py nr1 Light
  name: flower_nr1_light
  unit_of_measurement: "lx"

- platform: command_line
  command: /usr/bin/python /home/hass/.homeassistant/extraconfig/python_code/read_file.py nr1 Moisture
  name: flower_nr1_moist
  unit_of_measurement: "%"

- platform: command_line
  command: /usr/bin/python /home/hass/.homeassistant/extraconfig/python_code/read_file.py nr1 Fertility
  name: flower_nr1_fertility
  unit_of_measurement: "uS/cm"

And you’re done. Enjoy sitting at work monitoring your indoor flowers at home!


I have been looking for a decent (and cheap) moisture sensor!

looks like i am going to have to go back to the wallet!

Looks like somebody already found my code :wink:

Integration into Home Assistant will be much easier than this:

The code is already on my disk, I will create a pull request soon. I hope the code will make it into the 0.28 release.

Best regards,

Ok, initially I wanted to test the code a bit longer before submitting. However, I don’t want people to re-implement something that is already on my hard disk:

Reverse engineering that stuff took quite a while:

1 Like

@openha: I actually read your reverse-engineering-post, was impressed, prayed that someone had already implemented it somewhere and then found your Python code. Great work! I will happily scrap my ugly code as soon as Miflora makes it natively into Home Assistant.

1 Like

What is the range of these sensors? The server running HA is a room/floor away from most of my plants. I could add a BT adapter to the media center raspi, and send updates via mqtt (that would really make me wish for a proper distributed HA solution).

Depends a bit on the quality of your BTLE hardware. 1 wall might still work, but I can get a signal through 2 walls.
I will try to pimp the antenna of my NUC and Raspberry Pi.

My basil is now Twitter-enabled :wink:

Configuration here:

It’s just a few very simple automation rules combined with the Twitter notify module.


@homeha: This is spinning out of control :wink:


I love it. looks like you are remembering to water them :smiley:

i cloned your repo but am not having success with the miflora platform.


16-09-09 01:28:34 miflora.miflora_poller: Waiting for 80 seconds before retrying
16-09-09 01:28:34 miflora.miflora_poller: Lock released
16-09-09 01:28:34 homeassistant.components.sensor.miflora: Polling error [Errno Could not read data from Mi Flora sensor %s] C4:7C:8D:60:BF:D1

16-09-09 01:06:47 homeassistant.loader: Loaded sensor.miflora from homeassistant.components.sensor.miflora
16-09-09 01:09:04 homeassistant.components.sensor.miflora: Polling error [Errno Could not read data from Mi Flora sensor %s] C4:7C:8D:60:BF:D1

hass outputs following to the terminal:

Send: 0x0d 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
Send: 0x0d 0x00 0x00 0x01 0x02 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
Recv: 0x14 0x01 0x00 0x01 0x02 0x53 0x06 0x22 0x04 0x04 0x51 0x01 0x02 0x1c 0x03 0x00 0x00 0x00 0x00 0x00 0x00 

and also this:

connect error: Transport endpoint is not connected (107)

does this ring any bells?

This looks like your PC can’t read from the sensor. Does “hcitool lescan” show the sensor?
If yes, you might still be connected with the Smartphone app to the sensor. Stop the App on your Smartphone.

You can simply try to read from command line:
gatttool --device=MAC_ADDRESS --char-read -a 0x35

it started working :slight_smile:
i hope this will be included in 0.28

i dont suppose you have instructions on how to install and use it?

What miflora files am i supposed to put where?

Does anyone have any idea what it means by ‘fertility’ ? How would the sensor even measure that?

From my understanding, it the Xiaomi measures conductivity in the soil. I did some shallow research on this a while back and what I gathered was that it was a known quick, but efficient, test amongst farmers to check soil conductivity to ensure that their crops would grow as expected. If the value was to low, they would apply more fertilizers.

Looks great. I just ordered one.

Yes, the “fertility” is just the conductivity. Therefore I have renamed it here.

@openha - i have three running, but the light sensors aren’t that great.

I grabbed a copy of the component from your github repo, and added it as a custom component. I think there was something wrong that i had to fiddle to get it work, and i was wondering if there are some improvements / fixes?

my lux levels are somewhere between 0 and 32 lux, which seems quite low…

any help would be great.