[SOLVED] OH 2.4 MQTT-Binding: Generic thing stateTopic working, commandTopic not working

Hello community,

I am on the move from MQTT 1.x and Mosquitto to the new 2.4 Binding. I am using MQTT together with the Tuya/MQTT-Binding of @AgentK. So I thougt to start small and make only one tuya bulb working.

I followed the instructions given here:

The “old” MQTT-1.x-item which worked was:

Switch tuya_OG_Schlafzimmer1   <light> (group_OG_Schlafzimmer_Licht)  {mqtt="<[broker:tuya/lightbulb/12204702807d3a49b2ae/dd28170cbf5dcd08/192.168.75.118/state:state:default:.*], >[broker:tuya/lightbulb/12204702807d3a49b2ae/dd28170cbf5dcd08/192.168.75.118/command/on:command:ON:true], >[broker:tuya/lightbulb/12204702807d3a49b2ae/dd28170cbf5dcd08/192.168.75.118/command/off:command:OFF:false]"}

So I defined my bridge and generic thing accordingly:

mqtt.things

Bridge mqtt:broker:myUnsecureBroker [ host="127.0.0.1", secure=false ]
{
    Thing mqtt:topic:mything {
    Channels:
        Type switch : Tuya_Bulb_1_mqtt "LED1 MQTT" [ stateTopic="tuya/lightbulb/12204702807d3a49b2ae/dd28170cbf5dcd08/192.168.75.118/state", commandTopic="tuya/lightbulb/12204702807d3a49b2ae/dd28170cbf5dcd08/192.168.75.118/command" ]
    }
}

The thing in Paper UI:

I copied the channel from PaperUI:

And set up an item and linked to the channel:

Switch tuya_OG_Schlafzimmer1_mqtt <light>             {channel="mqtt:topic:mything:Tuya_Bulb_1_mqtt"}

And integrated in sitemap:

Switch item=tuya_OG_Schlafzimmer1_mqtt mappings=[ON="On", OFF="Off"]

When I use “Smart Life” (the app for controlling the Tuya bulb) to switch the bulb, OH correctly reflects the status of the bulb. So in principle things work.

2019-01-10 21:21:48.651 [vent.ItemStateChangedEvent] - tuya_OG_Schlafzimmer1_mqtt changed from ON to OFF
2019-01-10 21:22:10.846 [vent.ItemStateChangedEvent] - tuya_OG_Schlafzimmer1_mqtt changed from OFF to ON

But switching in OH does not affect the bulb, it stays in the same state as I set it in the Smart Life app. Even log says:

2019-01-10 21:23:27.711 [ome.event.ItemCommandEvent] - Item 'tuya_OG_Schlafzimmer1_mqtt' received command OFF
2019-01-10 21:23:27.717 [nt.ItemStatePredictedEvent] - tuya_OG_Schlafzimmer1_mqtt predicted to become OFF
2019-01-10 21:23:27.720 [vent.ItemStateChangedEvent] - tuya_OG_Schlafzimmer1_mqtt changed from ON to OFF

What point am I missing? I defined stateTopic and actionTopic in the generic MQTT-thing, but only stateTopic is working.

And bonus question: How can I get rid of the mapping in the sitemap, I want a “regular” switch?

Ok, the binding will send the value ON and OFF payloads to the light bulb
I think looking at your stateTopic that it needs 0 or 1 instead
And that is why your lightbulb doesn’t react
There is no straightforward answer to your problem as the binding does not support outgoing transformations (yet)

So the way around it (At the moment) is to create another Number channel with a commandTopic
Link a proxy Number item
Create a rule (When switch item received command, if command is ON sendCommand 1 on proxy number item and if command is OFF send command 0 on proxy number item). Does that make sense?

Remove it:

Switch item=tuya_OG_Schlafzimmer1_mqtt

As I have no Tuya lights, I can only suggest you try to send the MQTT action to the Tuya topic with a MQTT client and see if that works.

I did notice however that you specified the commandTopic as tuya/lightbulb/12204702807d3a49b2ae/dd28170cbf5dcd08/192.168.75.118/command while the old binding had something like tuya/lightbulb/12204702807d3a49b2ae/dd28170cbf5dcd08/192.168.75.118/command/on as the topic.

EDIT: @vzorglub just beat me to it. :grin:

@noppes123 you were right, I changed my thing like this (added “/on” to the command topic):

Bridge mqtt:broker:myUnsecureBroker [ host="127.0.0.1", secure=false ]
{
    Thing mqtt:topic:mything {
    Channels:
        Type switch : Tuya_Bulb_1_mqtt "LED1 MQTT" [ stateTopic="tuya/lightbulb/12204702807d3a49b2ae/dd28170cbf5dcd08/192.168.75.118/state", commandTopic="tuya/lightbulb/12204702807d3a49b2ae/dd28170cbf5dcd08/192.168.75.118/command/on" ]
    }
}

Now I can at least switch in “On”-Direction with OH.

But how do I continue now to have a “complete” switch? I guess I need three channels altogether: One for state, one for command on, one for command off. And then work with rules? Or is there a better way?

I think @vzorglub’s suggestion to add a proxy Item should work.

EDIT: If I understand you correctly, the Tuya has two topics, one for on and one for off. Does the payload matter at all?

Correct

The MQTT device topic structure is flawed basicaly. One should be able to send the payload on ONE topic.
You will need 2 items one for each channel and a proxy switch and a rule.

The Tuya-MQTT bridge code should be modified to accept the command on/off ON/OFF 0/1 on ONE topic only

EDIT according to the docs of the bridge:
tuya/<tuyaAPI-type>/<tuyaAPI-id>/<tuyaAPI-key>/<tuyaAPI-ip>/command
Should the ONE topic to send commands to

It doesn’t but it does support some simple modifications to outgoing messages. For ON/OFF type messages you can define a map to 0/1 in the “Show More” section of the Thing config in PaperUI

I haven’t used it so can’t say for sure it works though, but it’s better than nothing.

I don’t know how to define this mapping in .things files though.

Rich,

This for state only. It doesn’t apply to the outgoing value unfortunately

I created a patch for mqtt-tuya which AgentK has merged into his tree so you can set the command topic to just /command as in your first example and it will act on the message payload. Works like a charm in my setup, but it was just merged in the last 24 hours so you’ll need to pull the latest tuya-mqtt from Github to make sure you have this code. Let me know if you have trouble but I’ve tested it on two different setups now with no issues.

1 Like

@tsightler

Thank you so much for this patch, I indeed only saw the updated instructions, but not the updated script. But even with updated script, it still only reacts to stateTopic, commandTopic does not work. Here are my steps:

I removed the old script-directory completely and cloned it again. I added config.json

{
    "host": "localhost",
    "port": 1883,
    "topic": "tuya/",
    "mqtt_user": "",
    "mqtt_pass": ""
}

The mqtt.things I modified to:

Bridge mqtt:broker:myUnsecureBroker [ host="127.0.0.1", secure=false ]
{
    Thing mqtt:topic:mything {
    Channels:
        Type switch : Tuya_Bulb_1_mqtt "LED1 MQTT" [ stateTopic="tuya/lightbulb/12204702807d3a49b2ae/dd28170cbf5dcd08/192.168.75.118/state", commandTopic="tuya/lightbulb/12204702807d3a49b2ae/dd28170cbf5dcd08/192.168.75.118/command" ]
    }
}

In PaperUI:

Then I restarted the Pi.

But the commandTopic still does not switch the light.

2019-01-11 11:39:29.338 [ome.event.ItemCommandEvent] - Item 'tuya_OG_Schlafzimmer1_mqtt' received command ON
2019-01-11 11:39:29.343 [nt.ItemStatePredictedEvent] - tuya_OG_Schlafzimmer1_mqtt predicted to become ON
2019-01-11 11:39:29.358 [vent.ItemStateChangedEvent] - tuya_OG_Schlafzimmer1_mqtt changed from OFF to ON

Changing state with external app is still reflected:

2019-01-11 11:39:33.398 [vent.ItemStateChangedEvent] - tuya_OG_Schlafzimmer1_mqtt changed from OFF to ON
2019-01-11 11:39:34.182 [vent.ItemStateChangedEvent] - tuya_OG_Schlafzimmer1_mqtt changed from ON to OFF

The behaviour is like before updating the script. I even tried sitemap with

Switch item=tuya_OG_Schlafzimmer1_mqtt label="LED1 MQTT 1" mappings=[ON="On", OFF="Off"]

or without mapping

Switch item=tuya_OG_Schlafzimmer1_mqtt label="LED1 MQTT 1"

no difference.

Any ideas what I missed?

Have you tried to remove the %s in the outgoing format

The sitemap mappings will have not influence on the actual state on of the item and the command sent.

@vzorglub

I tried to remove the %s. First, it gives me a 409:conflict in PaperUI when doing so, but after saving the cahnge is reflected in PaperUI:

Sending command is still not working, but when I change topic to

commandTopic="tuya/lightbulb/12204702807d3a49b2ae/dd28170cbf5dcd08/192.168.75.118/command/on

it still behaves like the “old” way, i.e. it switches at least on.

I put my hands up, I don’t know
My guess… A bug in the TUYA-MQTT bridge

Let’s see if @tsightler has an idea. Otherwise I have to workaround with different channels and Proxy item, but I rather prefer a “clean” solution.

Open an issue on the gitHUB repo:

You defined your things via text file, didn’t you?
That’s why you have a 409 error

Add this to the thing:
formatBeforePublish=""

Bridge mqtt:broker:myUnsecureBroker [ host="127.0.0.1", secure=false ]
{
    Thing mqtt:topic:mything {
    Channels:
        Type switch : Tuya_Bulb_1_mqtt "LED1 MQTT" [ stateTopic="tuya/lightbulb/12204702807d3a49b2ae/dd28170cbf5dcd08/192.168.75.118/state", commandTopic="tuya/lightbulb/12204702807d3a49b2ae/dd28170cbf5dcd08/192.168.75.118/command", formatBeforePublish=""]
    }
}

Also there is a bug in the binding with text configured things.
After modifying them they loose connection.
The bug has been fixed in the snapshots

Did you try to just send a command/payload with a mqtt client?

1 Like

Good idea!!

@vzorglub

You pushed me into the right direction.

I removed the text file mqtt.things and created bridge and generic MQTT-thing in Paper UI, using the same topics as in the mqtt.things file.

Now the command topic is working perfectly!

And I am super happy as I finally was able to move to the 2.4 MQTT binding.