Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon

mqtt
xiaomi
miflora
Tags: #<Tag:0x00007f01527dd598> #<Tag:0x00007f01527dca58> #<Tag:0x00007f01527dc670>

(Patrick) #101

I think that’s not possible at the moment, but a feature request was already submitted for that:


(Klim Bim) #102

But I saw somewhere in old posts that they could see the connection quality. Was that taken out again?
It`s very hard for me to currently find out why it would not work through a window. It did sometimes and then all of a sudden it does not again?! Any idea?


(Thomas Bail) #103

What kind of window do you have. I am moving from the miflora daemon to the Bluetooth binding from @vkolotov and while this i regocnized that the sensore on our veranda are nearly never reached. In the blutooth diskussion some other people have the same problem. Modern Windows …


(Klim Bim) #104

Good question, I guess they are 10 years old… the other sensor ist through one thick wall of concrete, direct distance lets say 6 meters?

What benefits do you have by moving to the bluetooth binding?


(Thomas Bail) #105

If your windows are about 10 years old, then they are maybe the problem.

There is no particular reason to move to the bluetooth binding except from tha fact that the bluetooth binding does not only recognise the miflora sensors


(Carsten) #106

Hey guys…

I am still not able to connect one of my flora sensors… when i search via the hcitool i found booth of them…
I also stand the “problem” sensor in front of my PI…no connection possible…any ideas?

[13:39:44] openhabian@openHABianPi:~$ 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-07-02 13:39:45] Connecting to MQTT broker ...
[2018-07-02 13:39:45] MQTT connection established

Adding sensor to device list and testing connection ...
Name:          "Spathiphyllum"
connect: Device or resource busy (16)
connect: Device or resource busy (16)
[2018-07-02 13:40:56] Initial connection to Mi Flora sensor "Spathiphyllum" (C4:7C:8D:65:DE:85) failed.

Adding sensor to device list and testing connection ...
Name:          "Begonia_Corallina"
Internal name: "Begonia_Corallina"
Device name:   "Flower care"
MAC address:   C4:7C:8D:65:DE:9F
Firmware:      3.1.8
[2018-07-02 13:41:02] Initial connection to Mi Flora sensor "Begonia_Corallina" (C4:7C:8D:65:DE:9F) successful

[2018-07-02 13:41:02] Announcing Mi Flora devices to MQTT broker for auto-discovery ...

[2018-07-02 13:41:02] Retrieving data from sensor "Spathiphyllum" ...
connect: Device or resource busy (16)
connect: Device or resource busy (16)
connect: Device or resource busy (16)
[2018-07-02 13:41:52] Retrying ...
connect: Device or resource busy (16)

Another try…

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

[2018-07-02 13:45:38] Connecting to MQTT broker ...
[2018-07-02 13:45:38] MQTT connection established

Adding sensor to device list and testing connection ...
Name:          "Spathiphyllum"
[2018-07-02 13:47:30] Initial connection to Mi Flora sensor "Spathiphyllum" (C4:7C:8D:65:DE:85) failed.

Adding sensor to device list and testing connection ...
Name:          "Begonia_Corallina"
Characteristic value/descriptor read failed: Internal application error: I/O
connect error: Transport endpoint is not connected (107)
Internal name: "Begonia_Corallina"
Device name:   "Flower care"
MAC address:   C4:7C:8D:65:DE:9F
Firmware:      3.1.8
[2018-07-02 13:48:32] Initial connection to Mi Flora sensor "Begonia_Corallina" (C4:7C:8D:65:DE:9F) successful

[2018-07-02 13:48:32] Announcing Mi Flora devices to MQTT broker for auto-discovery ...

[2018-07-02 13:48:33] Retrieving data from sensor "Spathiphyllum" ...
[2018-07-02 13:50:23] Retrying ...
[2018-07-02 13:52:13] Failed to retrieve data from Mi Flora sensor "Spathiphyllum" (C4:7C:8D:65:DE:85), success rate: 0%

[2018-07-02 13:52:13] Retrieving data from sensor "Begonia_Corallina" ...
[2018-07-02 13:52:17] Result: {"light": 987, "temperature": 23.9, "moisture": 28, "conductivity": 324, "battery": 99}
[2018-07-02 13:52:17] Publishing to MQTT topic "miflora/Begonia_Corallina"

[2018-07-02 13:52:18] Sleeping (300 seconds) ...

[2018-07-02 13:57:18] Retrieving data from sensor "Spathiphyllum" ...


(Matthias) #107

Hi guys,
thanks for the great work. I got everything working except the daemon. I get the following error:

pi@raspberrypi:~ $ sudo systemctl start miflora.service
Job for miflora.service failed because the control process exited with error code.
See "systemctl status miflora.service" and "journalctl -xe" for details.
pi@raspberrypi:~ $ sudo systemctl status miflora.service
● miflora.service - Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon
   Loaded: loaded (/etc/systemd/system/miflora.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Sun 2018-07-08 15:34:49 UTC; 3s ago
     Docs: https://github.com/ThomDietrich/miflora-mqtt-daemon
  Process: 912 ExecStart=/opt/miflora-mqtt-daemon/miflora-mqtt-daemon.py (code=exited, status=203/EXEC)
 Main PID: 912 (code=exited, status=203/EXEC)

Jul 08 15:34:48 raspberrypi systemd[1]: miflora.service: Unit entered failed state.
Jul 08 15:34:48 raspberrypi systemd[1]: miflora.service: Failed with result 'exit-code'.
Jul 08 15:34:49 raspberrypi systemd[1]: miflora.service: Service hold-off time over, scheduling restart.
Jul 08 15:34:49 raspberrypi systemd[1]: Stopped Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon.
Jul 08 15:34:49 raspberrypi systemd[1]: miflora.service: Start request repeated too quickly.
Jul 08 15:34:49 raspberrypi systemd[1]: Failed to start Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon.
Jul 08 15:34:49 raspberrypi systemd[1]: miflora.service: Unit entered failed state.
Jul 08 15:34:49 raspberrypi systemd[1]: miflora.service: Failed with result 'exit-code'.

config.ini has enabled the daemon. I have a Rasperry Zero W.
Can someone help to get the script started as service?
My Linux knowledge is unfortunately very limited.
Thanks,
Matthias


(Klim Bim) #108

Pls post your config…


(Klim Bim) #109

Could you please as well post your config?


(Carsten) #110
[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, json encoded (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)
#           json - Print to stdout as json encoded string
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 = 300

[MQTT]

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

# The TCP port the MQTT broker is listening on. SSL/TLS currently not implemented (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_mode (mqtt-json, mqtt-smarthome: miflora, mqtt-homie: homie)
base_topic = miflora
#base_topic = homie

# 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 = openhabian
password = openhabian#0707

[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:
#
#Spathiphyllum@Bathroom = C4:7C:8D:65:DE:85
begonia_corallina@Office = C4:7C:8D:65:DE:9F

#
# Define your MQTT broker connections here for use in the MQTT Binding or MQTT
# Persistence bundles. Replace <broker> with an ID you choose.
#

# URL to the MQTT broker, e.g. tcp://localhost:1883 or ssl://localhost:8883
miflora.url=tcp://localhost:1883

# Optional. Client id (max 23 chars) to use when connecting to the broker.
# If not provided a random default is generated.
miflora.clientId=miflora

# Optional. True or false. If set to true, allows the use of clientId values
# up to 65535 characters long. Defaults to false.
# NOTE: clientId values longer than 23 characters may not be supported by all
# MQTT servers. Check the server documentation.
#<broker>.allowLongerClientIds=false

# Optional. User id to authenticate with the broker.
miflora.user=openhabian

# Optional. Password to authenticate with the broker.
miflora.pwd=open

# Optional. Set the quality of service level for sending messages to this broker.
# Possible values are 0 (Deliver at most once),1 (Deliver at least once) or 2
# (Deliver exactly once). Defaults to 0.
#<broker>.qos=<qos>

# Optional. True or false. Defines if the broker should retain the messages sent to
# it. Defaults to false.
#<broker>.retain=<retain>

# Optional. True or false. Defines if messages are published asynchronously or
# synchronously. Defaults to true.
#<broker>.async=<async>

# Optional. Defines the last will and testament that is sent when this client goes offline
# Format: topic:message:qos:retained <br/>
#<broker>.lwt=<last will definition>

// Mi Flora "Begonia_Corallina" (C4:7C:8D:65:DE:9F)
Number Office_begonia_corallina_LightIntensity "Office begonia_corallina Sunlight Intensity [%d lux]" <text> (gOfficebegonia_corallina, gLightIntensity) {mqtt="<[broker:miflora/begonia_corallina:state:JSONPATH($.light)]"}
Number Office_begonia_corallina_AirTemperature "Office begonia_corallina Air Temperature [%.1f °C]" <text> (gOfficebegonia_corallina, gAirTemperature) {mqtt="<[broker:miflora/begonia_corallina:state:JSONPATH($.temperature)]"}
Number Office_begonia_corallina_SoilMoisture "Office begonia_corallina Soil Moisture [%d %%]" <text> (gOfficebegonia_corallina, gSoilMoisture) {mqtt="<[broker:miflora/begonia_corallina:state:JSONPATH($.moisture)]"}
Number Office_begonia_corallina_SoilConductivity "Office begonia_corallina Soil Conductivity/Fertility [%d µS/cm]" <text> (gOfficebegonia_corallina, gSoilConductivity) {mqtt="<[broker:miflora/begonia_corallina:state:JSONPATH($.conductivity)]"}
Number Office_begonia_corallina_Battery "Office begonia_corallina Sensor Battery Level [%d %%]" <text> (gOfficebegonia_corallina, gBattery) {mqtt="<[broker:miflora/begonia_corallina:state:JSONPATH($.battery)]"}

My problems are…that the one above got connections but I am not able to get the items filled with values…

The Problem with the other sensor is that I will not get any data…


(Klim Bim) #111

Any moaning of the syslog if you save your .items file where these are defined? Miflora systemctl is running? Pythonscript is working?

Is there too much distance or other stuff in between Sensor and receiver (Raspi?)?


(FK) #112

Dear all,

thanks for this, I did step by step and was able to fix some issues, but whenever I try to run the test:
python3 /opt/miflora-mqtt-daemon/miflora-mqtt-daemon.py

I get a file not found. What am I missing?

Thanks!


(Olaf Schwenk) #113

Outstanding work! Thank you! Works perfect! :slight_smile:


(Kasra) #114

It always gives me the same information and none of the values (battery, conductivity, etc.) changes. This is my config file:

# 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/)
#                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 = 5

[MQTT]

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

# 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_mode (mqtt-json, mqtt-smarthome: miflora, mqtt-homie: homie)
#base_topic = miflora
#base_topic = homie

# 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 = myUser
password = myPassword

# 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:
#
Flower mate = C4:7C:8D:61:FC:F2
#JapaneseBonsai    = C4:7C:8D:44:55:66
#Petunia@Balcony   = C4:7C:8D:77:88:99


(Clint Crowe) #115

Hi @skatun i tried your rule below and I am getting an error and its not sending an email. Not sure what this error means?

val Boolean emailSent = true

rule "Water plants"
when
	Item Greenhouse_GreenhouseOne_SoilMoisture  changed
then
	if (Greenhouse_GreenhouseOne_SoilMoisture.state <15 && !emailSent){
	    sendMail("myemail@gmail.com","Plant","Water plants")
    }
end

error:

[el.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model 'waterplants.rules', using it anyway:

Constant condition is always false.

thanks!


(Clint Crowe) #116

I want to convert the temp to Fahrenheit. I read through some posts and they mostly talk about using a rule to do that? Is there not another way to convert? I also am persisting this so I saw where you had to use a rule to persist to change it so that it would persist as Fahrenheit (you could change in sitemap but it would not persist that way)? If not, would be awesome to have that as an option in the config!

thanks!


(David K.) #117

I’ve tried to connect the Daemon to the new OH2.4 MQTT embedded broker.

However the daemon does not set a client ID and the embedded broker somehow can’t handle that. Is there a way to specify the MQTT daemon client ID?

Thanks!


(Clint Crowe) #118

Was able to rebuild the rule and get it to work. One issue was the VAL vs VAR…

var Boolean emailSent = true

rule "Water Plants Notification"
when
	Item Greenhouse_GreenhouseOne_SoilMoisture  changed
then
	if (Greenhouse_GreenhouseOne_SoilMoisture.state <10 && emailSent){
	    logInfo("Water Plants", "Water Table One")
		sendNotification("myemail@gmail.com", "Water Table One")
		sendMail("myemail@gmail.com", "Plants", "Water Table One")
		emailSent=false
    }		
	else {
		emailSent=true
	}
end

(Killercorny) #119

Successfully got this working out of the Box. My Mi flora are running the latest Firmware 3.1.9 and using an extra BT adapter with my Pi 2. Everythig is working well…
@ThomDietrich Big thanks to you!


(Klim Bim) #120

Can someone tell me why this is showing up in syslog?

Dec  3 16:56:43 openHABianPi miflora-mqtt-daemon.py[3537]: Traceback (most recent call last): 
Dec  3 16:56:43 openHABianPi miflora-mqtt-daemon.py[3537]:   File "/opt/miflora-mqtt-daemon/miflora-mqtt-    daemon.py", line 218, in <module> 
Dec  3 16:56:43 openHABianPi miflora-mqtt-daemon.py[3537]:     flora_poller.fill_cache() 
Dec  3 16:56:43 openHABianPi miflora-mqtt-daemon.py[3537]:   File "/usr/local/lib/python3.5/dist-packages/miflora/miflora_poller.py", line 61, in fill_cache 
Dec  3 16:56:43 openHABianPi miflora-mqtt-daemon.py[3537]:     firmware_version = self.firmware_version() 
Dec  3 16:56:43 openHABianPi miflora-mqtt-daemon.py[3537]:   File "/usr/local/lib/python3.5/dist-packages/miflora/miflora_poller.py", line 104, in firmware_version 
Dec  3 16:56:43 openHABianPi miflora-mqtt-daemon.py[3537]:     res = connection.read_handle(_HANDLE_READ_VERSION_BATTERY)  # pylint: disable=no-member 
Dec  3 16:56:43 openHABianPi miflora-mqtt-daemon.py[3537]:   File "/usr/local/lib/python3.5/dist-packages/btlewrap/gatttool.py", line 23, in _func_wrapper 
Dec  3 16:56:43 openHABianPi miflora-mqtt-daemon.py[3537]:     return func(*args, **kwargs) 
Dec  3 16:56:43 openHABianPi miflora-mqtt-daemon.py[3537]:   File "/usr/local/lib/python3.5/dist-packages/btlewrap/gatttool.py", line 253, in read_handle 
Dec  3 16:56:43 openHABianPi miflora-mqtt-daemon.py[3537]:     raise BluetoothBackendException("Exit read_ble, no data ({})".format(current_thread())) 
Dec  3 16:56:43 openHABianPi miflora-mqtt-daemon.py[3537]: btlewrap.base.BluetoothBackendException: Exit read_ble, no data (<_MainThread(MainThread, started 1994330128)>)
Dec  3 16:56:43 openHABianPi systemd[1]: miflora.service: Main process exited, code=exited, status=1/FAILURE 
Dec  3 16:56:43 openHABianPi systemd[1]: miflora.service: Unit entered failed state. 
Dec  3 16:56:43 openHABianPi systemd[1]: miflora.service: Failed with result 'exit-code'. 
Dec  3 16:56:43 openHABianPi systemd[1]: miflora.service: Service hold-off time over, scheduling restart. 
Dec  3 16:56:43 openHABianPi systemd[1]: Stopped Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon. 
Dec  3 16:56:43 openHABianPi systemd[1]: Starting Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon... 
Dec  3 16:56:45 openHABianPi systemd[1]: Started Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon.