MQTT Pub -Sub failure. It's me

Hello community. Having problems getting Openhab2, Mosquitto, and my Sonoff basic talking. I’m pretty sure it comes down to my ignorance about how to pub - sub topics. I have a working Mosquitto tested through “Hello World” and a working connection to Openhab tested with Dim’s instructions:

Open a ssh login in one window and subscribe to all topics under openhab:

mosquitto_sub -u openhab -P habopen -t openhab/#
Open another ssh login in another window and publish a message (e.g. ON) to a topic (e.g. command) under openhab:

mosquitto_pub -u openhab -P habopen -t openhab/command -m ON

I even got MQTTlens working after a little learning curve.

However (I hate to admit it), I can’t for the life of me figure out how to publish or subscribe topics. I know some of the following configurations already have topics but, how the heck do I publish or subscribe to them? Is there another file to create? Is it all command line. Everything I’ve found in the last weeks refers to what -v or -p means or what commands to use. Then it says publish or subscribe but nothing about how. I’m sure once I get it I’ll feel even more foolish. But for now I am at a loss. If anyone can give a step by step (possibly using info provided below) I would greatly appreciate it.

mqtt.cfg

mosquitto.url=tcp://localhost:1883
mosquitto.clientId=openhabian
//mosquitto.user=openhabian
mosquitto.qos=1
mosquitto.retain=true
mosquitto.async=false

cord.items

Switch cord1 “Cord” {mqtt="<[mosquitto:diana/lights/cord1:state:default], >[mosquitto:diana/lights/cord1:command:ON:1], >[mosquitto:diana/lights/cord1:command:OFF:0]"}

lights.sitemap

sitemap lights label=“diana_lane” {
Frame label=“Date” {
Text item=Date
}
Frame label=“Lights” {
Switch item=cord1 label=“Cord”

}

}

On my Sonoff Basic Module

MQTT parameters

Host (domus1)
openhabianpi

Port (1883)
1883

Client Id (DVES_9702D5)
cord1
User (DVES_USER)
openhabian

Password
•••••••••

Topic = %topic% (sonoff)
lights

Full Topic (%prefix%/%topic%/)
diana/lights/cord/power

( I’m thinking I need to change the full topic of the Sonoff to:
diana/lights/cord1/power

Based on what you posted (NOTE, please use code fences to format your posted code, 5th to 9th icons above the posting window) if you flip cort1 on your sitemap it should publish 1 to diana/lights/cord1 for ON and 0 for OFF.

And message that gets posted to diana/lights/cord1 will be used to update cord1.

@rlkoshak Thanks but when you say flip do you just mean the switch? When I do that. The lamp connected to cord1 doesn’t come on. I know the manual switch on the lamp is on too. I can toggle it through the Sonoff’s web page.

How do I do code fences?
Been trying to figure it out.

Code Fence Example 1
Code Fence Example 2
Code Fence Example 3

image

Lets try it.

This is a test

Thanks @rgerrans

1 Like

Try this in your items file:

Switch cord1 “Cord” {mqtt="<[mosquitto:stat/lights/POWER:state:default], >[mosquitto:cmnd/lights/power:command:*:default]" }

This works with my Sonoff basic. If you have a MQTT client installed, then you can see the topic the Sonoff basic is publishing.

Click on one of the code fence icons above the posting composition entry field or put three backticks before and after

```
Code goes here
```

@ Likonaus: Didn’t work. Its asking for rules.

Do I need a transformation file?

maybe my mqtt binding didn’t take. I installed it through the paper UI but had to do a refresh for it to show as installed otherwise it just showed “installing” indefinitely. I also installed the weather, Wemo, Hue, Kodi, aand other bindings. My addons.cfg looks like this:

 # The installation package of this openHAB instance
# Note: This is only regarded at the VERY FIRST START of openHAB
# Note: If you want to specify your add-ons yourself through entries below, set the package to "minimal"
# as otherwise your definition might be in conflict with what the installation package defines.
#
# Optional. If not set, the dashboard (https://<yourserver>:8080/) will ask you to choose a package.
#
# Valid options:
#   - minimal  : Installation only with dashboard, but no UIs or other add-ons. Use this for custom setups.
#   - simple   : Setup for using openHAB purely through UIs - you need to expect MANY constraints in functionality!
#   - standard : Default setup for normal users, best for textual setup
#   - expert   : Setup for expert users, especially for people migrating from openHAB 1.x
#   - demo     : A demo setup which includes UIs, a few bindings, config files etc.
#
# See http://docs.openhab.org/configuration/packages.html for a detailed explanation of these packages.
#
#package = minimal

# Access Remote Add-on Repository
# Defines whether the remote openHAB add-on repository should be used for browsing and installing add-ons.
# This not only makes latest snapshots of add-ons available, it is also required for the installation of
# any legacy 1.x add-on. (default is true)
#
#remote = true

# Include legacy 1.x bindings. If set to true, it also allows the installation of 1.x bindings for which there is 
# already a 2.x version available (requires remote repo access, see above). (default is false)
#
#legacy = true

# A comma-separated list of bindings to install (e.g. "sonos,knx,zwave")
#binding = 

# A comma-separated list of UIs to install (e.g. "basic,paper")
#ui = 

# A comma-separated list of persistence services to install (e.g. "rrd4j,jpa")
#persistence = 

# A comma-separated list of actions to install (e.g. "mail,pushover")
#action = 

# A comma-separated list of transformation services to install (e.g. "map,jsonpath")
#transformation = 

# A comma-separated list of voice services to install (e.g. "marytts,freetts")
#voice = 

# A comma-separated list of miscellaneous services to install (e.g. "myopenhab")
#misc =

Did you setup your mqtt.cfg file in the services directory?

mqtt.cfg

#
# Define your MQTT broker connections here for use in the MQTT Binding or MQTT
# Persistence bundles. Replace <broker> with an ID you choose.
#

# URL to the MQTT broker, e.g. tcp://localhost:1883 or ssl://localhost:8883
#mosquitto.url=tcp://localhost:1883

# Optional. Client id (max 23 chars) to use when connecting to the broker.
# If not provided a random default is generated.
#<broker>.clientId=<clientId>

# Optional. True or false. If set to true, allows the use of clientId values
# up to 65535 characters long. Defaults to false.
# NOTE: clientId values longer than 23 characters may not be supported by all
# MQTT servers. Check the server documentation.
#<broker>.allowLongerClientIds=false

# Optional. User id to authenticate with the broker.
#<broker>.user=<user>

# Optional. Password to authenticate with the broker.
#<broker>.pwd=<password>

# Optional. Set the quality of service level for sending messages to this broker.
# Possible values are 0 (Deliver at most once),1 (Deliver at least once) or 2
# (Deliver exactly once). Defaults to 0.
#<broker>.qos=<qos>

# Optional. True or false. Defines if the broker should retain the messages sent to
# it. Defaults to false.
#<broker>.retain=<retain>

# Optional. True or false. Defines if messages are published asynchronously or
# synchronously. Defaults to true.
#<broker>.async=<async>

# Optional. Defines the last will and testament that is sent when this client goes offline
# Format: topic:message:qos:retained <br/>
#<broker>.lwt=<last will definition>


mosquitto.url=tcp://localhost:1883
mosquitto.clientId=openhabian
mosquitto.qos=1
mosquitto.retain=true
mosquitto.async=false

That’s your issue. You need to setup your cfg file - http://docs.openhab.org/addons/bindings/mqtt1/readme.html#transport-configuration

At the bottom it says this:

mosquitto.url=tcp://localhost:1883
mosquitto.clientId=openhabian
mosquitto.qos=1
mosquitto.retain=true
mosquitto.async=false

My bad, I didn’t scroll down.

Have you setup debug logs for mqtt to verify communication?

Maybe. I can check the logs but not sure if they’re debug.

Should I change my .items from

Switch cord1 “Cord” {mqtt="<[mosquitto:diana/lights/cord1:state:default], >[mosquitto:diana/lights/cord1:command:ON:1], >[mosquitto:diana/lights/cord1:command:OFF:0]"}

to this?

Switch cord1 “Cord” {mqtt="<[mosquitto:diana/lights/cord1:state:default], >[mosquitto:diana/lights/cord1:cmd:ON:1], >[mosquitto:diana/lights/cord1:cmd:OFF:0]"}

It’s just changing the word ‘command’ to ‘cmd’ in the parameters.

So I need to do this?

log:set DEBUG mosquitto.binding.mqtt

log:set DEBUG org.openhab.binding.mqtt

In Karaf console