Home Assistant Community

Xiaomi Mi Plants Monitor Flower

guide
Tags: #<Tag:0x00007fcf632e2ae0>
#1

[Guide] Xiaomi Mi Plants Monitor Flower

Hi,

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):
https://pypi.python.org/pypi/miflora/0.1.4

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.

#!/usr/bin/python

import sys
import time

from miflora.miflora_poller import MiFloraPoller, \
    MI_FERTILITY, MI_MOISTURE, MI_LIGHT, MI_TEMPERATURE

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":
  myMAC = "[MAC ADDRESS OF A MI FLORA SENSOR]"
else:
  myMAC = "[MAC ADDRESS OF ANOTHER MI FLORA SENSOR]"

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")

f.close()

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:

nr1
2016-08-29 20:45:04
Temperature:20.4
Moisture:33
Light:73
Fertility:104

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

import sys

#Temperature:18.1
#Moisture:32
#Light:329
#Fertility:117

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!

6 Likes
Magic Blue Bulb Bluetooth 4.0
#2

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

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

#3

Looks like somebody already found my code :wink:

Integration into Home Assistant will be much easier than this:
https://www.open-homeautomation.com/2016/08/29/monitor-your-plants-with-home-assistant/

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,
Daniel

#4

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:
https://www.open-homeautomation.com/2016/08/23/reverse-engineering-the-mi-plant-sensor/

1 Like
#5

@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
#6

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).

#7

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.

#8

My basil is now Twitter-enabled :wink:
https://twitter.com/danielspflanzen

Configuration here:
https://www.open-homeautomation.com/2016/08/31/let-your-plant-twitter-if-it-needs-to-be-watered/

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

5 Likes
#9

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

#10

HAHAH!

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

#11

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

hass.log

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?

#12

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.

#13

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

#14

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

#15

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

What miflora files am i supposed to put where?

#16

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

#17

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.

#18

Looks great. I just ordered one.

#19

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

#20

@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.

cheers,