Hi everyone!
In order to get sensor data from miflora compatible sensor devices I want to setup a MQTT server for my smarthome network, where Thomas Dietrich’s MQTT client enqueues the sensor data and my openHAB instance retrieves and provides it in items.
In order to keep the setup simple, I decided to let the MQTT server and client run on the same machine, i.e. the one that also runs openHAB. As MQTT server I’ve installed MQTT Broker Moquette vom den Misc tab in PaperUI.
To access the MQTT server I’ve additionally installed the MQTT binding from the Bindings tab in PaperUI. For parsing the JSON output I’ve installed some transformations via PaperUI aswell: Javascript, JSONPath and RegEx (JSON should be the default format stored in the broker).
I’ve created a miflora.things
:
Bridge mqtt:systemBroker:embedded-mqtt-broker "Moquette Embedded MQTT System Broker" @ "Sf" [
host="localhost",
port="1883",
retain="false",
qos="2",
keep_alive_time_ms="60",
reconnect_time_ms="10000",
url="localhost:1883",
clientid="Oh2Mqtt",
username="openhabian",
password="***"
]
{
Thing topic yucca "Yucca" @ "SrLr" {
Channels:
Type number : light "Light Intensity" [ stateTopic="miflora/Yucca", transformationPattern="JSONPATH:$.light" ]
Type number : temperature "Temperature" [ stateTopic="miflora/Yucca", transformationPattern="JSONPATH:$.temperature" ]
Type number : conductivity "Soil Fertility" [ stateTopic="miflora/Yucca", transformationPattern="JSONPATH:$.conductivity" ]
Type number : moisture "Soil Moisture" [ stateTopic="miflora/Yucca", transformationPattern="JSONPATH:$.moisture" ]
Type number : battery "Battery Charge" [ stateTopic="miflora/Yucca", transformationPattern="JSONPATH:$.battery" ]
}
}
My items files looks like this:
Number Yucca_Door_Battery "Yucca Batteriestand [%d %%]" <text> {mqtt="<[broker:miflora/Yucca:state:JSONPATH($.battery)]"}
Number Yucca_Door_Temperature " Yucca Lufttemperatur [%.1f °C]" <text> {mqtt="<[broker:miflora/Yucca:state:JSONPATH($.temperature)]"}
Number Yucca_Door_Moisture "Yucca Feuchtigkeit [%d %%]" <text> {mqtt="<[broker:miflora/Yucca:state:JSONPATH($.moisture)]"}
Number Yucca_Door_Conductivity "Yucca Fruchtbarketi [%d µS/cm]" <text> {mqtt="<[broker:miflora/Yucca:state:JSONPATH($.conductivity)]"}
Number Yucca_Door_Light "Yucca Lichteinfall [%d lux]" <text> {mqtt="<[broker:miflora/Yucca:state:JSONPATH($.light)]"}
As for the /opt/miflora-mqtt-daemon/config.ini
I’m using the following deviations from the default config:
# The period between two measurements in seconds (Default: 300)
period = 10
# The MQTT broker authentification credentials (Default: no authentication)
# Will also read from MQTT_USERNAME and MQTT_PASSWORD environment variables
username = openhabian
password = ***
[Sensors]
Yucca = C4:7C:3A:2F:92:19
First problem is that the miflora service seems to be started too early:
# 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 2020-07-05 04:00:50 CEST; 5h 44min ago
Docs: https://github.com/ThomDietrich/miflora-mqtt-daemon
Process: 1054 ExecStart=/usr/bin/python3 /opt/miflora-mqtt-daemon/miflora-mqtt-daemon.py (code=exited, status=1/FAILURE)
Main PID: 1054 (code=exited, status=1/FAILURE)
Status: "Jul 05 04:00:49 - MQTT connection error. Please check your settings in the configuration file "config.ini"."
Jul 05 04:00:49 openHABianPi systemd[1]: miflora.service: Unit entered failed state.
Jul 05 04:00:49 openHABianPi systemd[1]: miflora.service: Failed with result 'exit-code'.
Jul 05 04:00:50 openHABianPi systemd[1]: miflora.service: Service hold-off time over, scheduling restart.
Jul 05 04:00:50 openHABianPi systemd[1]: Stopped Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon.
Jul 05 04:00:50 openHABianPi systemd[1]: miflora.service: Start request repeated too quickly.
Jul 05 04:00:50 openHABianPi systemd[1]: Failed to start Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon.
Jul 05 04:00:50 openHABianPi systemd[1]: miflora.service: Unit entered failed state.
Jul 05 04:00:50 openHABianPi systemd[1]: miflora.service: Failed with result 'exit-code'.
Is there a way to have it retry connecting more often?
Retrying manually after openHAB is fully started up delivers:
# systemctl start miflora.service
# 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: active (running) since Sun 2020-07-05 09:47:07 CEST; 1s ago
Docs: https://github.com/ThomDietrich/miflora-mqtt-daemon
Main PID: 11554 (python3)
Status: "Jul 05 09:47:06 - MQTT connection established."
Tasks: 3 (limit: 4915)
CGroup: /system.slice/miflora.service
├─11554 /usr/bin/python3 /opt/miflora-mqtt-daemon/miflora-mqtt-daemon.py
└─11562 /usr/local/lib/python3.5/dist-packages/bluepy/bluepy-helper 0
Jul 05 09:47:05 openHABianPi systemd[1]: Starting Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon...
Jul 05 09:47:07 openHABianPi systemd[1]: Started Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon.
I’ve also checked that events should arrive in the queue manually:
# sudo python3 miflora-mqtt-daemon.py
Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon
Source: https://github.com/ThomDietrich/miflora-mqtt-daemon
[2020-07-05 09:49:26] Connecting to MQTT broker ...
[2020-07-05 09:49:26] MQTT connection established
Adding sensor to device list and testing connection ...
Name: "Yucca"
[2020-07-05 09:49:27] Initial connection to Mi Flora sensor "Yucca" (C4:7C:3A:2F:92:19) failed due to exception:
[2020-07-05 09:49:27] Announcing Mi Flora devices to MQTT broker for auto-discovery ...
[...]
[2020-07-05 09:49:49] Retrieving data from sensor "Yucca" ...
[2020-07-05 09:50:14] Retrying due to exception: Could not read data from Mi Flora sensor C4:7C:3A:2F:92:19
[2020-07-05 09:50:18] Result: {"light": 409, "temperature": 24.6, "moisture": 29, "conductivity": 226, "battery": 100}
[2020-07-05 09:50:18] Publishing to MQTT topic "miflora/Yucca"
Success rate of querying is not too high though, I should consider moving the Raspi closer to the devices.
Assuming that the MQTT connection to the broker/server should be fixable, let’s come to the next problem. Judging from the logs I think there’s a configuration issue in my miflora.things and some other problem related to a clientId and/or authentication, but I could not come up with a solution:
openhab.log
:
2020-07-05 10:23:51.984 [INFO ] [roker.internal.EmbeddedBrokerService] - Broker persistence file: mqttembedded.bin
2020-07-05 10:23:56.414 [INFO ] [.transport.mqtt.MqttBrokerConnection] - Starting MQTT broker connection to 'localhost' with clientid embedded-mqtt-broker
events.log
:
2020-07-05 10:24:53.506 [hingStatusInfoChangedEvent] - 'mqtt:systemBroker:embedded-mqtt-broker' changed from UNINITIALIZED to INITIALIZING
2020-07-05 10:24:53.532 [hingStatusInfoChangedEvent] - 'mqtt:systemBroker:embedded-mqtt-broker' changed from INITIALIZING to ONLINE
2020-07-05 10:25:08.964 [hingStatusInfoChangedEvent] - 'mqtt:topic:embedded-mqtt-broker:yucca' changed from UNINITIALIZED to INITIALIZING
2020-07-05 10:25:09.291 [hingStatusInfoChangedEvent] - 'mqtt:topic:embedded-mqtt-broker:yucca' changed from INITIALIZING to ONLINE
2020-07-05 10:30:07.257 [WARN ] [io.moquette.broker.MQTTConnection ] - Broker doesn't permit MQTT empty client ID. Username: openhabian, channel: [id: 0x16af6309, L:/0:0:0:0:0:0:0:1:1883 - R:/0:0:0:0:0:0:0:1:48763]
I did some research here in the forums but could not find anything beyond “use another broker” regarding the empty client id. Since Moquetto was introduced in openHAB 2.5 I think that this shouldn’t be necessary – or is it known to malfunction and shouldn’t be included in this version?
Platform information:
Hardware: Raspberry Pi 3 Model B Plus Rev 1.3
OS: openHABian / Linux 4.19.66-v7+
Java Runtime Environment: OpenJDK Runtime Environment (Zulu8.40.0.178-CA-linux_aarch32hf) (build 1.8.0_222-b178)
openHAB version: 2.5.3-1