[SOLVED] Help migrating MQTT 1.x binding to the new MQTT 2.x

Ehm… the issue is two different command topics for a switches on and off position. An outbound transformation would not have helped here in this case.

Yes this is not the same, but I have switches which receives on/off and also only changes state if on/off published to the topic (not ON/OFF). And plus it publishes/subcribes to the same topic.

Can I achieve this with this binding? Now I simply have a MAP file which transforms on to ON of to OFF and vice versa and the command and state also mapped by this transformation file. Is this possible?

Switch channels allow you to define custom on/offs. Look up the documentation. No need for a transformation.

In your above post you said:

State: /lightbulb/12204702807d3a49b2ae/dd28170cbf5dcd08/192.168.75.118/state
and Command: /lightbulb/12204702807d3a49b2ae/dd28170cbf5dcd08/192.168.75.118/command/off and -on.

That’s not the same topic for me?

Thanks! I might have read the whole docs again, because I have seen that it has been extended since I last saw it.

Yes these are different topics but I only have one topic like:

cmnd/sonoff/power

and now I use it like this:

 mqtt=">[mosquitto:cmnd/sonoff/power:command:*:default],<[mosquitto:stat/sonoff/POWER:state:default]"

So now it seems that most of my transforms can be done by channel configuration. So in general there is no map transform available?

Ok, you were not referring to the given example and also never told me your circumstances :smiley:

Yes state and command topic are the same on one channel. I was the whole time explaining for the other example. That was a little bit of a time robbery you did here :wink:

Please always read the doc first and don’t kidnap other peoples thread. … Edit: Oh this is your thread. Then don’t let other people kidnap your thread. Nobody scrolls to the first port before responding, you usually respond to the recent posts.

@David_Graeff

Would it be possible to add an example on how to convert a simple mqtt item to the new binding? I am struggling to understand how to do just that.

Here’s my current item:

Switch FF_Office_LightSwitch_MotionSensor "Office Motion Sensor"                
  {mqtt="<[broker:zigbee2mqtt/0x00158d000236b6c0:command:JS(occupancy.js)]" }

which is something pretty typical IMO. It would be helpful to have a step-to-step instruction on how to create the .things file (which you did have), how to create the associated channel, and how to apply the transform.

I know we should read the doc, and I did and will continue to until I can restore my system to the working state. But the fact is there are so much things to know with OH and the various bindings. We got something working, and then move on to other things. It’s quite daunting to try to understand everything. This is why I think a simple step-by-step migration would be very helpful.

Thanks for listening. I will now head back to try to restore my mqtt.

I’ve been using software for years and it has been my experience that most the time the documentation really only makes sense after you know how the software works. We read the docs!!! Heck, I almost gave up on openHAB because of a bug that didn’t reload my bindings on restart. But I didn’t know enough about the software to recognize it as a bug.

Also @David_Graeff thank you thank you thank you… and I don’t even use MQTT (yet) Your work here has been tireless and for all the times I’ve read your replies, you are patient and courteous even when folks have gotten awful demanding of a volunteer’s time.

I by no chance meant to criticize David’s work. I really appreciate all the free time David has put into this. My comment was just a suggestion from the user’s perspective. I am an average guy, and if I struggle, I think at least a few others would as well. If the document covers the normal usage well, we will save having to answer repeated questions.

Anyway, for my case, I got close. The only thing that doesn’t work right now is the transformation.
So from, this item

Switch FF_Office_LightSwitch_MotionSensor "Office Motion Sensor"                
  {mqtt="<[broker:zigbee2mqtt/0x00158d000236b6c0:command:JS(occupancy.js)]" }

I defined this mqttConnections.things

Bridge mqtt:broker:myUnsecureBroker [ host="localhost",secure=false ]                                                  
{                                                                                                                          
    Thing mqtt:topic:mything {                                                                                               
    Channels:                                                                                                                  
        Type switch : FoyerMotionSensor "Foyer Motion Sensor" stateTopic="zigbee2mqtt/0x00158d000204c7ef", commandTopic="zigbee2mqtt/0x00158d000204c7ef" ]
    }
} 

and the revised item looks like this:

Switch FF_Foyer_LightSwitch_ClosetMotionSensor "Foyer Closet Motion Sensor"     
  (FF_Foyer_LightSwitch_MotionSensor)                                           
  {channel="mqtt:topic:mything:FoyerMotionSensor"[profile="transform:JS", function="occupancy.js"]}

What doesn’t work for me currently is the transform. The value isn’t extracted correctly.

NO no, @yfaway so sorry did not mean to imply you were being critical, I appologize.

Which I agreed with, again so sorry

This is true, and again not being critical but sometimes it is hard to write documentation for software you wrote, you know how it works, you know the lingo. It is hard to imagine a person just starting out’s point of view or knowledge level. So anyhow, to help user help developers create better documentation I wrote a quick thread on how to contribute to openHAB’s documentation. Here is a link:

I got it working now:

Original item:

Switch FF_Office_LightSwitch_MotionSensor "Office Motion Sensor"                
  {mqtt="<[broker:zigbee2mqtt/0x00158d000236b6c0:command:JS(occupancy.js)]" }

With the new binding, the mqtt.things file has to look like this:

Bridge mqtt:broker:myUnsecureBroker [ host="localhost",secure=false ]                                                  {                                                                                                                          
    Thing mqtt:topic:mything {                                                                                             
    Channels:                                                                                                                  
        Type switch : FoyerMotionSensor "Foyer Motion Sensor" [stateTopic="zigbee2mqtt/0x00158d000204c7ef", commandTopic="zigbee2mqtt/0x00158d000204c7ef", transformationPattern="JSONPATH:$.occupancy", on="true", off="false"]                  
    }                                                                                                                  
}                                                                                                                      

And the item looks like this:

Switch FF_Foyer_LightSwitch_ClosetMotionSensor "Foyer Closet Motion Sensor"     
  (FF_Foyer_LightSwitch_MotionSensor)                                           
  {channel="mqtt:topic:mything:FoyerMotionSensor"}

The channel doesn’t support JS so I have to use a combination of JSONPATH and on/off values.

Js should also work, but jsonpath is the better decision here.

I have tried your configs but the Thing keeps offline with error “java.lang.Exception: No MQTT client” while the broker is connected normally.

I have 2.4 M6 build

I restarted and now some works and some are “UNINITIALIZED - HANDLER_CONFIGURATION_PENDING”

I can’t comment on your specific issue as I haven’t seen it. But I recommend to upgrade to the final stable 2.4 release. M6 is old.

I know my info is not detailed, i just was commenting from my phone… hoping it is a known issue or something :slight_smile:
I will upgrade tonight and check if it is working fine.

Meanwhile, one comment, the only failing generic thing is consisting of more than 30 channels… is there any limitations i should be aware of?

This has been fixed. It only happens if you configure via text files. You need 2.4 stable, potentially 2.5 snapshot for the fix though.

Perfect… i was indeed using text files… thanks for the feedback. I will upgrade today and re-test…
It looks great :slight_smile:

Thanks! The problem was that this is a too generic topic name I think :slight_smile:

However I have added my first MQTT item, it is working, but the state won’t change. So it turns the lamp ON/OFF but the state stays off. I have added the same topic to the state as the command topic.

My question is about the textual representation of the new MQTT things.
Where do i have to configure the Bridge?
I have this configuration, the bridge is configured 3 times. I tried to omit one of it, but I got errors.

And I get the message " Provide a thing type ID and a thing ID in this format:
<thingTypeId> <thingId>" in the log.

I refered to https://www.openhab.org/blog/2018-12-16-mqtt-arrives-in-the-modern-openhab-2-x-architecture.html

mqtt_broker.things:

mqtt:broker:mosquitto [ host="localhost",secure=false ]

esp.things:

Bridge mqtt:broker:mosquitto [ host="localhost", secure=false ] {
    Thing mqtt:topic:esp_waesche {
    Channels:
      Type number : temperature "temperature"   [ stateTopic="/Waesche/temperature" ]    
      Type number : humidity    "humidity"      [ stateTopic="/Waesche/humidity" ]        
      Type number : RSSI        "RSSI"          [ stateTopic="/Waesche/RSSI" ]  
      Type switch : relais_a    "relais_a"      [ stateTopic="/Waesche/Relais_A",   commandTopic="/Waesche/gpio/16", on="0", off="1" ]
      Type switch : relais_b    "relais_b"      [ stateTopic="/Waesche/Relais_B",   commandTopic="/Waesche/gpio/5",  on="0", off="1" ]      

    }
    
    
    Thing mqtt:topic:esp_badezimmer {
    Channels:
      Type number : temperature "temperature"   [ stateTopic="/Badezimmer/temperature" ]    
      Type number : humidity    "humidity"      [ stateTopic="/Badezimmer/humidity" ]     
      Type number : pressure    "pressure"      [ stateTopic="/Badezimmer/pressure" ]    
      Type number : Helligkeit  "Helligkeit"    [ stateTopic="/Badezimmer/Helligkeit" ]   
      Type number : RSSI        "RSSI"          [ stateTopic="/Badezimmer/RSSI" ]  
      Type switch : relais_a    "relais_a"      [ stateTopic="/Badezimmer/Relais_A",   commandTopic="/Badezimmer/gpio/5",  on="0", off="1" ]
      Type switch : relais_b    "relais_b"      [ stateTopic="/Badezimmer/Relais_B",   commandTopic="/Badezimmer/gpio/4",  on="0", off="1" ]    
      Type switch : relais_c    "relais_c"      [ stateTopic="/Badezimmer/Relais_C",   commandTopic="/Badezimmer/gpio/0",  on="0", off="1" ]
      Type switch : relais_d    "relais_d"      [ stateTopic="/Badezimmer/Relais_D",   commandTopic="/Badezimmer/gpio/2",  on="0", off="1" ]   

    }    
    
    Thing mqtt:topic:esp_werkstatt {
    Channels:
      Type number : temperature "temperature"   [ stateTopic="/Werkstatt/temperature" ]    
      Type number : humidity    "humidity"      [ stateTopic="/Werkstatt/humidity" ]        
      Type number : RSSI        "RSSI"          [ stateTopic="/Werkstatt/RSSI" ]  
      Type switch : relais_a    "relais_a"      [ stateTopic="/Werkstatt/Relais_A",   commandTopic="/Werkstatt/gpio/16", on="0", off="1" ]
      Type switch : relais_b    "relais_b"      [ stateTopic="/Werkstatt/Relais_B",   commandTopic="/Werkstatt/gpio/5",  on="0", off="1" ]      

    }        
    
}

fhem.things:

Bridge mqtt:broker:mosquitto [ host="localhost", secure=false ] {
    Thing mqtt:topic:Speedtest {
    Channels:
      Type number : download      "download"        [ stateTopic="/fhem/Speedtest/download" ]  
      Type number : upload        "upload"          [ stateTopic="/fhem/Speedtest/upload" ]  
      Type number : ping          "ping"            [ stateTopic="/fhem/Speedtest/ping" ]        
      Type number : score         "score"           [ stateTopic="/fhem/Speedtest/score" ]  
      Type switch : statusRequest "statusRequest"   [ commandTopic="/fhem/Speedtest/set",  on="statusRequest" ]      

    }
    
    
      
    
}
1 Like

So, i upgraded to 2.4 (upgrade not a fresh installation).

I have windows 10 with latest update and running a local mosquitto server.

After upgrade, if i change anything in the .things files, it just loses communication and same error appear, and i have to restart OH many times until all Things come to live.

Sample of the config is as below:

Bridge mqtt:broker:bonnnuc "Bonn Mqtt Broker" [ host="192.168.0.20", secure=false ]
{                                                                                                                          
    Thing mqtt:topic:sonoff_sc_topics "Sonoff SC MQTT" {                                                                                             
        Channels:                                                                      
            Type number : sonoff_sc_temp    "Sonoff SC Temp"     [stateTopic="tele/sonoff/SENSOR", transformationPattern="JSONPATH:$.SonoffSC.Temperature"]
            Type number : sonoff_sc_humid   "Sonoff SC Humid"    [stateTopic="tele/sonoff/SENSOR", transformationPattern="JSONPATH:$.SonoffSC.Humidity"]
            Type number : sonoff_sc_noise   "Sonoff SC Noise"    [stateTopic="tele/sonoff/SENSOR", transformationPattern="JSONPATH:$.SonoffSC.Noise"]
            Type number : sonoff_sc_air     "Sonoff SC Air"      [stateTopic="tele/sonoff/SENSOR", transformationPattern="JSONPATH:$.SonoffSC.AirQuality"]
            Type number : sonoff_sc_light   "Sonoff SC Light"    [stateTopic="tele/sonoff/SENSOR", transformationPattern="JSONPATH:$.SonoffSC.Light"]
    }
    Thing mqtt:topic:xiaomi_btn1 "Xiaomi Btn1 MQTT"  {
        Channels:
            Type string : state         "Xiaomi Btn1 State"     [stateTopic="zigbee2mqtt/mbedroom_xiaomi_button1", transformationPattern="JS:getZigbeeClick.js"]
            Type number : battery       "Xiaomi Btn1 Battery"   [stateTopic="zigbee2mqtt/mbedroom_xiaomi_button1", transformationPattern="JSONPATH:$.battery"]
    }
    Thing mqtt:topic:xiaomi_btn2 "Xiaomi Btn2 MQTT"  {
        Channels:
            Type string : state         "Xiaomi Btn2 State"     [stateTopic="zigbee2mqtt/bedroom_xiaomi_button1", transformationPattern="JS:getZigbeeClick.js"]
            Type number : battery       "Xiaomi Btn2 Battery"   [stateTopic="zigbee2mqtt/bedroom_xiaomi_button1", transformationPattern="JSONPATH:$.battery"]
    }
}

If i change anything (add a space or whatever) then the file is re-loaded and the error below appears:

i then have to restart OH to make it work (sometimes more than once).

any ideas?

Thanks in advance for your help.