Sonoff Tasmota with MQTT Binding 2.4 (using config files)

The new MQTT Binding supports auto-discovery via the Homie 3.x convention and via the HomeAssistant MQTT specification. Of the 2 options, the Homie convention is better for use with OpenHAB as it allows for full device capabilities to be auto-discovered.

Theo Arends’ wonderful Tasmota firmware - latest version is 6.4.1.1 - does not currently support the Homie MQTT convention. Hopefully that will change in the future. Tasmota does support HomeAssistant auto-discovery, if that meets your requirements. For now though, I just wanted to update my Tasmota items files to work with the new MQTT Binding. (I know that MQTT Things can now be configured using the GUI but, color me old-fashioned, I still prefer configuration files.)

Here is my working solution. It uses the Mosquitto broker that had already been installed before the recent update, not the new embedded one.

Take a look at your old mqtt.cfg. These settings will be used again when creating a new broker Thing:

broker.url=tcp://localhost:1883
broker.user=USERNAME
broker.pwd=PASSWORD

Now let’s say you have a sonoff.items file with contents like this:

Group  GF_Sonoff_S20           "Sonoff S20"                        <sonoff_s20_uk> (Sonoff_S20)
String GF_Sonoff_S20_Reachable "Reachable: [%s]"                   <contactable>   (GF_Sonoff_S20) {mqtt="<[broker:tele/sonoff-s20/LWT:state:default]" }
Switch GF_Sonoff_S20_Power     "Power"                             <sonoff_s20_uk> (GF_Sonoff_S20) {mqtt=">[broker:cmnd/sonoff-s20/POWER:command:*:default], <[broker:stat/sonoff-s20/POWER:state:default]"}       
Number GF_Sonoff_S20_RSSI      "WiFi Signal Strength [%d %%]"      <wifi>          (GF_Sonoff_S20) {mqtt="<[broker:tele/sonoff-s20/STATE:state:JSONPATH($.Wifi.RSSI)]" }

Group  FF_Sonoff_B1            "Sonoff B1"                          <sonoff_b1>     (Sonoff_B1)
String FF_Sonoff_B1_Reachable  "Reachable: [%s]"                    <contactable>   (FF_Sonoff_B1)  {mqtt="<[broker:tele/sonoff-b1/LWT:state:default]" }
Switch FF_Sonoff_B1_Power      "Power"                              <light>         (FF_Sonoff_B1)  {mqtt=">[broker:cmnd/sonoff-b1/POWER:command:*:default], <[broker:stat/sonoff-b1/RESULT:state:JSONPATH($.POWER)]"}
Switch FF_Sonoff_B1_Fade       "Fade: [%s]"                         <fade_out>      (FF_Sonoff_B1)  {mqtt=">[broker:cmnd/sonoff-b1/FADE:command:*:default], <[broker:stat/sonoff-b1/RESULT:state:JSONPATH($.Fade)]"}
Dimmer FF_Sonoff_B1_Dimmer     "Dimmer"                             <brightness>    (FF_Sonoff_B1)  {mqtt=">[broker:cmnd/sonoff-b1/DIMMER:command:*:default], <[broker:stat/sonoff-b1/RESULT:state:JSONPATH($.Dimmer)]"}
String FF_Sonoff_B1_Mode       "Mode: [MAP(spotlightmode.map):%s]"  <colorlight>    (FF_Sonoff_B1)  {mqtt=">[broker:cmnd/sonoff-b1/SCHEME:command:*:default], <[broker:stat/sonoff-b1/RESULT:state:JSONPATH($.Scheme)]"}
Color  FF_Sonoff_B1_ColorHSB   "Colour [%s]"                        <colorlight>    (FF_Sonoff_B1)  {mqtt=">[broker:cmnd/sonoff-b1/HSBColor:command:*:default], <[broker:stat/sonoff-b1/RESULT:state:JSONPATH($.HSBColor)]"}
Number FF_Sonoff_B1_RSSI       "WiFi Signal Strength [%d %%]"       <wifi>          (FF_Sonoff_B1)  {mqtt="<[broker:tele/sonoff-b1/STATE:state:JSONPATH($.Wifi.RSSI)]" }

Group  FF_Sonoff_POW           "Sonoff POW"                        <sonoff_pow>    (Sonoff_POW)
String FF_Sonoff_POW_Reachable "Reachable: [%s]"                   <contactable>   (FF_Sonoff_POW) {mqtt="<[broker:tele/sonoff-pow/LWT:state:default]" }
Switch FF_Sonoff_POW_Power     "Power []"                          <sonoff_pow>    (FF_Sonoff_POW) {mqtt=">[broker:cmnd/sonoff-pow/POWER:command:*:default], <[broker:stat/sonoff-pow/POWER:state:default]" }
Number FF_Sonoff_POW_Power     "Power load [%.1f W]"               <voltage>       (FF_Sonoff_POW) {mqtt="<[broker:tele/sonoff-pow/SENSOR:state:JSONPATH($.ENERGY.Power)]" }
Number FF_Sonoff_POW_Voltage   "Line voltage [%.1f V]"             <voltage>       (FF_Sonoff_POW) {mqtt="<[broker:tele/sonoff-pow/SENSOR:state:JSONPATH($.ENERGY.Voltage)]" }
Number FF_Sonoff_POW_Current   "Line current [%.3f A]"             <current>       (FF_Sonoff_POW) {mqtt="<[broker:tele/sonoff-pow/SENSOR:state:JSONPATH($.ENERGY.Current)]" }
Number FF_Sonoff_POW_Today     "Total energy today [%.3f kWh]"     <total_energy>  (FF_Sonoff_POW) {mqtt="<[broker:tele/sonoff-pow/SENSOR:state:JSONPATH($.ENERGY.Today)]" }
Number FF_Sonoff_POW_Yesterday "Total energy yesterday [%.3f kWh]" <total_energy>  (FF_Sonoff_POW) {mqtt="<[broker:tele/sonoff-pow/SENSOR:state:JSONPATH($.ENERGY.Yesterday)]" }
Number FF_Sonoff_POW_RSSI      "WiFi Signal Strength [%d %%]"      <wifi>          (FF_Sonoff_POW) {mqtt="<[broker:tele/sonoff-pow/STATE:state:JSONPATH($.Wifi.RSSI)]" }

Group  GF_Sonoff_SC             "Sonoff SC"                         <sonoff_sc>     (Sonoff_SC)
String GF_Sonoff_SC_Reachable   "Reachable: [%s]"                   <contactable>   (GF_Sonoff_SC)  {mqtt="<[broker:tele/sonoff-sc/LWT:state:default]" }
Number GF_Sonoff_SC_Temperature "Temperature [%d °C]"               <temp>          (GF_Sonoff_SC)  {mqtt="<[broker:tele/sonoff-sc/SENSOR:state:JSONPATH($.SonoffSC.Temperature)]" }
Number GF_Sonoff_SC_Humidity    "Humidity [%d %%]"                  <humidity>      (GF_Sonoff_SC)  {mqtt="<[broker:tele/sonoff-sc/SENSOR:state:JSONPATH($.SonoffSC.Humidity)]" }
Number GF_Sonoff_SC_Light       "Light [%d %%]"                     <light>         (GF_Sonoff_SC)  {mqtt="<[broker:tele/sonoff-sc/SENSOR:state:JSONPATH($.SonoffSC.Light)]" }
Number GF_Sonoff_SC_Noise       "Noise [%d %%]"                     <microphone>    (GF_Sonoff_SC)  {mqtt="<[broker:tele/sonoff-sc/SENSOR:state:JSONPATH($.SonoffSC.Noise)]" }
Number GF_Sonoff_SC_AirQuality  "Air Quality [%d %%]"               <air_quality>   (GF_Sonoff_SC)  {mqtt="<[broker:tele/sonoff-sc/SENSOR:state:JSONPATH($.SonoffSC.AirQuality)]" }
Number GF_Sonoff_SC_RSSI        "WiFi Signal Strength [%d %%]"      <wifi>          (GF_Sonoff_SC)  {mqtt="<[broker:tele/sonoff-sc/STATE:state:JSONPATH($.Wifi.RSSI)]" }

Group  GF_Sonoff_RF                "RF Bridge"                     c<sonoff_rf>     (Sonoff_RF)
String GF_Sonoff_RF_Reachable      "Reachable: [%s]"                <contactable>   (GF_Sonoff_RF)  {mqtt="<[broker:tele/sonoff-rf/LWT:state:default]" }
String GF_Sonoff_RF_Received_Data  "Received Data: [%s]"            <none>          (GF_Sonoff_RF)  {mqtt="<[broker:tele/sonoff-rf/RESULT:state:JSONPATH($.RfReceived.Data)]"}
String GF_Sonoff_RF_Received_Sync  "Received Sync: [%s]"            <none>          (GF_Sonoff_RF)  {mqtt="<[broker:tele/sonoff-rf/RESULT:state:JSONPATH($.RfReceived.Sync)]"}
String GF_Sonoff_RF_Received_Low   "Received Low: [%s]"             <none>          (GF_Sonoff_RF)  {mqtt="<[broker:tele/sonoff-rf/RESULT:state:JSONPATH($.RfReceived.Low)]"}
String GF_Sonoff_RF_Received_High  "Received High: [%s]"            <none>          (GF_Sonoff_RF)  {mqtt="<[broker:tele/sonoff-rf/RESULT:state:JSONPATH($.RfReceived.High)]"}
String GF_Sonoff_RF_Received_RfKey "Received RfKey: [%s]"           <none>          (GF_Sonoff_RF)  {mqtt="<[broker:tele/sonoff-rf/RESULT:state:JSONPATH($.RfReceived.RfKey)]"}
Switch GF_Sonoff_RF_Button_1       "Button 1"                       <none>          (GF_Sonoff_RF)  {mqtt=">[broker:cmnd/sonoff-rf/RFKEY1:command:*:default], <[broker:stat/sonoff-rf/RESULT:state:JSONPATH($.RfKey1)]", autoupdate="false"}
...
Switch GF_Sonoff_RF_Button_16      "Button 16: [%s]"                <none>          (GF_Sonoff_RF)  {mqtt=">[broker:cmnd/sonoff-rf/RFKEY16:command:*:default], <[broker:stat/sonoff-rf/RESULT:state:JSONPATH($.RfKey16)]", autoupdate="false"}
Number GF_Sonoff_RF_RSSI           "WiFi Signal Strength [%d %%]"   <wifi>          (GF_Sonoff_RF)  {mqtt="<[broker:tele/sonoff-rf/STATE:state:JSONPATH($.Wifi.RSSI)]" }

You will use those old MQTT commands again when defining new Things.

Now create a file in your things folder, let’s call it mqtt.things. This is where you will define an MQTT broker Thing using the details from your old mqtt.cfg. You will also be defining all of the Things that will communicate with this broker and their channels using those existing MQTT commands. Here is an example mqtt.things:

Bridge mqtt:broker:mosquitto "Mosquitto" [ host="localhost", port=1883, secure=false, username="USERNAME", password="PASSWORD", clientID="openHAB2" ]
{
    // Sonoffs
    Thing topic sonoffs20 "Sonoff S20" @ "Some Room" {
    Channels:
        Type string : reachable "Reachable"            [ stateTopic="tele/sonoff-s20/LWT" ]
        Type switch : power     "Power"                [ stateTopic="stat/sonoff-s20/POWER", commandTopic="cmnd/sonoff-s20/POWER" ]
        Type number : rssi      "WiFi Signal Strength" [ stateTopic="tele/sonoff-s20/STATE", transformationPattern="JSONPATH:$.Wifi.RSSI"]
    }

    Thing topic sonoffb1 "Sonoff B1" @ "Some Room" {
    Channels:
        Type string   : reachable  "Reachable"            [ stateTopic="tele/sonoff-b1/LWT" ]
        Type switch   : power      "Power"                [ stateTopic="stat/sonoff-b1/POWER", commandTopic="cmnd/sonoff-b1/POWER" ]
        Type switch   : fade       "Fade"                 [ stateTopic="tele/sonoff-b1/STATE", commandTopic="cmnd/sonoff-b1/FADE", transformationPattern="JSONPATH:$.Fade" ]
        Type dimmer   : dimmer     "Dimmer"               [ stateTopic="tele/sonoff-b1/STATE", commandTopic="cmnd/sonoff-b1/DIMMER", transformationPattern="JSONPATH:$.Dimmer" ]
        Type string   : mode       "Mode"                 [ stateTopic="tele/sonoff-b1/STATE", commandTopic="cmnd/sonoff-b1/SCHEME", transformationPattern="JSONPATH:$.Scheme" ]
        Type colorHSB : colour     "Colour"               [ stateTopic="tele/sonoff-b1/STATE", commandTopic="cmnd/sonoff-b1/HSBColor", transformationPattern="JSONPATH:$.HSBColor" ]
        Type number   : rssi       "WiFi Signal Strength" [ stateTopic="tele/sonoff-b1/STATE", transformationPattern="JSONPATH:$.Wifi.RSSI"]
    }

    Thing topic sonoffpow "Sonoff POW" @ "Some Other Room" {
    Channels:
        Type string : reachable "Reachable"              [ stateTopic="tele/sonoff-pow/LWT" ]
        Type switch : power     "Power"                  [ stateTopic="stat/sonoff-pow/POWER", commandTopic="cmnd/sonoff-pow/POWER" ]
        Type number : powerload "Power load"             [ stateTopic="tele/sonoff-pow/SENSOR", transformationPattern="JSONPATH:$.ENERGY.Power"]
        Type number : voltage   "Line voltage"           [ stateTopic="tele/sonoff-pow/SENSOR", transformationPattern="JSONPATH:$.ENERGY.Voltage"]
        Type number : current   "Line current"           [ stateTopic="tele/sonoff-pow/SENSOR", transformationPattern="JSONPATH:$.ENERGY.Current"]
        Type number : total     "Total energy today"     [ stateTopic="tele/sonoff-pow/SENSOR", transformationPattern="JSONPATH:$.ENERGY.Today"]
        Type number : totalyest "Total energy yesterday" [ stateTopic="tele/sonoff-pow/SENSOR", transformationPattern="JSONPATH:$.ENERGY.Yesterday"]
        Type number : rssi      "WiFi Signal Strength"   [ stateTopic="tele/sonoff-pow/STATE", transformationPattern="JSONPATH:$.Wifi.RSSI"]
    }

    Thing topic sonoffsc "Sonoff SC" @ "Another Room" {
    Channels:
        Type string : reachable   "Reachable"            [ stateTopic="tele/sonoff-sc/LWT" ]
        Type number : temperature "Temperature"          [ stateTopic="tele/sonoff-sc/SENSOR", transformationPattern="JSONPATH:$.SonoffSC.Temperature"]
        Type number : humidity    "Humidity"             [ stateTopic="tele/sonoff-sc/SENSOR", transformationPattern="JSONPATH:$.SonoffSC.Humidity"]
        Type number : light       "Light"                [ stateTopic="tele/sonoff-sc/SENSOR", transformationPattern="JSONPATH:$.SonoffSC.Light"]
        Type number : noise       "Noise"                [ stateTopic="tele/sonoff-sc/SENSOR", transformationPattern="JSONPATH:$.SonoffSC.Noise"]
        Type number : airquality  "Air Quality"          [ stateTopic="tele/sonoff-sc/SENSOR", transformationPattern="JSONPATH:$.SonoffSC.AirQuality"]
        Type number : rssi        "WiFi Signal Strength" [ stateTopic="tele/sonoff-sc/STATE", transformationPattern="JSONPATH:$.Wifi.RSSI"]
    }

    Thing topic sonoffrf "RF Bridge" @ "Yet Another Room" {
        Type string : reachable     "Reachable"            [ stateTopic="tele/sonoff-rf/LWT" ]
        Type string : recieveddata  "Received Data"        [ stateTopic="tele/sonoff-rf/RESULT", transformationPattern="JSONPATH:$.RfReceived.Data"]
        Type string : recievedsync  "Received Sync"        [ stateTopic="tele/sonoff-rf/RESULT", transformationPattern="JSONPATH:$.RfReceived.Sync"]
        Type string : recievedlow   "Received Low"         [ stateTopic="tele/sonoff-rf/RESULT", transformationPattern="JSONPATH:$.RfReceived.Low"]
        Type string : recievedhigh  "Received High"        [ stateTopic="tele/sonoff-rf/RESULT", transformationPattern="JSONPATH:$.RfReceived.High"]
        Type string : recievedrfkey "Received RfKey"       [ stateTopic="tele/sonoff-rf/RESULT", transformationPattern="JSONPATH:$.RfReceived.RfKey"]
        Type switch : button1       "Button 1"             [ stateTopic="stat/sonoff-rf/RESULT", commandTopic="cmnd/sonoff-rf/RFKEY1", transformationPattern="JSONPATH:$.RfKey1" ]
        ...
        Type switch : button16      "Button 16"            [ stateTopic="stat/sonoff-rf/RESULT", commandTopic="cmnd/sonoff-rf/RFKEY16", transformationPattern="JSONPATH:$.RfKey16" ]
        Type number : rssi          "WiFi Signal Strength" [ stateTopic="tele/sonoff-rf/STATE", transformationPattern="JSONPATH:$.Wifi.RSSI"]
    }
}

Next thing to do is revisit your sonoff.items file and update the old MQTT commands so that they use the newly created channels instead. Using the previous sonoff.items as an example, the updated content should resemble this:

Group  GF_Sonoff_S20           "Sonoff S20"                                   <sonoff_s20_uk> (Sonoff_S20)
String GF_Sonoff_S20_Reachable "Reachable: [%s]"                   <contactable>   (GF_Sonoff_S20) { channel="mqtt:topic:mosquitto:sonoffs20:reachable" }
Switch GF_Sonoff_S20_Power     "Power"                                   <sonoff_s20_uk> (GF_Sonoff_S20) { channel="mqtt:topic:mosquitto:sonoffs20:power" }       
Number GF_Sonoff_S20_RSSI      "WiFi Signal Strength [%d %%]"      <wifi>          (GF_Sonoff_S20) { channel="mqtt:topic:mosquitto:sonoffs20:rssi" }

Group  FF_Sonoff_B1            "Sonoff B1"                                    <sonoff_b1>     (Sonoff_B1)
String FF_Sonoff_B1_Reachable  "Reachable: [%s]"                    <contactable>   (FF_Sonoff_B1)  { channel="mqtt:topic:mosquitto:sonoffb1:reachable" }
Switch FF_Sonoff_B1_Power      "Power"                              <light>         (FF_Sonoff_B1)  { channel="mqtt:topic:mosquitto:sonoffb1:power" }
Switch FF_Sonoff_B1_Fade       "Fade: [%s]"                         <fade_out>      (FF_Sonoff_B1)  { channel="mqtt:topic:mosquitto:sonoffb1:fade" }
Dimmer FF_Sonoff_B1_Dimmer     "Dimmer"                             <brightness>    (FF_Sonoff_B1)  { channel="mqtt:topic:mosquitto:sonoffb1:dimmer" }
String FF_Sonoff_B1_Mode       "Mode: [MAP(spotlightmode.map):%s]"  <colorlight>    (FF_Sonoff_B1)  { channel="mqtt:topic:mosquitto:sonoffb1:mode" }
Color  FF_Sonoff_B1_ColorHSB   "Colour [%s]"                        <colorlight>    (FF_Sonoff_B1)  { channel="mqtt:topic:mosquitto:sonoffb1:colour" }
Number FF_Sonoff_B1_RSSI       "WiFi Signal Strength [%d %%]"       <wifi>          (FF_Sonoff_B1)  { channel="mqtt:topic:mosquitto:sonoffb1:rssi" }

Group  FF_Sonoff_POW           "Sonoff POW"                                   <sonoff_pow>    (Sonoff_POW)
String FF_Sonoff_POW_Reachable "Reachable: [%s]"                   <contactable>   (FF_Sonoff_POW) { channel="mqtt:topic:mosquitto:sonoffpow:reachable" }
Switch FF_Sonoff_POW_Power     "Power []"                          <sonoff_pow>    (FF_Sonoff_POW) { channel="mqtt:topic:mosquitto:sonoffpow:power" }
Number FF_Sonoff_POW_Load      "Power load [%.1f W]"               <voltage>       (FF_Sonoff_POW) { channel="mqtt:topic:mosquitto:sonoffpow:powerload" }
Number FF_Sonoff_POW_Voltage   "Line voltage [%.1f V]"             <voltage>       (FF_Sonoff_POW) { channel="mqtt:topic:mosquitto:sonoffpow:voltage" }
Number FF_Sonoff_POW_Current   "Line current [%.3f A]"             <current>       (FF_Sonoff_POW) { channel="mqtt:topic:mosquitto:sonoffpow:current" }
Number FF_Sonoff_POW_Today     "Total energy today [%.3f kWh]"     <total_energy>  (FF_Sonoff_POW) { channel="mqtt:topic:mosquitto:sonoffpow:total" }
Number FF_Sonoff_POW_Yesterday "Total energy yesterday [%.3f kWh]" <total_energy>  (FF_Sonoff_POW) { channel="mqtt:topic:mosquitto:sonoffpow:totalyest" }
Number FF_Sonoff_POW_RSSI      "WiFi Signal Strength [%d %%]"      <wifi>          (FF_Sonoff_POW) { channel="mqtt:topic:mosquitto:sonoffpow:rssi" }

Group  GF_Sonoff_SC             "Sonoff SC"                                   <sonoff_sc>     (Sonoff_SC)
String GF_Sonoff_SC_Reachable   "Reachable: [%s]"                   <contactable>   (GF_Sonoff_SC) { channel="mqtt:topic:mosquitto:sonoffsc:reachable" }
Number GF_Sonoff_SC_Temperature "Temperature [%d °C]"               <temp>          (GF_Sonoff_SC) { channel="mqtt:topic:mosquitto:sonoffsc:temperature" }
Number GF_Sonoff_SC_Humidity    "Humidity [%d %%]"                  <humidity>      (GF_Sonoff_SC) { channel="mqtt:topic:mosquitto:sonoffsc:humidity" }
Number GF_Sonoff_SC_Light       "Light [%d %%]"                     <light>         (GF_Sonoff_SC) { channel="mqtt:topic:mosquitto:sonoffsc:light" }
Number GF_Sonoff_SC_Noise       "Noise [%d %%]"                     <microphone>    (GF_Sonoff_SC) { channel="mqtt:topic:mosquitto:sonoffsc:noise" }
Number GF_Sonoff_SC_AirQuality  "Air Quality [%d %%]"               <air_quality>   (GF_Sonoff_SC) { channel="mqtt:topic:mosquitto:sonoffsc:airquality" }
Number GF_Sonoff_SC_RSSI        "WiFi Signal Strength [%d %%]"      <wifi>          (GF_Sonoff_SC) { channel="mqtt:topic:mosquitto:sonoffsc:rssi" }

Group  GF_Sonoff_RF                "RF Bridge"                                <sonoff_rf>     (Sonoff_RF)
String GF_Sonoff_RF_Reachable      "Reachable: [%s]"                <contactable>   (GF_Sonoff_RF) { channel="mqtt:topic:mosquitto:sonoffrf:reachable" }
String GF_Sonoff_RF_Received_Data  "Received Data: [%s]"            <none>          (GF_Sonoff_RF) { channel="mqtt:topic:mosquitto:sonoffrf:recieveddata" }
String GF_Sonoff_RF_Received_Sync  "Received Sync: [%s]"            <none>          (GF_Sonoff_RF) { channel="mqtt:topic:mosquitto:sonoffrf:recievedsync" }
String GF_Sonoff_RF_Received_Low   "Received Low: [%s]"             <none>          (GF_Sonoff_RF) { channel="mqtt:topic:mosquitto:sonoffrf:recievedlow" }
String GF_Sonoff_RF_Received_High  "Received High: [%s]"            <none>          (GF_Sonoff_RF) { channel="mqtt:topic:mosquitto:sonoffrf:recievedhigh" }
String GF_Sonoff_RF_Received_RfKey "Received RfKey: [%s]"           <none>          (GF_Sonoff_RF) { channel="mqtt:topic:mosquitto:sonoffrf:recievedrfkey" }
Switch GF_Sonoff_RF_Button_1       "Button 1"                       <none>          (GF_Sonoff_RF) { channel="mqtt:topic:mosquitto:sonoffrf:button1", autoupdate="false"}
...
Switch GF_Sonoff_RF_Button_16      "Button 16: [%s]"                <none>          (GF_Sonoff_RF) { channel="mqtt:topic:mosquitto:sonoffrf:button16", autoupdate="false"}
Number GF_Sonoff_RF_RSSI           "WiFi Signal Strength [%d %%]"   <wifi>          (GF_Sonoff_RF) { channel="mqtt:topic:mosquitto:sonoffrf:rssi" }

Lastly, go through your rules files and replace any of these:

publish("broker", "topic", "payload")

With these:

val mqttActions = getActions("mqtt", "mqtt:broker:mosquitto")
mqttActions.publishMQTT("topic", "payload") 

Done. :sunglasses:

Don’t forget to keep an eye on the OpenHAB logs to see if any of your old transformations no longer work.

Edit: Here is an example .sitemap using the items defined above:

sitemap default label="Some Place" {
    Frame label="Ground Floor" icon="groundfloor" {

        Group item=GF_Sonoff_S20 label="Some Adapter" 

        Group item=GF_Sonoff_SC label="Some Sensors" {
            Text item=GF_Sonoff_SC_Reachable label="Status [%s]"
            Text item=GF_Sonoff_SC_Temperature label="Temperature [%d °C]"
            Text item=GF_Sonoff_SC_Humidity label="Humidity [%d %%]"
            Text item=GF_Sonoff_SC_Light label="Light [%d %%]"
            Text item=GF_Sonoff_SC_Noise label="Noise [%d %%]"
            Text item=GF_Sonoff_SC_AirQuality label="Air Quality [%d %%]"
            Text item=GF_Sonoff_SC_RSSI label="WiFi Signal Strength [%d %%]"
        }

        Group item=GF_Sonoff_RF {
            Text item=GF_Sonoff_RF_Reachable label="Status [%s]"
            Text item=GF_Sonoff_RF_Received_Data label="Data [%s]"
            Text item=GF_Sonoff_RF_Received_Sync label="Sync [%s]"
            Text item=GF_Sonoff_RF_Received_Low label="Low [%s]"
            Text item=GF_Sonoff_RF_Received_High label="High [%s]"
            Text item=GF_Sonoff_RF_Received_RfKey label="RfKey [%s]"
            Switch item=GF_Sonoff_RF_Button_1 label="RfKey1" mappings=[ON="Key 1"]
            ...
            Switch item=GF_Sonoff_RF_Button_16 label="RfKey16" mappings=[ON="Key 16"]
            Text item=GF_Sonoff_RF_RSSI label="WiFi Signal Strength [%d %%]"
        }
    }
    
    Frame label="First Floor" icon="firstfloor" {
        Group item=FF_Sonoff_B1 label="Some Light" {
            Text item=FF_Sonoff_B1_Reachable label="Status [%s]"
            Switch item=FF_Sonoff_B1_Power label="Power"
            Switch item=FF_Sonoff_B1_Fade label="Fade"
            Slider item=FF_Sonoff_B1_Dimmer label="Brightness [%d]"
            Selection item=FF_Sonoff_B1_Mode label="Color Mode" mappings=["0"="Single Color", "1"="Wake up", "2"="Slow Cycle", "3"="Fast Cycle", "4"="Disco"]
            Colorpicker item=FF_Sonoff_B1_ColorHSB label="Colour"
            Text item=FF_Sonoff_B1_RSSI label="WiFi Signal Strength [%d %%]"
        }

        Group item=FF_Sonoff_POW {
            Text item=FF_Sonoff_POW_Reachable label="Status [%s]"
            Switch item=FF_Sonoff_POW_Power label="Power []"
            Text item=FF_Sonoff_POW_Load label="Power load [%.1f W]"
            Text item=FF_Sonoff_POW_Voltage label="Line voltage [%.1f V]"
            Text item=FF_Sonoff_POW_Current label="Line current [%.3f A]"
            Text item=FF_Sonoff_POW_Today label="Total energy today [%.3f kWh]"
            Text item=FF_Sonoff_POW_Yesterday label="Total energy yesterday [%.3f kWh]"
            Text item=FF_Sonoff_POW_RSSI label="WiFi Signal Strength [%d %%]"
        }
    }
}
49 Likes

Thanks for posting. I just reverted back to the 1.x binding after attempting to get the 2.4 version working for several hours over the past couple of days. I’ve saved this for a later read. Cheers.

I’d be interested in a report / post / rant about your experiences with the mqtt 2.4 binding and where and why the migration failed for you.

Cheers, David

hey guys,

i just upgraded my openhab to latest version with apt… after that my mqtt setup do not work anymore ?

i’m liitle bit confused about the mqtt binding ut wasn’t installed anymore and i had to install the mqtt binding. now i have mqtt.cfg and mqtt-eventbus.cfg.

do you have any advise how to configure correctly ?

Thanks in advance

Lars

Hey David,

I just updated by accident to OpenHAB2 2.4 and my MQTT did not work anymore. SO I decided to switch to the new binding.
Thanks you that you posted this. It helped me a lot to get it running again!!

I have only one remark. First my Items didn’t work than I added the broker name in the items configuration and they are online again and I can switch my tasmota switches!!

For example:

>  Switch GF_Sonoff_S20_Power  "Sonoff S20"  <sonoff_s20_uk> (GF_Sonoff_S20) { channel="mqtt:topic:mosquitto:sonoffs20:power" }

Is this wrong in your example?

Another question. For what do I need “mqttActions” lines. I added them in the startup code. But what are they doing?

Thanks a lot
Oliver

No problem. Hope it helps.

In the example, all of the Things are defined inside curly braces “{ }” that are directly after the square brackets “” that define Bridge parameters. My understanding is that those curly braces are telling OpenHAB that any Things defined within them are associated with that Bridge. That is why I have not added the broker name to the channels.

The example reflects settings that are working in my OpenHAB installation. Are your Things inside the curly braces that define your Bridge?

They are required if you have rules that will send MQTT actions. You do not need those lines in the startup code, unless the action of starting OpenHAB is supposed to send an MQTT action.

That’s part of why this thread exists! :wink:

The MQTT Binding was significantly updated from version 1.13.0 to version 2.4. The new version does not require those files. It does require that you do some re-configuring though.

:neutral_face:

Read the first post in this thread? Also, if you don’t want to use the .items and .things approach, you can always use the GUI.

Actually your post helped me a lot! So again: Thank you!

I understand your argument that the bridge must be defined by placing the thing name inside the bridge config, but in my case it did not work!? Anyway after after adding the bridge name to the channel it worked fine.

Sorry for the dummy question. But do you know where can I read something about the MQTT actions and what I can do with them? A short google search did not help much.

:slight_smile:

This is only one way to do this. You can also define the bridge and thing in different lines:

bridge.things

Bridge mqtt:broker:broker “MQTT Broker: Mosquitto” [ host=“192.168.2.1”, port=1883, secure=false]

other_things.things

Thing mqtt:topic:miTemp (mqtt:broker:broker) {
Channels:
    Type number : tempObyvak “Air Temperature” [ stateTopic=“sensor/teplota/obyvak”]
}

(as also posted here: MQTT Generic Thing: java.lang.Exception: No MQTT client - #5 by Ales_Macoun)

1 Like

I was typing up a convoluted response to explain this. Yours is much more concise. Response trimmed, cheers!

You can read about rules here. MQTT actions are used within custom rules to send commands. For instance:

rule "<RULE_NAME>"
when
    <TRIGGER_CONDITION>
then
    val mqttActions = getActions("mqtt", "mqtt:broker:mosquitto")

    if (Some_Item.state != 0) {
        mqttActions.publishMQTT("cmnd/sonoff-pow/POWER", "ON")
    }
end

:slightly_smiling_face:

1 Like

thank you very much for responding, i started with the gui…

i have an ESP8266 (smart plug with energy meter) with espurna firmware an MQTT support

  1. configured a broker connection
  2. configured generic mqtt thing

as far as good, i do not get my switch item updated, may be you may help once again, in my item file i had:

{mqtt="

[openhab:avatar-plug2/relay/0/set:command:*:default],
<[openhab:avatar-plug2/relay/0:state:MAP(onoff.map)]
"}

the command direction is working, get the state does not work… i think is related to the mal i will dive into …

thanks in advance

Lars

I am also experiencing the same issue. I have a device that accepts upper or lower case on/off as command, but returns only lowercase as state: on or off.
With the old binding, I guess I used the same transformation as you did (MAP() ) to translate upper- to lowercase and vice versa.
The new binding seems to ignore my attempts with “transformationPattern” and also with on=“on” as the switch state is never updated to on. When using the OH app or web interface, i can toggle the device on and off, if I do not update the page/app in between (the switch changes to the on position, but the icon does not show the on state; refreshing resets the switch to off). So I know, that the commands are sent correctly.
@David_Graeff do you know about this behaviour?

Edit: as the item state does not change, rules based on this are also no longer executed.

The sonoff firmware uses two distinct mqtt topics to report the state. Are you sure you are subscribing to the correct one? (Use any mqtt client to verify)

i could not get it working with ma(). I used the extend menu (show more) in channel definition, there i could pass my values for on and off. now they state gets updated…

Thanks again. Unfortunately I needed to switch bach to openHAB 2.3. 2.4 was totally unstable in my environment, Not because MQTT. In got in general, exceptions over exceptions and on this morning nothing works anymore… :-(.
Back to 2.3 I have a totally clean log… I hope there will be some fixes soon.

Regarding your “totally unstable in my environment”, I had just that until I removed entries in * /var/lib/openhab2/config/org/openhab/addons.config which were not needed anymore ( in my case binding mqtt1 and action mqtt) .
Consider doing that!

openHAB 2.4 in a fresh environment works without any errors in the log. You might want to backup your user data, do a clean install and move your custom files and paperui json files back.

Thanks for the hints! I will consider doing a fresh installation when I have more time and patience. I just did this a few weeks ago when I switched to Debain Stretch…
Currently I am happy with 2.3. No problems at all! It was only a lot of stupid clicking to delete over 80 devices in Alexa and install them again. First for 2.4 and than for 2.3 again… :wink:

@Jürgen: My proplems are not coming from MQTT. There is a problem with my Homematic rollershuters in the sitemap. It crashes in kind of a loop and more and more homematic erros occur.

Hey Sorry Guys, me again. Happy new year!
I got the hint to increase the JAVA heap size and updated again. Now its much better. No strange exception anymore. MQTT workes also fine. But there is one error/warning in the log

2019-01-01 09:40:56.999 [INFO ] [el.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model 'mqtt.things', using it anyway:
Provide a thing type ID and a thing ID in this format:
 <thingTypeId> <thingId>
Provide a thing type ID and a thing ID in this format:
 <thingTypeId> <thingId>
Provide a thing type ID and a thing ID in this format:
 <thingTypeId> <thingId>
Provide a thing type ID and a thing ID in this format:
 <thingTypeId> <thingId>
2019-01-01 09:40:57.010 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'mqtt.things'

My thinks 4 look like this:

Bridge mqtt:broker:mosquitto "Mosquitto" [ host="127.0.0.1", secure=false, clientID="openHAB2"]
{
 // My OBI switches with Tasmota
    Thing mqtt:topic:alexaWohnzimmer "AlexaSwitchWohnzimmer" @ "Wohnzimmer" {
        Channels:
            Type string : reachable "Reachable"            [ stateTopic="Home/Alexa/LivingRoom/tele/LWT" ]
            Type switch : power     "Power"                [ stateTopic="Home/Alexa/LivingRoom/stat/POWER", commandTopic="Home/Alexa/LivingRoom/cmnd/POWER" ]
            Type number : rssi      "WiFi Signal Strength" [ stateTopic="Home/Alexa/LivingRoom/tele/STATE", transformationPattern="JSONPATH:$.Wifi.RSSI"]
    }
    Thing mqtt:topic:alexaEsszimmer "AlexaSwitchEsszimmer" @ "Esszimmer" {
        Channels:
            Type string : reachable "Reachable"            [ stateTopic="Home/Alexa/DiningRoom/tele/LWT" ]
            Type switch : power     "Power"                [ stateTopic="Home/Alexa/DiningRoom/stat/POWER", commandTopic="Home/Alexa/DiningRoom/cmnd/POWER" ]
            Type number : rssi      "WiFi Signal Strength" [ stateTopic="Home/Alexa/DiningRoom/tele/STATE", transformationPattern="JSONPATH:$.Wifi.RSSI"]
    }
    Thing mqtt:topic:alexaSchlafzimmer "AlexaSwitchSchlafzimmer" @ "Schlafimmer" {
        Channels:
            Type string : reachable "Reachable"            [ stateTopic="Home/Alexa/BedRoom/tele/LWT" ]
            Type switch : power     "Power"                [ stateTopic="Home/Alexa/BedRoom/stat/POWER", commandTopic="Home/Alexa/BedRoom/cmnd/POWER" ]
            Type number : rssi      "WiFi Signal Strength" [ stateTopic="Home/Alexa/BedRoom/tele/STATE", transformationPattern="JSONPATH:$.Wifi.RSSI"]
    }
    Thing mqtt:topic:alexaKueche "AlexaSwitchKueche" @ "Kueche" {
        Channels:
            Type string : reachable "Reachable"            [ stateTopic="Home/Alexa/Kitchen/tele/LWT" ]
            Type switch : power     "Power"                [ stateTopic="Home/Alexa/Kitchen/stat/POWER", commandTopic="Home/Alexa/Kitchen/cmnd/POWER" ]
            Type number : rssi      "WiFi Signal Strength" [ stateTopic="Home/Alexa/Kitchen/tele/STATE", transformationPattern="JSONPATH:$.Wifi.RSSI"]
    }
}

Any idea idea whats not OK in the definition?

This is probably copied from the documentation right? Unfortunately that is an incorrect syntax. But I do not know the correct one. There were other people with working examples, look for recent posts.