[SOLVED] Change the status of the mqtt switch

Hello. I’m studying openhab and mqtt. Set up a broker mosquitto, created test items:

Switch mqttsw1 "Switch 1" (all) {mqtt=">[mosquitto:/testsw/1:command:on:default],>[mosquitto:/testsw/1:command:off:default]"}

And displayed on a sitemap:

sitemap default label="My first sitemap"
{
	Frame label="MQTT" {
		Switch item=mqttsw1 label="MQTT Switch 1"	
	} 
}

If you change the position of the switch, the messages testsw / 1: ON are published. But when I publishes testsw / 1: OFF with another client mqtt, the basic UI switch does not change its position. How do I make the switch position change when I get the publication?

This is because you have not configured an inbound configuration (a subscription to a topic) in OH2 to be able to read the status of the device/switch.

modify your item line as follows:

Switch mqttsw1 "Switch 1" (all) {mqtt=">[mosquitto:/testsw/1:command:*:default],<[mosquitto:/testsw/1:state:state:default]"}

or:

Switch mqttsw1 "Switch 1" (all) {mqtt=">[mosquitto:/testsw/1:command:on:default],>[mosquitto:/testsw/1:command:off:default],<[mosquitto:/testsw/1:state:state:default]"}

This assumes that your device supports status reporting

Hmm did I do something wrong with my Sonoff ?
Because I can turn on/off but same as @Emil it does not update the status.

Switch MQTT_Grill "MQTT Grill Lampe"  (ALL,Office_Outdoor, Office_First_Floor, MySQLPersist) { mqtt=">[mosquitto:cmnd/sonoff-977686/POWER:command:*:default], <[mosquitto:stat/sonoff-977686/POWER:state:default]" }

Your item config looks good and it is according to the info that I can find (I don’t use the sonoff myself):


Did status updates work before? maybe you changed something recently in your system?

“Long shot” note: If you recently upgraded your Mosquitto MQTT Broker Server to version 1.4.12, you may have a problem with the clientID naming.
But on the other hand… sonoff-977686 does not include ‘#’, ‘+’, or ‘/’ and you can send on/off commands… so this must not be the root-cause…
See: http://mosquitto.org/2017/05/security-advisory-cve-2017-7650/

2 Likes

Unfortunately, it did not help, in mqtt.cfg it is necessary to specify the configuration of data saving?

Which device are you using? (is it a Sonoff module? which one?) Or you are just testing mqtt without a physical device?

No need to change something in mqtt.cfg

Edit: I made a mistake in the item config… try this:

Switch mqttsw1 "Switch 1" (all) {mqtt=">[mosquitto:/testsw/1:command:on:default],>[mosquitto:/testsw/1:command:off:default],<[mosquitto:/testsw/1:state:default]"}

So does not work either. At the moment I do not use the module. Now I’m studying openhab, switch to BasicUI and track on android MQTT Dashboard, then publish from mobile i monitor the status change in BasicUI

So you’re saying that you cannot read the status of a device that does not exist?

And the switch should not change its position when receiving the power-on message without the device?

Update:
I just checked my Sonoff MQTTs and now it looks like everything is working fine :slight_smile:

1 Like

This should work (it works for me at least):

Switch mqttsw1 "Switch 1" (all) {mqtt=">[mosquitto:testsw/1:command:*:default],<[mosquitto:testsw/1:state:default]"}

Publish a message from another client and see your switch react in your OH2 sitemap:

mosquitto_pub -t testsw/1 -m OFF
1 Like

Yes, it works. Thanks for the help.

Explain to me please to understand what it means *, it turns out it accepts all the messages of publication testsw \ 1?

It’s the trigger for the outbound message.
See: http://docs.openhab.org/addons/bindings/mqtt1/readme.html#item-configuration-for-outbound-messages

Instead of using 2 entries for ON & OFF like in your first post, you use 1 entry with *

I apologize. I do not understand how status works. The device is connected, this is es8266 + DHT22. If you switch the switch to OpenHAB on other devices, the state will change, but if you switch to a smartphone, the status does not change in openhab and does not update the temperature information, it is updated only after the page reloads. Tell me, what will I miss?

Switch mqttsw1 "Switch 1" (all) {mqtt=">[mosquitto:/ESP/LED/001:command:on:1],>[mosquitto:/ESP/LED/001:command:off:0],<[mosquitto:/ESP/LED/001:state:default]"}

Number temp "temp [%.1f]" {mqtt="<[mosquitto:/ESP/DHT/TEMP:state:default]"}

	Text   item=temp label="Temp [%s C]" icon="temperature"
        Switch item=mqttsw1 label="MQTT Switch 1"
1 Like

If I use the :command:*:default the switch does not change. What is wrong?
Thanks.

THIS WORKS:

Switch qLDbed "LD Bed" (gESP8266) {mqtt="
                >[broker:/pucon/haus/licht/control:command:ON:1],
				>[broker:/pucon/haus/licht/control:command:OFF:0],                
                <[broker:/pucon/haus/licht/state:state:default]" }

THIS DOES NOT WORK:

Switch qLDbed "LD Bed" (gESP8266) {mqtt="
                >[broker:/pucon/haus/licht/control:command:*:default],            
                <[broker:/pucon/haus/licht/state:state:default]" }

obviously, your ESP code does not accept ON/OFF commands. Only 1/0.
The first version of the item config transforms the ON from openHAB to 1 (this will be mqtt payload that will be published).
You can check the payload in both scenarios using a tool (like MQTT.fx)

You can add mapping to openHAB to do the transformation from ON to 1 and OFF to 0 for you.

In your transform folder add a file called ON1OFF0.map with the following content:

OFF=0
ON=1

Now you item is:

Switch qLDbed "LD Bed" (gESP8266) {mqtt=">[broker:/pucon/haus/licht/control:command:*:MAP(ON1OFF0.map)], <[broker:/pucon/haus/licht/state:state:default]" }
1 Like

Dim, you are right!
Thank you and Mr. MQTT.fx a lot.

1 Like

Hello Vincent, this works however with :

OFF=0
ON=1

thanks.

Yes sorry my bad. If your ESP published 0 or 1 as a state then you can use OFF=0 and ON=1 in the inbound mqtt