[SOLVED] Mosquitto MQTT cannot get service to run

correct

I have not been able to work on this but with winter pending, I need to get a few temperature devices up and running.
I’ve installed MQTT Embedded Broker service and beyond setting the Embedded user brokername (MQTT temporarily during testing) and password I haven’t made any setting changes.
I also have the MQTT system broker connection service and as far as I can tell, don’t need to make any setting changes.
My test device is a Sonoff TH16 with Si7021 temperature sensor that’s been reprogrammed with Tasmota firmware. The Sonoff seems to be working just fine, I can log into it via my web browser, screen shot of the MQTT configuration below.
The Sonoff isn’t connecting to the MQTT broker; the blue indicator light continues to flash and the log reading:
“19:48:55 MQT: Connect failed to 127.0.0.1:1883, rc -2. Retry in 10 sec”
continues to log.
Question: Is 127.0.0.0 the correct connection address? I don’t see this setting in the MQTT broker.

You need to set the Host to the IP of the system where the broker is running. Setting it to 127.0.0.1 means localhost, in your case that would be the Sonoff-Device!!

1 Like

127.0.0.1 = loclhost = the network card you are using right now…
ifconfig will show your IP(you need the IP of your broker , the same machine runing MQTT)

try to watch some basic networking youtubes, its the basic for home auto…

Ok, great news. Entering the appropriate IP address of my host computer did the trick. I can see the Sonoff is connected by using MQTT.fx. One step down!

Now, onto the .things and .items configuration. The youtube videos are little help as I can’t see what they’re entering and their web sites have no good info (at least the videos I’ve seen). I’ve been following this guide:


but am unsure of what terms all need to match, between the Sonoff configuration, the myMQTTBroker thing properties and the .things and .items entries. Does anyone have a good example I can follow?
I’m also using a TH16 with an Si7021 sensor, and am not quite sure those values are right (I can find no examples that use this particular temp/humidity sensor).
Thanks again,

Read Here to find an example of the file setup.
For the setup via PaperUI take the settings found in the files-example.

what is the “clientID” within the thing file? No matter what I put in there I get a log error "Session does not exist. CId=“whatever I put in there”

Here is an example from my own Setup:

.items:

//192.xxx.xxx.xx                            Steckdosenleiste/Power Strip  TH16

Switch    Sonoff_PowerStrip_01          "Steckdosenleiste 1 [MAP(de.map):%s]"             <poweroutlet>           (EG_Kind1,gPlugSwitch,gSonoff)  { channel="mqtt:topic:hans:TH1601:power"}               
Number    Sonoff_PowerStrip_01_RSSI     "Steckdosenleiste 1 RSSI [%s]"                    <qualityofservice>      (EG_Kind1,gRSSI)                { channel="mqtt:topic:hans:TH1601:rssi" }               
String    Sonoff_PowerStrip_01_Version  "Steckdosenleiste 1 Version [%s]"                 <sonoff_pow>            (gSonoffs,gVer)                 { channel="mqtt:topic:hans:TH1601:version" }            
Switch    Sonoff_PowerStrip_01_Unreach  "Steckdosenleiste 1 Erreichbarkeit [%s]"          <siren1>                (EG_Kind1,gLWT)                 { channel="mqtt:topic:hans:TH1601:reachable" }          
//        For Fun and Test                                                                                                                                  
Number    Sonoff_PowerStrip_01_Temp     "Steckdosenleiste 1 Temperatur[%.1f °C]"          <temperature>           (gSonoffs,EG_Kind1,gRtIstI)     { channel="mqtt:topic:hans:TH1601:temperature"}               
Number    Sonoff_PowerStrip_01_Hum      "Steckdosenleiste 1 Luftfeuchtigkeit[%.2f %%]"    <humidity>              (gSonoffs,EG_Kind1)             { channel="mqtt:topic:hans:TH1601:humidity" }               
String    Sonoff_PowerStrip_01_SSID     "Steckdosenleiste 1 Wlan [%s]"                    <chart>                 (gSonoffs)                      { channel="mqtt:topic:hans:TH1601:ssid" }            
DateTime  Sonoff_PowerStrip_01_Date     "Steckdosenleiste 1 Refresh [ %1$tH:%1$tM]"       <time>                  (gSonoffs)                      { channel="mqtt:topic:hans:TH1601:time" }          

.things (including a Bridge)

Bridge mqtt:broker:hans "Mosquitto" [ host="localhost", port=1883, secure=false, clientID="OH2_Client" ]  
{
    //Gruppe TH10/16 Monitoring WiFi Smart Switch
    Thing topic TH1601 "Sonoff TH16 01" @ "MQTT2" {
    Channels:
        Type switch : power       "Power "               [ stateTopic="stat/th16_01/POWER", commandTopic="cmnd/th16_01/POWER" ]
        Type number : rssi        "WiFi Signal Strength" [ stateTopic="tele/th16_01/STATE", transformationPattern="JSONPATH:$.Wifi.RSSI"]
        Type string : version     "Firmware Version"     [ stateTopic="stat/th16_01/STATUS2", transformationPattern="JSONPATH:$.StatusFWR.Version"]
        Type switch : reachable   "Reachable"            [ stateTopic="tele/th16_01/LWT", transformationPattern="MAP:reachable.map" ]
        Type number : temperature "Temperature"          [ stateTopic="tele/th16_01/SENSOR", transformationPattern="JSONPATH:$.AM2301.Temperature"]
        Type number : humidity    "Humidity"             [ stateTopic="tele/th16_01/SENSOR", transformationPattern="JSONPATH:$.AM2301.Humidity"]
        Type string : ssid        "WiFi"                 [ stateTopic="tele/th16_01/STATE", transformationPattern="JSONPATH:$.Wifi.SSId"]
        Type datetime : time      "Time"                 [ stateTopic="tele/th16_01/STATE", transformationPattern="JSONPATH:$.Time"]
    }
}

Hope this helps.

1 Like

Thanks, Peter. I am seeing some other details that will help me later but it is not helping my current problem.
I am thinking it is something to do with the Client ID (or at least that is one problem). I’ve seen a number of different names used in .items files but I continue to get an error with any entry I provide:
Session does not exist. CId=openhab
for example when I list clientID=“openhab”
Is this client ID entered somewhere else? The only other place I see it listed is in the MQTT broker configurations, but it’s not allowing me to enter a value there. Also does this correspond to a setting on the Sonoff?
Thanks again,

Each device has to have its own ClientID, in other words the same ClientID must not be used by two devices.

1 Like

As Jürgen said

I think the clientID is not mandatory in the Bridge/Thing so omit it.

1 Like

Within the MQTT Broker thing, some text beneath the Client ID setting states:“Use a fixed client ID. Defaults to empty which means a user ID is generated for this connection.” So that must be hapening because when deleting the clientID within the Bridge statement of the .things file, I get the following error:

Session does not exist. CId=paho51621326352350

!!!
So now I have to believe something else is holding this up. Perhaps something in the “MQTT Embedded Broker” service or the “MQTT system broker connection” service? There aren’t much for settings in those services however…

I’m going through everything I have installed and verifying their settings:

  1. Under Services/MQTT I have MQTT Embedded Broker and MQTT system broker connections installed. The only field filled under the embedded broker is the name of the persistence file (mqttembedded.bin).

  2. The following things are installed:
    image
    The first is a thing that I tried installing via PaperUI, but am now unable to install (when I try I get Error: 409 - Conflict).
    The MQTT Broker configuration settings are:

  • Broker Hostname/IP: 127.0.0.1
  • Broker Port: 1883
  • Client ID: paho51621326352350 (system generated)

And that’s all that I can see. I was “hoping” to find some conflict but I can’t find any differences from online guides.

Not sure if you have a special case and this is in purpose, but right now you have two MQTT brokers configured, which will be rather challenging to configure.
Remember, the basic information flow is device <–> MQTT broker <–> OH2

In the best case you have now:
device1 <–> MQTT embedded <–> OH2
device2 <–> MQTT Mosquitto <-------^

While OH2 can likely handle two brokers, it will be challenging to remember who does what (unless you have a special case and this is on purpose). OH2 provides an embedded broker for convenience and hence, maybe you want to delete the Mosquitto broker you had set up (or vice versa if this is your preference).
A few more things to remember:

  • 127.0.0.1 always points to the local machine (whichever that is); if you are unsure, just use the real IP address in all cases.
  • Clear the Cache can be your friend if you work on configs a lot and get weird results
  • in any case, do it step-wise: first check if you device connects to the broker, than check if OH2 connects to the broker; and if both, check then you get to content and items, etc. You can use a separate program to connect to your broker with your computer and see what is happening: MQTT Exploration App (as well as read incoming commands or post MQTT messages)

I’ve made the change to 2.5.0.M3, removed and reinstalled the embedded broker and binding. I did not install the auto-generated thing and am configuring it in a thing file. As of today my device (TH16 with SI7021 sensor) is at least partially working; I can control the switch (super happy about that!) but cannot pull the sensor data out. The current entries in the .things files are:

Bridge mqtt:broker:mqttbroker "MQTT Broker" [ host="127.0.0.1", secure=false, port=1883, qos=0, retain=false, clientid="openhab", keep_alive_time=30000, reconnect_time=60000 ]
{
     Thing topic Sonoff_Coop "Sonoff Coop" @ "openhab" {
    Channels:
        Type switch : power "Power" [ stateTopic="stat/Sonoff_Coop/POWER", commandTopic="cmnd/Sonoff_Coop/POWER" ]
    }
}

and a .item as:

Switch Sonoff_Coop_Power "Coop Switch" <switch> { channel="mqtt:topic:mqttbroker:Sonoff_Coop:power"}

I can control the switch remotely any all is happy. When I begin to attempt to create a Channel to pull out the temperature, any configuration I try either does nothing or messes up the operation of the switch.
The Sonoff TH is sending out the following string from its sensor, seen from the Sonoff console:

06:45:32 MQT: tele/Sonoff_Coop/SENSOR = {"Time":"2019-10-06T06:45:32","SI7021":{"Temperature":77.0,"Humidity":53.6},"TempUnit":"F"}

I’ve tried the following channel configurations (separately of course) without luck (I have the JSON transformation installed):

Type number : Temperature "Temperature" [ stateTopic="tele/Sonoff_Coop/SENSOR:SI7021:Temperature"]
Type number : temperature "Temperature" [ stateTopic="tele/Sonoff_Coop/SENSOR", transformationPattern="JSONPATH:$.SI7021.Temperature"]

I’ve found some variations out there in the community pages but none work to pull the temperature number over.

Have you installed the JSONPATH transformation service?

Yes, and uninstalled/reinstalled JSONPath after the 2.5.0.M3 update to be sure the latest was there

The second version should work. I have it working like that with the AM2301 sensor.

I agree, I think it should! But it immediately breaks the switch and still doesn’t transmit the temperature! I’d actually be ok without the switch working and in fact tried leaving the temperature channel and removed the switch channel, and it still doesn’t transmit…
Could the JSON transform be malfunctioning somehow?

You have to define your Thing like this:

    //Gruppe TH10/16 Monitoring WiFi Smart Switch
    Thing topic TH1601 "Sonoff TH16 01" @ "MQTT2" {
    Channels:
        Type switch : power       "Power "               [ stateTopic="stat/th16_01/POWER", commandTopic="cmnd/th16_01/POWER" ]
        Type number : rssi        "WiFi Signal Strength" [ stateTopic="tele/th16_01/STATE", transformationPattern="JSONPATH:$.Wifi.RSSI"]
        Type string : version     "Firmware Version"     [ stateTopic="stat/th16_01/STATUS2", transformationPattern="JSONPATH:$.StatusFWR.Version"]
        Type switch : reachable   "Reachable"            [ stateTopic="tele/th16_01/LWT", transformationPattern="MAP:reachable.map" ]
        Type number : temperature "Temperature"          [ stateTopic="tele/th16_01/SENSOR", transformationPattern="JSONPATH:$.AM2301.Temperature"]
        Type number : humidity    "Humidity"             [ stateTopic="tele/th16_01/SENSOR", transformationPattern="JSONPATH:$.AM2301.Humidity"]
        Type string : ssid        "WiFi"                 [ stateTopic="tele/th16_01/STATE", transformationPattern="JSONPATH:$.Wifi.SSId"]
        Type datetime : time      "Time"                 [ stateTopic="tele/th16_01/STATE", transformationPattern="JSONPATH:$.Time"]
    }

and the corresponding items look like:

//**********************************************************************************************************************************************************************************************************************
//Gruppe TH10/16 Sensoren
//**********************************************************************************************************************************************************************************************************************
//192.168.x.y                            Steckdosenleiste/Power Strip  TH16

Switch    Sonoff_PowerStrip_01          "Steckdosenleiste 1 [MAP(de.map):%s]"             <poweroutlet>           (EG_Kind1,gPlugSwitch,gSonoff)  { channel="mqtt:topic:hans:TH1601:power"}               
Number    Sonoff_PowerStrip_01_RSSI     "Steckdosenleiste 1 RSSI [%s]"                    <qualityofservice>      (EG_Kind1,gRSSI)                { channel="mqtt:topic:hans:TH1601:rssi" }               
String    Sonoff_PowerStrip_01_Version  "Steckdosenleiste 1 Version [%s]"                 <sonoff_pow>            (gSonoffs,gVer)                 { channel="mqtt:topic:hans:TH1601:version" }            
Switch    Sonoff_PowerStrip_01_Unreach  "Steckdosenleiste 1 Erreichbarkeit [%s]"          <siren1>                (EG_Kind1,gLWT)                 { channel="mqtt:topic:hans:TH1601:reachable" }          
//        For Fun and Test                                                                                                                                  
Number    Sonoff_PowerStrip_01_Temp     "Steckdosenleiste 1 Temperatur[%.1f °C]"          <temperature>           (gSonoffs,EG_Kind1,gRtIstI)     { channel="mqtt:topic:hans:TH1601:temperature"}               
Number    Sonoff_PowerStrip_01_Hum      "Steckdosenleiste 1 Luftfeuchtigkeit[%.2f %%]"    <humidity>              (gSonoffs,EG_Kind1)             { channel="mqtt:topic:hans:TH1601:humidity" }               
String    Sonoff_PowerStrip_01_SSID     "Steckdosenleiste 1 Wlan [%s]"                    <chart>                 (gSonoffs)                      { channel="mqtt:topic:hans:TH1601:ssid" }            
DateTime  Sonoff_PowerStrip_01_Date     "Steckdosenleiste 1 Refresh [ %1$tH:%1$tM]"       <time>                  (gSonoffs)                      { channel="mqtt:topic:hans:TH1601:time" }          
//Number    Sonoff_PowerStrip_01_VCC      "Steckdosenleiste 1 VCC[%.2f V]"                  <energy>                (gSonoffs)    // doesn't work any longer
//**********************************************************************************************************************************************************************************************************************