Module MQTT seems to generate protocol errors at mosquitto broker level, looping connection/disconnection, as a result FHEM gets delayed while establishing the connection.
Using MQTT2.
I have a few devices that send topics on their own, as a result on FHEM must have one device per topic. To circumvent the problem, a signel device can be created and the subtopic values can be stored in the device’s readings
Example:
A Smart meter deice that sends the reading on subtopics of sensors/power/p1meter/
The MQTT2 broker has to subscribe to sensors/power/p1meter/#
The mosquitto broker running on the fhem machine and subscribing topics:
sensor/PIR2/#
nucpc_plug/# sma/solar/#
sensors/power/p1meter/#
define myBroker MQTT2_CLIENT 127.0.0.1:1883 attr myBroker clientId myBroker attr myBroker clientOrder MQTT_GENERIC_BRIDGE MQTT2_DEVICE attr myBroker icon mqtt attr myBroker lwt system/raspdomo/connection/status connection lost attr myBroker lwtRetain 1 attr myBroker room MQTT attr myBroker subscriptions sensor/PIR2/# nucpc_plug/# sma/solar/# sensors/power/p1meter/# attr myBroker username jcoenen
The devices corresponding to the subscribed topics
define P1meter MQTT2_DEVICE
attr P1meter readingList sensors/power/p1meter/[^:]+:.* { my @toptree=split('/',$TOPIC);; return {$toptree[$#toptree] => $EVENT} }
define PIR_Bureau MQTT2_DEVICE
setuuid PIR_Bureau 69bfe863-f33f-7d6a-7323-f72cf14ea6e1a18f
attr PIR_Bureau disable 0
attr PIR_Bureau readingList sensor/PIR2/[^:]+:.* { my @toptree=split('/',$TOPIC);;;; return {$toptree[$#toptree] => $EVENT} }\
sensor/PIR2/[^:]+:.* { return {event => $TOPIC}}
attr PIR_Bureau room Bureau,MQTT2_DEVICE
attr PIR_Bureau stateFormat motion
define SMAsolar MQTT2_DEVICE
setuuid SMAsolar 69bfe94e-f33f-7d6a-23ab-6416b97f260c885a
attr SMAsolar disable 0
attr SMAsolar readingList sma/solar/[^:]+:.* { my @toptree=split('/',$TOPIC);;;; return {$toptree[$#toptree] => $EVENT} }
attr SMAsolar room MQTT2_DEVICE,Photovoltaïque,Power
attr SMAsolar stateFormat instant_ac (Watts)
This is a obk firmare that generates subtopics with more layers like this
nucpc_plug
connected = online
temp = 0.00
ssid = Malperthuis
power
get = 19.50
sockets = 2
rssi = -50
voltage
get = 251.5
current
get = 0.142
power_apparent
get = 35.79
uptime = 1608812
power_reactive
get = 30.93
power_factor
get = 0.54
ip = 192.168.1.242
so for example the power will be on nucpc_plug/power/get
To generate the reading power an additional test miusr be performed in the perl code.
define nucpc_plug MQTT2_DEVICE
setuuid nucpc_plug 69bff250-f33f-7d6a-2330-33645447a8605029
attr nucpc_plug disable 0
attr nucpc_plug eventMap { dev=>{ 'on'=>'1', 'off'=>'0' } }
attr nucpc_plug readingList nucpc_plug[^:]+:.* { my @toptree=split('/',$TOPIC);;;; if ($toptree[$#toptree] eq "get") { return {$toptree[@toptree-2] => $EVENT}} else {return {$toptree[$#toptree] => $EVENT}}}
attr nucpc_plug room Bureau,Computers,MQTT2_DEVICE
attr nucpc_plug setList on nucpc_plug/switch 1\
off nucpc_plug/switch 0
When a topic is returned that holds a json formatted string, to put the values of the json in the device readings like this:
attr Meteo readingList myBroker:on4kch/meteo/speed:.* { json2nameValue($EVENT) }