MQTT Things Issues

I have spent a lot of time over the past three months learning OpenHAB with a lot of help from some very helpful and knowledgeable people on this forum. Now after persevering for a couple of days I need help again.
As they say, “if it ain’t broke don’t fix it” and guess what I did.
I had a fully working system controlling my Garage Door, reading 3 temperature sensors on my water heater, 2 Sonoff S26 plugs and the Davis Weatherstation binding when I decided to replace the embeded MQTT broker with the Mosquitto broker and stuffed things up. I decided my system needed a clean up anyway so I backed up the configuration files and made an image of my SD card before doing a clean install with Mosquitto. I then restored all my configuration files and installed the MQTT, JSON and Map bindings.
My the S26 plugs and the temperature sensors worked immediately so I must have configured MQTT correctly, however, my Garage Door appears in ClassicUI but doesn’t work.
On my original system the Garage Door items and things were created in PaperUI and now I’m trying to script them like everything else and this is where my problem is. My SonoffSV_1 is showing as online and has the two channels but the switch and status in ClassicUI don’t work.
My Things file

 Thing mqtt:topic:SonoffSV_1 "SonoffSV_1" @ "myHome/Garage" {
		Channels:
			Type switch : power1 "Garage Door Open/Close" [ 
				stateTopic="stat/SonoffSV_1/POWER1",
                commandTopic="cmnd/SonoffSV_1/POWER1",
				on="OPEN",
				off="CLOSED"
				]
            Type string : power2 "Garage Door State" [ 
				stateTopic="stat/SonoffSV_1/POWER2",
				ON="OPEN",
				OFF="CLOSED",
				-="UNKNOWN"			
              ]   
   }

My items file.

Switch GaragedoorActivate "Garage Door Open/Close" {channel="mqtt:topic:SonoffSV_1:power1"}
String GaragedoorStatus "Garage Door State [MAP(openclosed.map):%s]" {channel="mqtt:topic:SonoffSV_1:power2"}

Where am I going wrong?

When you save this things file what do you see in the logs?

What does the -="UNKOWN" do? That’s new to me!

And put your on and off in the second channel to lowercase.

And and - just to check: this thing is sitting inside a bridge to your Mosquitto broker?

I don’t use .things files and don’t recommend their use. So I can only be of so much help. But some things I notice:

  • You are using a switch type channel instead of a contact type channel, why not just use a contact type channel and no have to convert from OPEN/CLOSED to on/off (that’s what the on="OPEN" does)
  • You can’t define a .map file inside the Thing definition. For switch and contact type channels, you can identify certain messages to mean ON/OPEN and certain message to mean OFF/CLOSED. You can’t randomly change the case of the thing parameter (i.e. it’s on="OPEN" not ON="OPEN"). And you can’t make up parameters (there is no -= parameter).

I would suggest changing the title of the main post because the problem really doesn’t have anything to do with your first start of openHAB. The problem is entirely MQTT .things file syntax as evidenced by the fact that your other MQTT Things are working. Changing the title may get the attention of others who might be able to help more.

How did you define the broker thing and how did you set the shown mqtt thing to connect to this broker thing?

This is a bit long winded but I tried to cover everything.
I’ve changed the title of this thread to MQTT Things Issues to correctly identify what it is about.
I have also corrected the case to on=“OPEN” and off=“CLOSED” in each instance.
My ClassicUI was showing - for status so I added the -=“UNKNOWN” and now it shows UNKNOWN in ClassicUI.
The first channel is the relay on the SonoffSV that I have set in Tasmota as a momentary switch to activate the garage door and the second is a reed switch which is closed (on) when the door is open. I’ve tried changing the first channel from switch to button but it made no difference. The SonoffSV is just not receiving any MQTT command from openHAB.
The Tasmota console shows the following when the second button on the Tasmota main screen is pressed.

05:26:48 MQT: stat/sonoffsv_1/RESULT = {"POWER2":"ON"}
05:26:48 MQT: stat/sonoffsv_1/POWER2 = ON (retained)

This is why I had uppercase ON and OFF.
I’ve tried to check the MQTT communication between openHAB and my sonoffsv by subscribing to sonoffsv and publishing the following in MQTT.fx
cmnd/sonoffsv_1/POWER1=ON
but the Tasmota console shows
06:21:55 MQT: stat/sonoffsv_1/RESULT = {“Command”:“Unknown”}
Obviously it is being received but what is the correct syntax of a command to publish to test it. I couldn’t find an example by googling that I could understand.
What I showed before was just the one thing from my things file. This is my full things file.

Bridge mqtt:broker:MosquittoMqttBroker [host="openhab", secure=false] {
Thing mqtt:topic:nodemcu1 "nodemcu1" @ "myHome/Water_Heater"
	{
		Channels:
			Type number : temperature "Temperature" [
				stateTopic="tele/nodemcu1/SENSOR",
				transformationPattern="JSONPATH:$.DS18B20_1.Temperature"
				]
			Type number : temperature2 "Temperature2" [
				stateTopic="tele/nodemcu1/SENSOR",
				transformationPattern="JSONPATH:$.DS18B20_2.Temperature"
				]
			Type number : temperature3 "Temperature3" [
				stateTopic="tele/nodemcu1/SENSOR",
				transformationPattern="JSONPATH:$.DS18B20_3.Temperature"
				]
	}
Thing mqtt:topic:sonoffs26_1 "SonoffS26_1" @ "myHome/Living_Room"
	{
		Channels:
			Type switch : power "S26_1 On/Off" [ 
				stateTopic="stat/sonoffs26_1/POWER",
                commandTopic="cmnd/sonoffs26_1/POWER",
				on="ON",
				off="OFF"
				]
	}
Thing mqtt:topic:sonoffs26_2 "SonoffS26_2" @ "myHome/Living_Room" 
	{
		Channels:
			Type switch : power "S26_2 On/Off" [ 
				stateTopic="stat/sonoffs26_2/POWER",
                commandTopic="cmnd/sonoffs26_2/POWER",
				on="ON",
				off="OFF"
				]
	}
 Thing mqtt:topic:sonoffsv_1 "SonoffSV_1" @ "myHome/Garage" {
		Channels:
			Type switch : power1 "Garage Door Open/Close" [ 
				stateTopic="stat/sonoffsv_1/POWER1",
                commandTopic="cmnd/sonoffsv_1/POWER1",
				on="OPEN",
				off="CLOSED"
				]
            Type string : power2 "Garage Door State" [ 
				stateTopic="stat/sonoffsv_1/POWER2",
				on="OPEN",
				off="CLOSED",
				-="UNKNOWN"			
                ]   
   }
}

The nodemcu1 and SonoffS26s are working fine and I based the SonoffSV thing on them.
After making the changes recommended it is still not working.

OH already knows how to interpret ON and OFF so there is no need to translate it or define alternative messages that mean ON and OFF. The on and off you see in the channel config in the .things file is the name of a predefined parameter.

Try changing this to:

Thing topic sonoffsv_1 "SonoffSV_1" @ "myHome/Garage" {

Then your .items file, instead of:

Switch GaragedoorActivate "Garage Door Open/Close" {channel="mqtt:topic:SonoffSV_1:power1"}

Needs to be like:

Switch GaragedoorActivate "Garage Door Open/Close" {channel="mqtt:topic:MosquittoMqttBroker:SonoffSV_1:power1"}
1 Like

I’ve tried your suggestion but it still doesn’t work. I’m suspecting something wrong in mqtt communication.
I get the following in the openHAB log when I turn the switch on in ClassicUI but nothing when I turn it off and there is nothing in the Tasmota console or MQTT.fx log.

2020-06-25 09:40:15.696 [ome.event.ItemCommandEvent] - Item 'GaragedoorActivate' received command ON
2020-06-25 09:40:15.700 [nt.ItemStatePredictedEvent] - GaragedoorActivate predicted to become NULL
2020-06-25 09:40:24.479 [ome.event.ItemCommandEvent] - Item 'GaragedoorActivate' received command ON
2020-06-25 09:40:24.482 [nt.ItemStatePredictedEvent] - GaragedoorActivate predicted to become NULL

What is real strange is that the two SonoffS26 smart plugs shown in my full things file above work as they are and my SonoffSV thing is modeled on them.

Okay, incoming topic and payload for openHAB.

And your Thing channel that listens to that topic.
You’ve chosen a string type channel.
The on= and off= parameters do not apply to string channels, take those away.
The -= parameter is something you made up, take that away.
The docs should be your bible

You would need to amend your String Item channel link in a similar way to the other channel.

Have you amended the on=/off= from the switch channel yet? Obviously it’s no good sending OPEN/CLOSED as commands. (commandTopic uses those for sending, as well as stateTopic using them for inbound.)
I think SV1 uses lowercase commands, you can test that from mqtt.fx.
Rreckon you want on=on and off=off.

Some bindings are not so good at picking up small edits to Things files in flight, sometimes you’ve got to restart the binding package.

Finally it’s sorted.
I removed the on=“ON” and off=“OFF” in all things.
I then changed POWER1 to power1 and my switch started working.
I then changed POWER2 to power2 and removed the -=“UNKNOWN” but the status was showing CLOSED no matter what state the reed switch was in.
It works fine if I used the Toggle 2 button on the Tasmota interface so it must be something physical on the reed switch connection.
Thanks everyone for your help and input.
Now to re-establish my openHAB cloud and my Davis Weather Station binding.
For anyone looking for help and find this thread in a search here is my final thing for a Tasmotized SonoffSV setup as a Garage Door Controller.

	Thing mqtt:topic:sonoffsv_1 "SonoffSV_1" @ "myHome/Garage" 
 	{
		Channels:
			Type switch : power1 "Garage Door Open/Close" [ 
				stateTopic="stat/sonoffsv_1/POWER1",
                commandTopic="cmnd/sonoffsv_1/POWER1"
				]
            Type string : power2 "Garage Door State" [ 
				stateTopic="stat/sonoffsv_1/POWER2",
				on="OPEN",
				off="CLOSED"
                ]   
   }

Good catch, rossko57

@lesterb,

Even if my suggestion was not the source of your problem, personally I find it cleaner to read and therefore understand what is going on.

Also, unless I am mistaken, I do think it is needed to put the broker name in the channel in the .items file?

1 Like

That’s not the case for me, at least. I define my broker bridge in one things file, then reference that broker when defining each thing in another things file.

The item them just references the thing channel, but doesn’t explicitly mention the broker.

It does appear there are different ways to do it. I remember reading examples both ways when getting mine set up initially. I suppose I am still trying to get my head around all of them.

Is it necessary to split the broker Thing into a separate file from child Things in order to then link your Items to the child Things directly?

Would you mind posting brief sample config? I think that would help me a lot to understand. And or, maybe I experiment both ways with my own setup to determine exactly how it works.

I suppose I was just so happy when I got it working initially, I was afraid to touch it or even breathe on it any more after that. :laughing:

But maybe I go back now and play around some more and try and figure it out.

IMHO the two ways to define the bridge thing connections are described Here..

It is possible to have them in seperate files, but both ways are working.

@opus linked the page: here’s the specific heading: https://www.openhab.org/docs/configuration/things.html#defining-bridges-using-files

I’ll edit this post with my MQTT examples in a bit.

@TRS-80:

Bridge and things in one file

lights.things

Bridge mqtt:broker:MosquittoMqttBroker "Mosquitto MQTT Broker" [host="192.168.1.92", secure=false]
{
	// Switch Porch Light
	Thing mqtt:topic:swPorchLight "Switch Porch Light" {
		Channels:
			Type switch : switch "Power Switch" [ 
				stateTopic="stat/swPorchLight/POWER", 
				commandTopic="cmnd/swPorchLight/POWER",
				on="ON",
				off="OFF"          
			]
			Type string : reachable "Reachable" [
				stateTopic = "tele/swPorchLight/LWT"
			]
			Type number : temperature "Temperature" [ 
				stateTopic="tele/swPorchLight/SENSOR",
				transformationPattern="JSONPATH:$.AM2301.Temperature"
			]
			Type number : humidity "Humidity" [ 
				stateTopic="tele/swPorchLight/SENSOR",
				transformationPattern="JSONPATH:$.AM2301.Humidity"
			]
	}
}

Bridge and things in separate files

bridge.things

Bridge mqtt:broker:MosquittoMqttBroker "Mosquitto MQTT Broker" [
	host="192.168.1.92",
	secure=false
]

lights.things

Note the reference to the MQTT bridge in brackets after Thing mqtt:topic:swPorchLight "Switch Porch Light"

	// Switch Porch Light
	Thing mqtt:topic:swPorchLight "Switch Porch Light" (mqtt:broker:MosquittoMqttBroker) {
		Channels:
			Type switch : switch "Power Switch" [ 
				stateTopic="stat/swPorchLight/POWER", 
				commandTopic="cmnd/swPorchLight/POWER",
				on="ON",
				off="OFF"          
			]
			Type string : reachable "Reachable" [
				stateTopic = "tele/swPorchLight/LWT"
			]
			Type number : temperature "Temperature" [ 
				stateTopic="tele/swPorchLight/SENSOR",
				transformationPattern="JSONPATH:$.AM2301.Temperature"
			]
			Type number : humidity "Humidity" [ 
				stateTopic="tele/swPorchLight/SENSOR",
				transformationPattern="JSONPATH:$.AM2301.Humidity"
			]
	}

Yes, I have seen both ways. At the time I did my setup, I found them by searching a lot in the forum and elsewhere on the Internet. But it appears the official docs have improved a lot in the meantime!

Of course will only be improved even further if/when @hafniumzinc gets around to this!

Thanks for the comments guys, glad I asked now!