[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):
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!