Page 1 of 5

Python plugin: Xiaomi Mi Flower Mate

Posted: Tuesday 20 February 2018 16:15
by blauwebuis
With this plugin you can easily connect to your Xiaomi Flower Mate devices.

These are Bluetooth LE plant monitoring devices which cost about 12 dollars. They last up to a year on a coincell battery, and measure moisture, temperature, light and conductivity.
https://wiki.hackerspace.pl/projects:xiaomi-flora

1. Install the plugin
2. Keep automatic mode, or change to manual mode. In manual mode you can control which devices should be enabled, simply by adding their mac addresses on the hardware page.
3. The plugin creates 4 sensors (moisture, etc) for each device.
4. The plugin creates a special switch. Toggle the switch to get fresh data from all sensors.

By having a switch to poll for new data, it becomes easy to create timers and other fun integrations in scripts.

Details on the wiki:
https://www.domoticz.com/wiki/Plugins/Mi_flower_mate

Download on Github:
https://github.com/flatsiedatsie/Mi_Flower_mate_plugin

Re: Python plugin: Xiaomi Mi Flower Mate

Posted: Tuesday 20 February 2018 21:02
by joostnl
Great to see new development! Are there any advantages above the original wiki script?

Re: Python plugin: Xiaomi Mi Flower Mate

Posted: Wednesday 21 February 2018 0:57
by blauwebuis
UPDATE

I've added an "automatic" mode. It does a bluetooth scan when Domoticz starts and then automatically adds any new MiFlora sensors it finds. So after installing this plugin your MiFlora devices will automatically show up.

Advantages of using the plugin:
- Much easier to install than the old script.
- No need to manually edit anything in the script. The plugin even has an 'automatic' mode.
- Easy to change polling frequency: just set a timer for the 'master switch' which the plugin creates. Goodbye cron!
- Curious how your plants are doing right now? Just toggle that switch.
- It uses the new BluePy library, and the latest version of the MiFlora library, making it more robust.

The Mi Flora / Mi Flower Mate devices have come down in price a bit too ( $12 ).

Re: Python plugin: Xiaomi Mi Flower Mate

Posted: Wednesday 21 February 2018 21:43
by IDANIT
TYVM, this plugin is awesome, great job!!!
blauwebuis wrote: Wednesday 21 February 2018 0:57 Easy to change polling frequency: just set a timer for the 'master switch' which the plugin creates.
Is it possible (how) to poll, let say every hour instead once a day (grafana)? As I noticed it checks the status of both on and off.

Re: Python plugin: Xiaomi Mi Flower Mate

Posted: Thursday 22 February 2018 0:19
by blauwebuis
Setting a timer is easy. Just setup the switch with a timer as you would any other switch.

I could make it so that the switch immediately turns itself off again.

Re: Python plugin: Xiaomi Mi Flower Mate

Posted: Thursday 22 February 2018 8:53
by IDANIT
blauwebuis wrote: Thursday 22 February 2018 0:19 Setting a timer is easy. Just setup the switch with a timer as you would any other switch.
It is easy when You know how to ;-), never did it...
blauwebuis wrote: Thursday 22 February 2018 0:19 I could make it so that the switch immediately turns itself off again.
I think it would be ok

Some issue:

Code: Select all

2018-02-22 08:32:14.672 (MiFlora) Calling message handler 'onHeartbeat'.
2018-02-22 08:32:14.672 (MiFlora) Calling message handler 'onHeartbeat'.
2018-02-22 08:32:20.644 (MiFlora) Calling message handler 'onHeartbeat'.
2018-02-22 08:32:30.677 (MiFlora) Calling message handler 'onHeartbeat'.
2018-02-22 08:32:40.653 (MiFlora) Calling message handler 'onHeartbeat'.
2018-02-22 08:32:50.680 (MiFlora) Calling message handler 'onHeartbeat'.
2018-02-22 08:33:00.656 (MiFlora) Calling message handler 'onHeartbeat'.
2018-02-22 08:33:10.639 (MiFlora) Calling message handler 'onHeartbeat'.
2018-02-22 08:33:20.667 (MiFlora) Calling message handler 'onHeartbeat'.
2018-02-22 08:33:30.644 (MiFlora) Calling message handler 'onHeartbeat'.
2018-02-22 08:33:40.671 (MiFlora) Calling message handler 'onHeartbeat'.
2018-02-22 08:33:50.699 (MiFlora) Calling message handler 'onHeartbeat'.
2018-02-22 08:34:00.678 (MiFlora) Calling message handler 'onHeartbeat'.
Domoticz status:

Code: Select all

2018-02-22 08:04:03.883 Domoticz V3.8930 (c)2012-2018 GizMoCuz
2018-02-22 08:04:03.886 Build Hash: 2b97ba86, Date: 2018-02-21 09:02:15
2018-02-22 08:04:03.887 Startup Path: /home/pi/domoticz/
2018-02-22 08:04:04.314 EventSystem: reset all events...
2018-02-22 08:04:05.075 PluginSystem: Started, Python version '3.5.3'.
2018-02-22 08:04:05.137 WebServer(HTTP) started on address: :: with port 8080
2018-02-22 08:04:05.176 WebServer(SSL) started on address: :: with port 443
2018-02-22 08:04:05.183 Proxymanager started.
2018-02-22 08:04:05.193 TCPServer: shared server started...
2018-02-22 08:04:05.193 RxQueue: queue worker started...
2018-02-22 08:04:07.194 (MiFlora) Started.
2018-02-22 08:04:07.195 EventSystem: reset all events...
2018-02-22 08:04:07.196 EventSystem: reset all device statuses...
2018-02-22 08:04:07.350 PluginSystem: Entering work loop.
2018-02-22 08:04:07.740 Python EventSystem: Initalizing event module.
2018-02-22 08:04:07.747 EventSystem: Queue thread started...
2018-02-22 08:04:07.747 EventSystem: Started
2018-02-22 08:04:08.809 (MiFlora) Initialized version 1.0.0, author 'blauwebuis'
2018-02-22 08:04:08.809 (MiFlora) Entering work loop.

Re: Python plugin: Xiaomi Mi Flower Mate

Posted: Friday 23 February 2018 15:03
by Oliwier
IDANIT wrote: Thursday 22 February 2018 8:53Some issue:

Code: Select all

2018-02-22 08:32:04.672 (MiFlora) Calling message handler 'onHeartbeat'.
2018-02-22 08:32:14.672 (MiFlora) Calling message handler 'onHeartbeat'.
2018-02-22 08:33:50.699 (MiFlora) Calling message handler 'onHeartbeat'.
2018-02-22 08:34:00.678 (MiFlora) Calling message handler 'onHeartbeat'.
Same problem here every 10s 'onHeartbeat' but bigger issue is:

Code: Select all

2018-02-23 15:31:56.966 Error: (MiFlora) 'onCommand' failed 'BTLEException'.
2018-02-23 15:31:56.966 Error: (MiFlora) ----> Line 242 in /home/pi/domoticz/plugins/Mi_Flower_mate_plugin/plugin.py, function onCommand
2018-02-23 15:31:56.966 Error: (MiFlora) ----> Line 93 in /home/pi/domoticz/plugins/Mi_Flower_mate_plugin/plugin.py, function onCommand
2018-02-23 15:31:56.966 Error: (MiFlora) ----> Line 149 in /home/pi/domoticz/plugins/Mi_Flower_mate_plugin/plugin.py, function getPlantData
2018-02-23 15:31:56.966 Error: (MiFlora) ----> Line 103 in /home/pi/domoticz/plugins/Mi_Flower_mate_plugin/miflora/miflora_poller.py, function firmware_version
2018-02-23 15:31:56.966 Error: (MiFlora) ----> Line 47 in /home/pi/domoticz/plugins/Mi_Flower_mate_plugin/miflora/backends/__init__.py, function __enter__
2018-02-23 15:31:56.967 Error: (MiFlora) ----> Line 55 in /home/pi/domoticz/plugins/Mi_Flower_mate_plugin/miflora/backends/bluepy.py, function connect
2018-02-23 15:31:56.967 Error: (MiFlora) ----> Line 353 in /usr/local/lib/python3.5/dist-packages/bluepy/btle.py, function __init__
2018-02-23 15:31:56.967 Error: (MiFlora) ----> Line 402 in /usr/local/lib/python3.5/dist-packages/bluepy/btle.py, function _connect
after that, switch doesn't respond and no new data from sensors, only solution is restarting domoticz.

Re: Python plugin: Xiaomi Mi Flower Mate

Posted: Friday 23 February 2018 18:56
by blauwebuis
@idanit: you are using a older version that still has debugging output enabled.

@Oliwier: how many Flower Mates do you have? I have tested it with 2. I also got the error you mentioned, but fixed that by adding a bit more time between readings of all the sensors. Perhaps if you have more than 2 it needs a bit more time still. Or perhaps you're also using the older version, as you also get the debug output.

You can try making this longer in the code too:

Code: Select all

# give bluetooth a little breathing room
time.sleep(1)

Re: Python plugin: Xiaomi Mi Flower Mate

Posted: Saturday 24 February 2018 9:12
by IDANIT
blauwebuis wrote: Friday 23 February 2018 18:56 @idanit: you are using a older version that still has debugging output enabled.
I've noticed that it is 1.0.0 version, but I just git clone it from Your github. I did it again but it is still 1.0.0.
I am not much of a python programmer, should I change it to

Code: Select all

time.sleep(2)
or

Code: Select all

time.sleep(3)
?

Re: Python plugin: Xiaomi Mi Flower Mate

Posted: Sunday 25 February 2018 13:39
by Oliwier
blauwebuis wrote: Friday 23 February 2018 18:56 @Oliwier: how many Flower Mates do you have? I have tested it with 2. I also got the error you mentioned, but fixed that by adding a bit more time between readings of all the sensors. Perhaps if you have more than 2 it needs a bit more time still. Or perhaps you're also using the older version, as you also get the debug output.

You can try making this longer in the code too:

Code: Select all

# give bluetooth a little breathing room
time.sleep(1)
1. I got 5 MiFloras
2. I've changed sleep time to 3 sec.
3. I git clone plugin onece again... no 'onHeartbeat' but same as @Idanit plugin v 1.0.0
4. still got the same error as before:

Code: Select all

2018-02-25 12:00:01.140 Schedule item started! Name: MiFlora - Flip to update Mi Flowermates, Type: On Time, DevID: 1, Time: 2018-02-25 12:00:01
2018-02-25 12:00:01.152 (MiFlora) onCommand called for Unit 1: Parameter 'On', Level: 0
2018-02-25 12:00:01.175 (MiFlora) getting data from sensor: xxxxxxxxx
2018-02-25 12:00:03.508 (MiFlora) moisture = 0
2018-02-25 12:00:03.517 (MiFlora) temperature = 26.7
2018-02-25 12:00:03.526 (MiFlora) light = 40
2018-02-25 12:00:03.536 (MiFlora) conductivity = 0
2018-02-25 12:00:06.539 (MiFlora) getting data from sensor: xxxxxxxxx
2018-02-25 12:00:08.167 (MiFlora) moisture = 0
2018-02-25 12:00:08.177 (MiFlora) temperature = 22.4
2018-02-25 12:00:08.187 (MiFlora) light = 76
2018-02-25 12:00:08.196 (MiFlora) conductivity = 0
2018-02-25 12:00:11.199 (MiFlora) getting data from sensor: xxxxxxxxxx
2018-02-25 12:00:14.566 (MiFlora) moisture = 30
2018-02-25 12:00:14.575 (MiFlora) temperature = 21.6
2018-02-25 12:00:14.585 (MiFlora) light = 108
2018-02-25 12:00:14.594 (MiFlora) conductivity = 343
2018-02-25 12:00:17.598 (MiFlora) getting data from sensor: xxxxxxxxxx
2018-02-25 12:00:22.107 (MiFlora) moisture = 1
2018-02-25 12:00:22.117 (MiFlora) temperature = 26.7
2018-02-25 12:00:22.126 (MiFlora) light = 22
2018-02-25 12:00:22.135 (MiFlora) conductivity = 0
2018-02-25 12:00:25.139 (MiFlora) getting data from sensor: xxxxxxxxxxx
2018-02-25 12:00:29.497 (MiFlora) moisture = 23
2018-02-25 12:00:29.507 (MiFlora) temperature = 21.5
2018-02-25 12:00:29.516 (MiFlora) light = 104
2018-02-25 12:00:29.525 (MiFlora) conductivity = 164
2018-02-25 12:23:22.515 Domoticz: TCP connected to: 192.168.1.85:6144
2018-02-25 12:25:01.383 Incoming Domoticz connection from: 192.168.1.110
2018-02-25 13:00:01.852 Schedule item started! Name: MiFlora - Flip to update Mi Flowermates, Type: On Time, DevID: 1, Time: 2018-02-25 13:00:01
2018-02-25 13:00:01.856 (MiFlora) onCommand called for Unit 1: Parameter 'Off', Level: 100
2018-02-25 13:00:01.875 (MiFlora) getting data from sensor: xxxxxxxxxxxx
2018-02-25 13:00:05.484 (MiFlora) moisture = 0
2018-02-25 13:00:05.497 (MiFlora) temperature = 26.7
2018-02-25 13:00:05.510 (MiFlora) light = 6
2018-02-25 13:00:05.523 (MiFlora) conductivity = 0
2018-02-25 13:00:08.524 (MiFlora) getting data from sensor: xxxxxxxxxxxxxxxxx
2018-02-25 13:00:13.047 (MiFlora) moisture = 0
2018-02-25 13:00:13.056 (MiFlora) temperature = 22.5
2018-02-25 13:00:13.066 (MiFlora) light = 0
2018-02-25 13:00:13.075 (MiFlora) conductivity = 0
2018-02-25 13:00:16.079 (MiFlora) getting data from sensor: xxxxxxxxxxxxxxxxx
2018-02-25 13:00:19.013 Error: (MiFlora) 'onCommand' failed 'BTLEException'.
2018-02-25 13:00:19.013 Error: (MiFlora) ----> Line 242 in /home/pi/domoticz/plugins/Mi_Flower_mate_plugin/plugin.py, function onCommand
2018-02-25 13:00:19.013 Error: (MiFlora) ----> Line 93 in /home/pi/domoticz/plugins/Mi_Flower_mate_plugin/plugin.py, function onCommand
2018-02-25 13:00:19.014 Error: (MiFlora) ----> Line 149 in /home/pi/domoticz/plugins/Mi_Flower_mate_plugin/plugin.py, function getPlantData
2018-02-25 13:00:19.014 Error: (MiFlora) ----> Line 103 in /home/pi/domoticz/plugins/Mi_Flower_mate_plugin/miflora/miflora_poller.py, function firmware_version
2018-02-25 13:00:19.014 Error: (MiFlora) ----> Line 47 in /home/pi/domoticz/plugins/Mi_Flower_mate_plugin/miflora/backends/__init__.py, function __enter__
2018-02-25 13:00:19.014 Error: (MiFlora) ----> Line 55 in /home/pi/domoticz/plugins/Mi_Flower_mate_plugin/miflora/backends/bluepy.py, function connect
2018-02-25 13:00:19.014 Error: (MiFlora) ----> Line 353 in /usr/local/lib/python3.5/dist-packages/bluepy/btle.py, function __init__
2018-02-25 13:00:19.014 Error: (MiFlora) ----> Line 402 in /usr/local/lib/python3.5/dist-packages/bluepy/btle.py, function _connect
5. I've change sleep time to 5sec, we will see what happens...

Re: Python plugin: Xiaomi Mi Flower Mate

Posted: Monday 26 February 2018 8:36
by Oliwier
Well, 5 sec also did not help... lets try with 7.

Re: Python plugin: Xiaomi Mi Flower Mate

Posted: Monday 26 February 2018 9:15
by blauwebuis
@oliwier: see also here:
https://github.com/IanHarvey/bluepy/issues/218

Are you using python 3.5 by any chance?

Re: Python plugin: Xiaomi Mi Flower Mate

Posted: Monday 26 February 2018 18:08
by Oliwier
blauwebuis wrote: Monday 26 February 2018 9:15 Are you using python 3.5 by any chance?
Indeed, 3.5.3

Re: Python plugin: Xiaomi Mi Flower Mate

Posted: Wednesday 28 February 2018 20:13
by IDANIT
I've read somewhere that there are some issues with python 3.5 on Raspbian Stretch.
I uninstalled version 3.5 and installed 3.4, but the problem remained. Also 10s did not solve the problem.

Re: Python plugin: Xiaomi Mi Flower Mate

Posted: Wednesday 28 February 2018 21:45
by Oliwier
IDANIT wrote: Wednesday 28 February 2018 20:13 I uninstalled version 3.5 and installed 3.4, but the problem remained. Also 10s did not solve the problem.

Code: Select all

sudo apt-get remove python3.5
is enough?

Re: Python plugin: Xiaomi Mi Flower Mate

Posted: Wednesday 28 February 2018 22:40
by IDANIT
and

Code: Select all

sudo apt-get autoremove


Re: Python plugin: Xiaomi Mi Flower Mate

Posted: Thursday 01 March 2018 12:37
by blauwebuis
UPDATE

I've uploaded a new version of the plugin that may fix the bluetooth issue.

The plugin now uses the heartbeat function to poll one sensor per heartbeat.
- This could give more space between polls, and thus prevent the Bluepy helper from crashing if users have a lot of Flower Mates.
- This also keeps Domoticz happy, as the plugin now doesn't take up too much processing time.

The plugin now also toggles the switch visually to "on" when the switch it pressed, and turns itself visually back into the off-state once all the polling is done.

Re: Python plugin: Xiaomi Mi Flower Mate

Posted: Thursday 01 March 2018 21:08
by IDANIT
sorry mate, no change at all.
Well... there is one... instead of: "onCommand"

Code: Select all

(MiFlora) 'onCommand' failed 'BTLEException'.
Error: (MiFlora) ----> Line 242 in /home/pi/domoticz/plugins/Mi_Flower_mate_plugin/plugin.py, function onCommand
Error: (MiFlora) ----> Line 93 in /home/pi/domoticz/plugins/Mi_Flower_mate_plugin/plugin.py, function onCommand
Error: (MiFlora) ----> Line 149 in /home/pi/domoticz/plugins/Mi_Flower_mate_plugin/plugin.py, function getPlantData
Error: (MiFlora) ----> Line 103 in /home/pi/domoticz/plugins/Mi_Flower_mate_plugin/miflora/miflora_poller.py, function firmware_version
Error: (MiFlora) ----> Line 47 in /home/pi/domoticz/plugins/Mi_Flower_mate_plugin/miflora/backends/__init__.py, function __enter__
Error: (MiFlora) ----> Line 55 in /home/pi/domoticz/plugins/Mi_Flower_mate_plugin/miflora/backends/bluepy.py, function connect
Error: (MiFlora) ----> Line 353 in /usr/local/lib/python3.5/dist-packages/bluepy/btle.py, function __init__
Error: (MiFlora) ----> Line 402 in /usr/local/lib/python3.5/dist-packages/bluepy/btle.py, function _connect
it is: "onHeartbeat"

Code: Select all

Error: (MiFlora) 'onHeartbeat' failed 'BTLEException'.
Error: (MiFlora) ----> Line 247 in /home/pi/domoticz/plugins/Mi_Flower_mate_plugin/plugin.py, function onHeartbeat
Error: (MiFlora) ----> Line 99 in /home/pi/domoticz/plugins/Mi_Flower_mate_plugin/plugin.py, function onHeartbeat
Error: (MiFlora) ----> Line 154 in /home/pi/domoticz/plugins/Mi_Flower_mate_plugin/plugin.py, function getPlantData
Error: (MiFlora) ----> Line 103 in /home/pi/domoticz/plugins/Mi_Flower_mate_plugin/miflora/miflora_poller.py, function firmware_version
Error: (MiFlora) ----> Line 47 in /home/pi/domoticz/plugins/Mi_Flower_mate_plugin/miflora/backends/__init__.py, function __enter__
Error: (MiFlora) ----> Line 55 in /home/pi/domoticz/plugins/Mi_Flower_mate_plugin/miflora/backends/bluepy.py, function connect
Error: (MiFlora) ----> Line 353 in /usr/local/lib/python3.5/dist-packages/bluepy/btle.py, function __init__
Error: (MiFlora) ----> Line 402 in /usr/local/lib/python3.5/dist-packages/bluepy/btle.py, function _connec

Re: Python plugin: Xiaomi Mi Flower Mate

Posted: Saturday 10 March 2018 9:49
by Danielson
I'm also facing the BTLEException error. 90% of the time the plug-in is working great, but it seems when the script has trouble seeing the Flower Mate the script crashes and i have to restart Domoticz before the script will work again.

Re: Python plugin: Xiaomi Mi Flower Mate

Posted: Saturday 10 March 2018 16:45
by deennoo
That a really good Job guy !

I'm using BLE Scanner Plugin too, I have 2 bluetooth LE dongle, can you add a choice of which one to use please ? using dongle mac adresse is a good choice, hci0 or hci1 can be good too

Actually, if i check miflora info running blescanner this give this :

Code: Select all

018-03-10 16:42:46.830 Error: (Miflora) 'onHeartbeat' failed 'BTLEException'.
2018-03-10 16:42:46.830 Error: (Miflora) ----> Line 247 in /home/pi/domoticz/plugins/MiFlora/plugin.py, function onHeartbeat
2018-03-10 16:42:46.830 Error: (Miflora) ----> Line 99 in /home/pi/domoticz/plugins/MiFlora/plugin.py, function onHeartbeat
2018-03-10 16:42:46.830 Error: (Miflora) ----> Line 154 in /home/pi/domoticz/plugins/MiFlora/plugin.py, function getPlantData
2018-03-10 16:42:46.830 Error: (Miflora) ----> Line 103 in /home/pi/domoticz/plugins/MiFlora/miflora/miflora_poller.py, function firmware_version
2018-03-10 16:42:46.830 Error: (Miflora) ----> Line 47 in /home/pi/domoticz/plugins/MiFlora/miflora/backends/__init__.py, function __enter__
2018-03-10 16:42:46.830 Error: (Miflora) ----> Line 55 in /home/pi/domoticz/plugins/MiFlora/miflora/backends/bluepy.py, function connect
2018-03-10 16:42:46.830 Error: (Miflora) ----> Line 353 in /usr/local/lib/python3.5/dist-packages/bluepy/btle.py, function __init__
2018-03-10 16:42:46.830 Error: (Miflora) ----> Line 402 in /usr/local/lib/python3.5/dist-packages/bluepy/btle.py, function _connect