Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon

I’m struggling with the setup and I’m sure someone can help :slight_smile:
First of all … I’m running OpenHAB 2.4 stable (openhabian) on my RPi 3b.

What is my status:

I installed Mosquitto MQTT using the OpenHabian config tool.
First question: Do I have to change a Mosquitto config file?

The Python part was already installed and I changed the config to:
reporting_method = mqtt-json
username = openhabian
password = openhabian
Miflora1 = C4:7C:8D:61:B0:67
Miflora2 = C4:7C:8D:61:7C:2E

The script is running without any errors:

**[14:27:41]** **openhabian@openHABianPi** : **/opt/miflora-mqtt-daemon** $ sudo python3 /opt/miflora-mqtt-daemon/miflora-mqtt-daemon.py

**Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon**

**Source: https://github.com/ThomDietrich/miflora-mqtt-daemon**

[2018-12-25 14:41:12] Connecting to MQTT broker ...

[2018-12-25 14:41:12] MQTT connection established

Adding sensor to device list and testing connection ...

Name: "Miflora1"

Internal name: "Miflora1"

Device name: "Flower care"

MAC address: C4:7C:8D:61:B0:67

Firmware: 3.1.9

[2018-12-25 14:41:15] Initial connection to Mi Flora sensor "Miflora1" (C4:7C:8D:61:B0:67) successful

Adding sensor to device list and testing connection ...

Name: "Miflora2"

Internal name: "Miflora2"

Device name: "Flower care"

MAC address: C4:7C:8D:61:7C:2E

Firmware: 3.1.9

[2018-12-25 14:41:19] Initial connection to Mi Flora sensor "Miflora2" (C4:7C:8D:61:7C:2E) successful

[2018-12-25 14:41:19] Announcing Mi Flora devices to MQTT broker for auto-discovery ...

[2018-12-25 14:41:19] Retrieving data from sensor "Miflora1" ...

[2018-12-25 14:41:23] Result: {"moisture": 33, "battery": 99, "light": 97, "conductivity": 1695, "temperature": 20.9}

[2018-12-25 14:41:23] Publishing to MQTT topic "miflora/Miflora1"

[2018-12-25 14:41:23] Retrieving data from sensor "Miflora2" ...

[2018-12-25 14:41:31] Result: {"moisture": 26, "battery": 99, "light": 261, "conductivity": 169, "temperature": 20.8}

[2018-12-25 14:41:31] Publishing to MQTT topic "miflora/Miflora2"

[2018-12-25 14:41:31] Sleeping (300 seconds) ...

In OpenHAB I installed the MQTT Binding (2.4).
The MQTT thing is “Online” (name: broker)
And I installed the JSONPath Transformation

In addition I installed a second thing called “Generic MQTT Thing”
The Generic MQTT Thing is also “Online”

So far so good … everything seems to be okay …

Using Visual Code Studio, I created a miflora.items file with the following content:

// miflora.items - Generated by miflora-mqtt-daemon.
// Adapt to your needs! Things you probably want to modify:
//     Room group names, icons,
//     "gAll", "broker", ""

// Mi Flora specific groups
Group gMiFlora "All Mi Flora sensors and elements"
Group gLightIntensity "Mi Flora Sunlight Intensity elements" (gMiFlora)
Group gAirTemperature "Mi Flora Air Temperature elements" (gMiFlora)
Group gSoilMoisture "Mi Flora Soil Moisture elements" (gMiFlora)
Group gSoilConductivity "Mi Flora Soil Conductivity/Fertility elements" (gMiFlora)
Group gMFBattery "Mi Flora Sensor Battery Level elements" (gMiFlora)

// Mi Flora "Miflora1" (C4:7C:8D:61:B0:67)
Group gMiflora1 "Mi Flora Sensor Miflora1" (gMiFlora)
Number Miflora1_LightIntensity "Miflora1 Sunlight Intensity [%d lux]" <text> (gMiflora1, gLightIntensity) {mqtt="<[broker:miflora/Miflora1:state:JSONPATH($.light)]"}
Number Miflora1_AirTemperature "Miflora1 Air Temperature [%.1f °C]" <text> (gMiflora1, gAirTemperature) {mqtt="<[broker:miflora/Miflora1:state:JSONPATH($.temperature)]"}
Number Miflora1_SoilMoisture "Miflora1 Soil Moisture [%d %%]" <text> (gMiflora1, gSoilMoisture) {mqtt="<[broker:miflora/Miflora1:state:JSONPATH($.moisture)]"}
Number Miflora1_SoilConductivity "Miflora1 Soil Conductivity/Fertility [%d µS/cm]" <text> (gMiflora1, gSoilConductivity) {mqtt="<[broker:miflora/Miflora1:state:JSONPATH($.conductivity)]"}
Number Miflora1_Battery "Miflora1 Sensor Battery Level [%d %%]" <text> (gMiflora1, gMFBattery) {mqtt="<[broker:miflora/Miflora1:state:JSONPATH($.battery)]"}

// Mi Flora "Miflora2" (C4:7C:8D:61:7C:2E)
Group gMiflora2 "Mi Flora Sensor Miflora2" (gMiFlora)
Number Miflora2_LightIntensity "Miflora2 Sunlight Intensity [%d lux]" <text> (gMiflora2, gLightIntensity) {mqtt="<[broker:miflora/Miflora2:state:JSONPATH($.light)]"}
Number Miflora2_AirTemperature "Miflora2 Air Temperature [%.1f °C]" <text> (gMiflora2, gAirTemperature) {mqtt="<[broker:miflora/Miflora2:state:JSONPATH($.temperature)]"}
Number Miflora2_SoilMoisture "Miflora2 Soil Moisture [%d %%]" <text> (gMiflora2, gSoilMoisture) {mqtt="<[broker:miflora/Miflora2:state:JSONPATH($.moisture)]"}
Number Miflora2_SoilConductivity "Miflora2 Soil Conductivity/Fertility [%d µS/cm]" <text> (gMiflora2, gSoilConductivity) {mqtt="<[broker:miflora/Miflora2:state:JSONPATH($.conductivity)]"}
Number Miflora2_Battery "Miflora2 Sensor Battery Level [%d %%]" <text> (gMiflora2, gMFBattery) {mqtt="<[broker:miflora/Miflora2:state:JSONPATH($.battery)]"}

But saving this file dod not create Miflora items …

So I don’t see any error messages … but I don’t see my items and values.

What is wrong? Is something missing?

Do I have to edit the services/mqtt.cfg file? I thought that this is not necessary because I was able to set the config of the mqtt binding using PaperUI.

The auto generated items of the Daemon are still in mqtt1 syntax, while you are using the new mqtt binding. You have to translate the items into the new channel syntax.

Any idea how the item definition should look like?
I tried it with
Number Miflora1_LightIntensity “Miflora1 Sunlight Intensity [%d lux” <text> (gMiflora1, gLightIntensity) {channel="<mqtt:miflora/Miflora1:state:JSONPATH($.light)"}
but it’s not working.

Anyone using the new MQTT binding with miflora?

With the new binding you have to create a Thing. In that thing you will put the state topic, miflora/Miflora1
in the translation for incoming put JSONPATH:$.light. Then link the channel to the item and you are good to go.

Thank you for helping me.

But since it’s the first time I’m using mqtt, I need some further help :slight_smile:

I changed my items to:

String Miflora1_json  "Miflora1"  {channel="mqtt:topic:miflora:Miflora1"}
Number Miflora1_LightIntensity "Miflora1 Sunlight Intensity [%d lux" 
Number Miflora1_AirTemperature "Miflora1 Air Temperature [%.1f °C" 
Number Miflora1_SoilMoisture "Miflora1 Soil Moisture [%d %%" 
Number Miflora1_SoilConductivity "Miflora1 Soil Conductivity/Fertility [%d µS/cm" 
Number Miflora1_Battery "Miflora1 Sensor Battery Level [%d %%" 

First question: Do I use mqtt:broker (which is the name of the mqtt broker) or mqtt:topic (which is the name for the Generic MQTT Thing).
I checked both options, but there was no entry in the openhab log that the item changed his state.

Where and how do I create the translation?
I found a solution with a rule … but I think that you meant something else, or am I wrong?

Yes, the broker is the first step, then you create a Generic MQTT thing and inside that you have to create a channel for each item. First would be a text item, then numbers for the rest. You can use and install the internal broker vice setting up an external one.

I tried a lot … but at the end I had no luck with the new 2.4 binding. Not sure what the problem was. And it was not easy to find an example how to setup a connection using the new binding.

So I switched to the 1.x mqtt binding … and voila … everything is working with the generated items :slight_smile:

Hey, got it working today.

Basically you have to translate the old mqtt definition into a Channel within a thing. Then you can add this channel to your old items.
Following is one example of my plants.

Thing:

Thing mqtt:topic:mqttMiFloraFicus "Wohnzimmer - Ficus mqqt" {
    Channels:
        Type number : ficusLight "Wohnzimmer - Ficus Licht" [ stateTopic="miflora/Ficus", transformationPattern="JSONPATH:$.light" ]
        Type number : ficusTemperature "Wohnzimmer - Ficus Temperatur" [ stateTopic="miflora/Ficus", transformationPattern="JSONPATH:$.temperature" ]
        Type number : ficusMoisture "Wohnzimmer - Ficus Feuchtigkeit" [ stateTopic="miflora/Ficus", transformationPattern="JSONPATH:$.moisture" ]
        Type number : ficusConductivity "Wohnzimmer - Ficus Leitfähigkeit" [ stateTopic="miflora/Ficus", transformationPattern="JSONPATH:$.conductivity" ]
        Type number : ficusBattery "Wohnzimmer - Ficus Batteriestand" [ stateTopic="miflora/Ficus", transformationPattern="JSONPATH:$.battery" ]

    }

Items:

Number Wohnzimmer_Ficus_LightIntensity "Wohnzimmer Ficus Sunlight Intensity [%d lux]" <text> (gWohnzimmerFicus, gLightIntensity) { channel="mqtt:topic:mqttMiFloraFicus:ficusLight" } 
Number Wohnzimmer_Ficus_AirTemperature "Wohnzimmer Ficus Air Temperature [%.1f °C]" <text> (gWohnzimmerFicus, gAirTemperature) { channel="mqtt:topic:mqttMiFloraFicus:ficusTemperature" } 
Number Wohnzimmer_Ficus_SoilMoisture "Wohnzimmer Ficus Soil Moisture [%d %%]" <text> (gWohnzimmerFicus, gSoilMoisture) { channel="mqtt:topic:mqttMiFloraFicus:ficusMoisture" } 
Number Wohnzimmer_Ficus_SoilConductivity "Wohnzimmer Ficus Soil Conductivity/Fertility [%d µS/cm]" <text> (gWohnzimmerFicus, gSoilConductivity) { channel="mqtt:topic:mqttMiFloraFicus:ficusConductivity" }
Number Wohnzimmer_Ficus_Battery "Wohnzimmer Ficus Sensor Battery Level [%d %%]" <text> (gWohnzimmerFicus, groupBattery) { channel="mqtt:topic:mqttMiFloraFicus:ficusBattery" }

This is my setup, but it doesn’t work for some reason:

mqtt.things

Bridge mqtt:broker:openhab [ host="localhost", secure=false ] {
    Thing topic MiFloraAdam "MiFlora Adam" @ "LivingRoom" {
    Channels:
        Type number : MiFloraAdamLightIntensity "MiFlora Adam Sunlight Intensity" [ stateTopic="miflora/Adam", transformationPattern="JSONPATH:$.light" ]
        Type number : MiFloraAdamAirTemperature "MiFlora Adam Air Temperature" [ stateTopic="miflora/Adam", transformationPattern="JSONPATH:$.temperature" ]
        Type number : MiFloraAdamSoilMoisture "MiFlora Adam Soil Moisture" [ stateTopic="miflora/Adam", transformationPattern="JSONPATH:$.moisture" ]
        Type number : MiFloraAdamSoilConductivity "MiFlora Adam Soil Conductivity/Fertility" [ stateTopic="miflora/Adam", transformationPattern="JSONPATH:$.conductivity" ]
        Type number : MiFloraAdamBattery "MiFlora Adam Sensor Battery Level" [ stateTopic="miflora/Adam", transformationPattern="JSONPATH:$.battery" ]
    }
}

mqtt.items

Number MiFloraAdamLightIntensity "LivingRoom Adam Sunlight Intensity [%d lux]" (LivingRoom) { channel="mqtt:topic:openhab:MiFloraAdam:MiFloraAdamLightIntensity" }
Number MiFloraAdamAirTemperature "LivingRoom Adam Air Temperature [%.1f °C]" (LivingRoom) { channel="mqtt:topic:openhab:MiFloraAdam:MiFloraAdamAirTemperature" }
Number MiFloraAdamSoilMoisture "LivingRoom Adam Soil Moisture [%d %%]" (LivingRoom) { channel="mqtt:topic:openhab:MiFloraAdam:MiFloraAdamSoilMoisture" }
Number MiFloraAdamSoilConductivity "LivingRoom Adam Soil Conductivity/Fertility [%d µS/cm]" (LivingRoom) { channel="mqtt:topic:openhab:MiFloraAdam:MiFloraAdamSoilConductivity" }
Number MiFloraAdamBattery "LivingRoom Adam Sensor Battery Level [%d %%]" (LivingRoom) { channel="mqtt:topic:openhab:MiFloraAdam:MiFloraAdamBattery" }

Can anybody help? :slight_smile:

Hi all,

I’m using @ThomDietrich’s python code for a long time now, it worked almost perfectly till last week end.
The script is not able to communicate with my miflora anymore.
The code is installed on a Raspberry 3B+, Debian 8.
Of course I did apt-get update and apt-get upgrade, I even reinstalled @ThomDietrich script after purging and reinstalling bluetooth package.

When I launch the script, here are logs :

Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon
Source: https://github.com/ThomDietrich/miflora-mqtt-daemon

[2019-01-09 22:57:02] Connecting to MQTT broker ...
[2019-01-09 22:57:02] MQTT connection established

Adding sensor to device list and testing connection ...
Name:          "miflora/Zamioculcas"
[2019-01-09 22:57:34] Initial connection to Mi Flora sensor "miflora/Zamioculcas" (C4:7C:8D:67:2B:08) failed.

Adding sensor to device list and testing connection ...
Name:          "miflora/Beaucarnea"
[2019-01-09 22:58:04] Initial connection to Mi Flora sensor "miflora/Beaucarnea" (C4:7C:8D:67:69:8D) failed.

Adding sensor to device list and testing connection ...
Name:          "miflora/ChlorophytumComosum"
[2019-01-09 22:58:34] Initial connection to Mi Flora sensor "miflora/ChlorophytumComosum" (C4:7C:8D:67:6A:06) failed.

Adding sensor to device list and testing connection ...
Name:          "miflora/PothosAureus"
[2019-01-09 22:59:04] Initial connection to Mi Flora sensor "miflora/PothosAureus" (C4:7C:8D:67:67:F5) failed.

Adding sensor to device list and testing connection ...
Name:          "miflora/Sansevieria"
[2019-01-09 22:59:34] Initial connection to Mi Flora sensor "miflora/Sansevieria" (C4:7C:8D:67:2C:71) failed.

Adding sensor to device list and testing connection ...
Name:          "miflora/Schefflera"
[2019-01-09 23:00:04] Initial connection to Mi Flora sensor "miflora/Schefflera" (C4:7C:8D:67:68:0A) failed.

Adding sensor to device list and testing connection ...
Name:          "miflora/Phalaenopsis"
[2019-01-09 23:00:35] Initial connection to Mi Flora sensor "miflora/Phalaenopsis" (C4:7C:8D:67:68:FF) failed.

Adding sensor to device list and testing connection ...
Name:          "miflora/Calathea"
[2019-01-09 23:01:05] Initial connection to Mi Flora sensor "miflora/Calathea" (C4:7C:8D:67:69:E0) failed.

Adding sensor to device list and testing connection ...
Name:          "miflora/DracaenaMarginata"
[2019-01-09 23:01:35] Initial connection to Mi Flora sensor "miflora/DracaenaMarginata" (C4:7C:8D:67:4D:D2) failed.

[2019-01-09 23:01:35] Announcing Mi Flora devices to MQTT broker for auto-discovery ...

[2019-01-09 23:01:35] Retrieving data from sensor "miflora/Zamioculcas" ...
[2019-01-09 23:02:06] Retrying ...
[2019-01-09 23:02:36] Failed to retrieve data from Mi Flora sensor "miflora/Zamioculcas" (C4:7C:8D:67:2B:08), success rate: 0%

[2019-01-09 23:02:36] Retrieving data from sensor "miflora/Beaucarnea" ...
[2019-01-09 23:03:06] Retrying ...
[2019-01-09 23:03:36] Failed to retrieve data from Mi Flora sensor "miflora/Beaucarnea" (C4:7C:8D:67:69:8D), success rate: 0%

As I said, it worked perfectly for months, and I didn’t change anything in the code.
So I told myself maybe there is a problem with the bluetooth connections.
So I used the Xiaomi MiHome application to be sure that devices were up… and no problem to get data.
ok, so let check on raspberry by using hcitool command, and the devices are detected without any problem as shown below

pi@jarvis:~ $ sudo hcitool lescan
LE Scan ...
4F:80:78:77:2D:54 (unknown)
4F:80:78:77:2D:54 (unknown)
E0:AC:CB:7E:1A:65 (unknown)
C4:7C:8D:67:2B:08 (unknown)
C4:7C:8D:67:6A:06 (unknown)
C4:7C:8D:67:6A:06 Flower care
C4:7C:8D:67:67:F5 (unknown)
E0:AC:CB:7E:1A:65 (unknown)
4D:07:CB:FA:C2:BF (unknown)
52:14:B6:DE:F2:2B (unknown)
C4:7C:8D:67:68:0A (unknown)
C4:7C:8D:67:69:E0 (unknown)
C4:7C:8D:67:69:E0 Flower care
C4:7C:8D:67:69:8D (unknown)
C4:7C:8D:67:69:8D Flower care
C4:7C:8D:67:4D:D2 (unknown)
4D:07:CB:FA:C2:BF (unknown)
C4:7C:8D:67:2C:71 (unknown)
C4:7C:8D:67:2B:08 Flower care
C4:7C:8D:67:68:0A Flower care
52:14:B6:DE:F2:2B (unknown)
C4:7C:8D:67:68:FF Flower care

My config file is the following :

# Configuration file for Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon
# Source: https://github.com/ThomDietrich/miflora-mqtt-daemon
#
# Uncomment and adapt all settings as needed.

[General]

# The operation mode of the program. Determines wether retrieved sensor data is published via MQTT or stdout/file.
# Currently supported:
#
#           mqtt-json - Publish to an MQTT broker in a proprietary json format (Default)
#          mqtt-homie - Publish to an MQTT broker following the Homie MQTT convention
#                       (https://github.com/marvinroger/homie)
#      mqtt-smarthome - Publish to an MQTT broker following the mqtt-smarthome proposal
#                       (https://github.com/mqtt-smarthome/mqtt-smarthome)
#  homeassistant-mqtt - Publish to an MQTT broker following the HomeAssistant discovery format
#                       (https://www.home-assistant.io/docs/mqtt/discovery/)
#    thingsboard-json - Publish to the ThingsBoard MQTT broker
#                       (https://thingsboard.io)
#                json - Print to stdout as json encoded strings
#
#reporting_method = mqtt-json

# The bluetooth adapter that should be used to connect to Mi Flora devices (Default: hci0)
#adapter = hci0

[Daemon]

# Enable or Disable an endless execution loop (Default: true)
#enabled = true

# The period between two measurements in seconds (Default: 300)
period = 7200

[MQTT]

# The hostname or IP address of the MQTT broker to connect to (Default: localhost)
hostname = 192.168.1.1

# The TCP port the MQTT broker is listening on (Default: 1883)
#port = 1883

# Maximum period in seconds between ping messages to the broker. (Default: 60)
#keepalive = 60

# The MQTT base topic to publish all Mi Flora sensor data topics under.
# Default depends on the configured reporting_method
#base_topic = miflora                   # Default for: mqtt-json, mqtt-smarthome
#base_topic = homie                     # Default for: mqtt-homie
#base_topic = homeassistant             # Default for: homeassistant-mqtt
#base_topic = v1/devices/me/telemetry   # Default for: thingsboard-json

# Homie specific: The device ID for this daemon instance (Default: miflora-mqtt-daemon)
#homie_device_id = miflora-mqtt-daemon

# The MQTT broker authentification credentials (Default: no authentication)
#username = user
#password = pwd123

# Enable TLS/SSL on the connection
#tls = false

# Path to CA Certificate file to verify host
#tls_ca_cert =

# Path to TLS client auth key file
#tls_keyfile =

# Path to TLS client auth certificate file
#tls_certfile =

[Sensors]

# Add your Mi Flora sensors here. Each sensor consists of a name and a Ethernet MAC address.
# Additional location information can be added to the name, delimited by an '@'.
# Scan for sensors from the command line with:
#    $ sudo hcitool lescan
#
# Examples:
#
#Schefflera@Living = C4:7C:8D:11:22:33
#JapaneseBonsai    = C4:7C:8D:44:55:66
#Petunia@Balcony   = C4:7C:8D:77:88:99

Zamioculcas		= C4:7C:8D:67:2B:08
Beaucarnea 		= C4:7C:8D:67:69:8D
ChlorophytumComosum	= C4:7C:8D:67:6A:06
PothosAureus		= C4:7C:8D:67:67:F5
Sansevieria		= C4:7C:8D:67:2C:71
Schefflera		= C4:7C:8D:67:68:0A
Phalaenopsis		= C4:7C:8D:67:68:FF
Calathea		= C4:7C:8D:67:69:E0
DracaenaMarginata	= C4:7C:8D:67:4D:D2

and to finish, a very weird thing happened while I was writing this message… it worked again once and only once (I didn’t change anything, except manually relaunching the script) :frowning: and now it behaves exactly as I explained in this post.

Any idea ?
Thanks in advance for your help

Anyone using the script with the devices firmware 3.1.8? any problems?

My devices have only Firmware 2.6.2 and 2.7.0.

May I ask where you got the ones with 3.1.8 from?

I have 3.1.9 and all 4 sensors are up and running in openHAB :slight_smile:

I got this update a few weeks ago in the official Flower-Care-App

Oops, I must admit that I never once used that App :blush:

Same here. Works perfectly. :slight_smile:

I’m using the library since a couple of years. I’m still on raspbian jessie. It happened that all of a sudden sensors was not read and rebooting made it work for a while, but the time between repeated failures was shortening from several days to several hours. Several other malfunctions made me suspect about a failing SD card and, in effect, after restoring a backup on a new SD card, I experienced no sensor readout failures anymore.
LionHe

Ok, anyone got problems with connection their devices via APP? I cannot connect…
I delete the device and i can add it but then i am not able to connect to it!? Any advice maybe?

no errors when updating the item file…

Miflora systemctl is running

any idea how i can debug this and find the problem why i get no updates? maybe too dump setting up mqtt?!

Hello,

I’m trying to get the plant sensor connected to my openhab, but in basic ui, I don’t see any values.
When I try from command line, I get this output:

[2019-01-30 23:20:16] Retrieving data from sensor “Pflanze” …
[2019-01-30 23:20:18] Result: {“light”: 76, “temperature”: 14.0, “moisture”: 29, “battery”: 99, “conductivity”: 365}
[2019-01-30 23:20:18] Publishing to MQTT topic “miflora/Pflanze”

So, I think, the connection to sensor works fine.

My system is openhab 2.4 Release build on Raspberry 3B. I use the embedded MQTT broker.

My thing configuration is:

Thing mqtt:topic:mqttMiFloraPflanze "Küche - Clusia rosea Licht mqqt" {
    Channels:
        Type number : pflanzeLight "Küche - Clusia rosea Licht" [ stateTopic="miflora/Pflanze", transformationPattern="JSONPATH:$.light" ]
    }```

My item is configured as follows:

```csv
umber Kueche_Pflanze_LightIntensity "Kueche Pflanze Sunlight Intensity [%d lux]" &lt;text&gt; (gKuechePflanze, gLightIntensity) { channel="mqtt:topic:mqttMiFloraPflanze:pflanzeLight" }

In Basic UI, I’ve got the output without a value: Kueche Pflanze Sunlight Intensity - lux

I’m new to openhab and also new to MQTT. Maybe the broker does not work correctly, but I don’t know how to test this. Any help is very appreciated.

Thank you in advance. Manuel

Has anyone got the inbuilt broker in 2.4 to work and want to share their config files(thing, miflora.config and item file)?