Step-by-Step guide for adding Tuya-bulbs, Wi-Fi smart LED (Smart Life app) to OH2 using tuya-mqtt.js by AgentK

I will look again, now that I have finished reinstalling openhab2 build 1561 to only find that the MQTT binding is really a mess!!

OK, I actually attempted add dps functionality to the tuya-mqtt, but I don’t use mqtt, and didn’t test if it works, but you should be able to see my commit and it shouldn’t be that hard to fix any issues or bugs.
Read my post about it, I don’t know if things have changed since

But you can try my branch, it probably won’t work for the reasons AgentK mentioned but it should be fairly trivial to fix them if you know how mqtt works. AgentK has everything already there except for a few lines joining it all together.

I’m pretty busy atm so if you want to get tuya-mqtt working with dps setting, just tell AgentK to merge and fix my branch.

I don’t want to hijack this thread but you did mention my scripts.

My njstuya script has a js script, and example, rules, items to get it working with your system. It’s more of a manual process and no-where as good as a properly working mqtt setup. But I give you pretty much complete control to send the exact dps setting you want. It works very similar to tuya-cli, but I have some rules and items which means it’s simpler to integrate into openhab. It’s up to you what you do, they both are based on tuyapi, which I’ve also started making contributions to.

The new things I was working on is cloud based control, so you don’t have to perform a mitma, all you need to do is register the devices like normal with your smartlife app, and use those credentials to control your device. I’ve added those features to njsTuya, so it can either work locally only using tuyapi, or via the cloud using GitHub - unparagoned/cloudtuya: Tuya control over the cloud Now cloudtuya doesn’t let you directly set the dps value, and the api is a bit strange. It’s also may be a bit buggy and there are various options and settings I haven’t tested.

1 Like

This is probably due to the fact the device can only have one connection. So if MQTT has a persistent connection to the device, tuyapi won’t work. It’s like if you have the smartlife app open it won’t work.

If you have any reliability issues, make sure you have the lastest version of tuya-cli and tuyapi, then post an issue on github with details and we’ll have a look to fix it for you. Append command with DEBUG=* to get the all the debug data.

See my post above and branch where I effectively add that functionality to tuya-mqtt, it will need some tweaking to work properly,
unparagoned/tuya-mqtt

Your analysis is pretty much exactly what I thought. I’ve never used this tuya-mqtt but I did look through the code, and some of the code is a copy and paste from my project, which was flattering. I guess technically I’m kind of a contributor to this project :stuck_out_tongue: I’m not an expert but it looks like AgentK does use dps for setting color, in a very fancy way, but there is no way for you to set the dps. That’s why I made a branch showing how to add that feature. I may be wrong but it does look like it’s a required feature or if it’s there it’s not clear how to use it.

See my post above about how I just have items and rules to integrate the script nicely into OH. It doesn’t have scratch on this awesome mqtt implementation but if you need dps settings and options it’s not bad.

I will check it out. But at the moment my HUSBZB-1 Zigbee port is not getting recognized and I need my zigbee motion sensors back… I will get back to this so, I have not given up on getting the MQTT working with the dps values.

Update: I found your fork. I will down load it after I wake up tomorrow… I finally got all my zigbee stuff back, so next it’s seeing if I can figure out JAVA SCRIPT. I am a VB programmer so the syntax is different and I need to learn more JAVA Script. Can someone recommend a good reference book to read on how to do asynchronous programming with JAVA Script? I need books to learn.

Any advice on a good book would be appreciated. My background is instrumentation and system programming. I also enjoy compiler theory so I know how to program I just need to get familiar with Java object oriented programming.

Good night, I am going to bed for the next 10 hours!!:sleeping::smirk:

Here is what I am talking about, I ran these two commands back to back. I do not understand why it states “undefined” and then I run it again and it does what it should had done the first time

PS C:\openhab2\userdata\etc\scripts> node  c:/openhab2/userdata/etc/scripts/node_modules/njstuya/njstuya.js -ip 192.168.0.30 -id 5846055184f3eb20bfaf -key afb9b013346054e6 -set '{ \"dps\": 2, \"set\": \"white\" }' DEBUG=*
(node:12400) UnhandledPromiseRejectionWarning: undefined
(node:12400) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:12400) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
PS C:\openhab2\userdata\etc\scripts> node  c:/openhab2/userdata/etc/scripts/node_modules/njstuya/njstuya.js -ip 192.168.0.30 -id 5846055184f3eb20bfaf -key afb9b013346054e6 -set '{ \"dps\": 2, \"set\": \"white\" }' DEBUG=*
OFF
PS C:\openhab2\userdata\etc\scripts>

I tried stepping thru your code but you are more familiar with the code then me, since their is alot of timing stuff going on. It take me to long to figure out what variables I need to look at.

But if you could figure out why the above happens, I would appreciate it.

I am leaning toward using your code but I have to know if it executed correctly. I hate the idea of having to send a command twice just to make sure the command goes thru to the bulb

BTW it reported that the bulb is OFF when it was ON

I don’t want to hijack this thread. So keep this thread about the mqtt script by AgentK. If you want to talk about njsTuya please use the other thread Tuya Switch Control (Jinvoo, Smart Life)

OK, sorry about that

TheAgent/tuya-mqtt

I have submitted a pull request to TheAgent/tuya-mqtt.

I have added the ability for users to set multiple dps in the MQTT topic.

The new MQTT topic is dpsJ

so to use the topic, the JSON string is different than what you are use to. The format is as follows:

{ “dps as index”: dpsvalue, “dps as index”: dpsvalue }

Here are examples of the JSON string:

JSON string for the topic dpsJ

{ "1": false }
{ "1": true }
{ "2": "scene_1" }
{ "2": "scene_2" }
{ "2": "scene_3" }
{ "2": "scene_4" }
{ "2": "scene", "6": "c479000025ffc3" )
{ "2": "scene", "6": "fffcf7002607ff" }
{ "2": "scene", "6": "d438000010ffd3" }
{ "2": "scene", "6": "3a59bd00e2afbc" }
{ "2": "colour", "5": "ff0033015cffff" }
{ "2": "colour", "5": "ffc400002effff" }
{ "2": "colour", "5": "000dff00edffff" }
{ "2": "colour", "5": "00ff040079ffff" }
{ "2": "white", "3": 255, "4": 255 }
{ "2": "white", "3": 138, "4": 255 }
{ "2": "white", "3": 27, "4": 255 }

NOTE:
this JSON string can be placed in the ON=" on string " or OFF=" off string" in the Generic MQTT Thing or this JSON string my be placed as the next topic behind the dpsJ topic. Here is what I mean.

command Topic:

tuya/<tuyaAPI-type>/<tuyaAPI-id>/<tuyaAPI-key>/<tuyaAPI-ip>/dpsJ/{ "2": "white", "3": 27, "4": 255 }

or 

tuya/<tuyaAPI-type>/<tuyaAPI-id>/<tuyaAPI-key>/<tuyaAPI-ip>/dpsJ

and     on="{ '2': 'white', '3': 27, '4': 255 }" is set 

A side note:

I noticed, while testing the code, that the software on initial start up gets an “Error: connection not made to device”. I believe it is a timing problem. If I submit the command again (and this includes the command topic also) the ERROR goes away and tuya-mqtt.exe can now talk to the device. So, if any of you, have seen error messages in your log , I would not worry to much. You will notice it only occurs when the system reboots and it will only occurr on the very first communication between tuya-mqtt.exe and a Tuya Plug/Bulb. On the second communication the problem goes away. I am sorry I could not figure out how to fix it because the error originates in the tuyapi code not in the tuya-mqtt.exe code.

TheAgent/tuya-mqtt, if you have any question please contact me.

thanks for considering my addition to your code :sunglasses:

1 Like

@AgentK Hi, I hope you are still maintaining the MQTT code tuya-mqtt.js

I would like for you to consider my pull request. I have fixed some issues with tuya-mqtt.exe and even made it so the unhandled promise error does not get sent to the openhab2 log file. It will print out when set DEBUG=* is used when calling node to execute the code manually.

If you decide not to add it, that’s OK too. I am just trying to help out.

Here is what the update allows:

This new update allows the user to set multiple dps values via MQTT messages or topics

State Topic

tuya/<tuyaAPI-type>/<tuyaAPI-id></tuyaAPI-key>/<tuyaAPI-ip>/state

where <tuyaAPI-type> is either socket or lightbulb

Command Topic

tuya/<tuyaAPI-type>/<tuyaAPI-id>/<tuyaAPI-key>/<tuyaAPI-ip>/command 


where <tuyaAPI-type> is either socket or lightbulb

The latest change: I got rid of the additional topic (dspJ TOPIC IS GONE, YOU MUST use the “multiple” format of the JSON string if you want to set multiple dps values), I use the command topic.
where the command topic can be followed by the JSON string for the dps settings or the message string can hold the JSON string of dps settings. The JSON string can also be of the format for multiple dps settings.

The “on” or “ON” or “off” or “OFF” or “1” or “0” can also follow the command topic or appear in the message area (on="" or off=""). IT all works now.

the two JSON strings are as follows:

  1. dps settings: { “dps”: 1, “set”: true } ----- this only sets on dps value
  2. multiple dps settings: { “multiple”: true, “data”: { “1”: true, “7”: true }

So to put this all together, the dps topic still returns the status of the device.

There fore,
state topic

tuya/<tuyaAPI-type>/<tuyaAPI-id></tuyaAPI-key>/<tuyaAPI-ip>/state

where <tuyaAPI-type> is either socket or lightbulb

command topic

tuya/<tuyaAPI-type>/<tuyaAPI-id>/<tuyaAPI-key>/<tuyaAPI-ip>/command 


where <tuyaAPI-type> is either socket or lightbulb

IF using the on=" " or off=" " place whats in the below box in those strings

1
0
ON
OFF
{ "dps": 1, "set": true }
{ "multiple": true, "data": { "1": true, "7": true }

* don't forget to escape sequence the " marks *

so with escape sequences:

'{ \"dps\": 1, \"set\": true }' or "{ \'dps\': 1, \'set\': true }" or

 " \"{ \\\"dps\\\": 1, \\\"set\\\": true }\" "



'{ "multiple": true, "data": { "1": true, "7": true }'  or "{ 'multiple': true, 'data': { '1': true, '7': true }" or

 " \"{ \\\"multiple\\\": true, \\\"data\\\": { \\\"1\\\": true, \\\"7\\\": true }\" "

To set one or multiple dps’ the command topic can look like the following:

tuya/<tuyaAPI-type>/<tuyaAPI-id>/<tuyaAPI-key>/<tuyaAPI-ip>/command/ON
tuya/<tuyaAPI-type>/<tuyaAPI-id>/<tuyaAPI-key>/<tuyaAPI-ip>/command/OFF
tuya/<tuyaAPI-type>/<tuyaAPI-id>/<tuyaAPI-key>/<tuyaAPI-ip>/command/1
tuya/<tuyaAPI-type>/<tuyaAPI-id>/<tuyaAPI-key>/<tuyaAPI-ip>/command/0
tuya/<tuyaAPI-type>/<tuyaAPI-id>/<tuyaAPI-key>/<tuyaAPI-ip>/command/{ "dps": 1, "set": true } 
tuya/<tuyaAPI-type>/<tuyaAPI-id>/<tuyaAPI-key>/<tuyaAPI-ip>/command/{ "multiple": true, "data": { "1": true, "7": true }

NOTE: During my tests, i noticed that when the tuya-mqtt.exe program is first started (initial boot up) the program gets an unhanded promise rejection warning. I found a way to suppress the error message so that the user will not see this error in the openhab2 error log.

If you are not aware, the error message only shows up on initial boot or initial communication with a tuya device. After the first communication has been establish, [by sending the first command twice in a row,] then as long as the tuya-mqtt server is up and running, the error will not return until reboot of the tuya-mqtt server.

Therefore, I would recommend to openhab2 users that if they use tuya-mqtt.exe they should upon system reboot send two state commands to each device they plan to talk to. This can be accomplished in the Tuya.rules file.

I believe this is a TCP/IP problem. These tuya devices use a TCP and that protocol is not considered to be deterministic. The error message is coming from a routine outside the scope of tuya-mqtt code.

So the best I could do is suppress the Error from the openhab2 log file. You will git this error for every tuya device tuya-mqtt will talk to on the first try. I have 18 devices and I really do not want that many errors is my log file.

Well that all from me. :wink:

Hey @GadgetAngel,

I had a lot of work to do in the last months and couldn’t really follow the project any further. Since everything works with my 5 devices so far, I didn’t have the need to do anything.

But I am very pleased that my Scipt finds so much resonance here. I will try to look at your pull-request in the next days and integrate it if necessary.

But already in advance I thank you for the support!

Best regards
AgentK

For WINDOWS USERS or Linux USERS:

As a windows user, or Linux user, you can use the openhab2 MQTT-Embedded-broker or install Mosquitto Broker.

If you plan to use the openhab2 MQTT-Embedded-Broker than the following information is important:

NOTE: THE EMBEDDED MQTT BROKER can be found at:

ADDONS —> Misc.-----> “MQTT Broker Moquette”

One of the hardest things to get working on a windows openhab2 server is to install Mosquitto.

I thought I do this write up to help others do what I have been able to do.

To install Mosquitto on your machine. Here is an excellent step-by-step guide on how to install mosquitto on windows and linux machines

I used Steve’s mosquitto files (see below picture, just link on the picture to go to Steve’s Website, click on the link labeled Mosquitto 1.5.8 Windows Files).

I only downloaded from his site because his mosquitto files were the only ones that passed www.virustotal.com virus scans. Steve’s Files had no viruses in them at all.

NOTE:
Be care of other mosquitto files downloads. To be sure go to www.virustotal.com and upload the files you want to install before you install them on your machine, even if you download the files from the from Eclipse website.

Here is the URL to the excellent Step-By-Step Guide To Installing Mosquitto (click on the picture to go to his website):

So how would one use a dimmer on this?
OH passes a value from 0-100.
DPS needs 1-255
I assume a rule would need to be set in the sense of, if the dimmer item changes
do the transformation from 1-255 to % and or vice versa
send a payload of {multiple, data, %, DPS} and the converted number for the %.

Is my thinking right or is there a simpler way?

When you define the dimmer ‘Thing’ you can set a min of 0 amd max of 255. Then an slider of 0-100 will send a percentage of 255.

I haven’t been using MQTT 2, but thanks.

Actually just setting up the binding now. There’s no Dimmer channel in the thing at least in 2.4.
The closest would be percent. The documentation doesn’t line up with what the binding provides.

@tebore, Hi,

I am just starting to check on this myself. I too want to dim lights and set the color on the color Tuya Bulbs.

As soon as I test it, I will let you know how I did it.

@tebore

Well, after a lot of work I finally got this working with color light bulbs. The problem was me (operator error). I own two different brands of Tuya Color light Bulbs. Lumiman Smart Color Bulbs found here

and Elelight Smart Color Bulbs found here .

I have mostly been testing with the Elelight brand…that was my mistake!! For some dumb reason the Elelight Color Bulbs do not set color the same way other Tuya Bulbs do. Most use Hue, Saturation and Brightness. Nope these bulbs use dps “5” to hold a 8 hex format of the color. I am having a hard time figuring out what format they are using to store the color in. So after I figured out that the Elelight Bulbs are not like other Tuya Bulbs, I started to use the Lumiman Bulbs…Which work exactly as @AgentK describes in his write up.

UPDATE: I can now control my Elelight Smart bulbs, these bulbs for some reason use RGB values instead of HSL values. So I use the following rule to get the RGB values from the HSBtype

rule "LEDColorChanging"
then Item LEDColor changed
then 	
	logInfo("LED", "Color changing")
		
	hsbValue = LEDColor.state as HSBType
	redValue   = String.format("%02x", (hsbValue.red.floatValue * 2.55) as int)
	greenValue = String.format("%02x", (hsbValue.green.floatValue * 2.55) as int)
	blueValue  = String.format("%02x", (hsbValue.blue.floatValue * 2.55) as int)
    
	var String color2 = redValue + greenValue + blueValue
	
I then use the color2 in a set color command string and send it out over the bulb's command topic, setting dps 5 with the RGB string and attach on the end "002effff" to finish up the 14 hex digit that dps 5 needs to set color.

so the command going out would look like this:
Topic ="tuya/<tuyaAPI-type>/<tuyaAPI-id>/<tuyaAPI-key>/<tuyaAPI-ip>/command", payload="{ \"multiple\": true, \"data\": { \"1\": true, \"2\": \"colour\", \"5\": \"<color2>002effff\" } }", where <color2> is the concatenated string of (red_in_hex + green_in_hex + blue_in_hex)

end

BTW, thank you @AgentK for updating your documentation. Here is his new instructions for using his wonderful MQTT translation software for Tuya Plugs and Tuya Bulbs https://github.com/TheAgentK/tuya-mqtt#tuyapi-mqtt-client

First off you need to make a couple of decisions when you decide to use tuya-mqtt.exe. Are you going to install your own MQTT Broker or are you going to use the Moquette System Embedded MQTT Broker in openHAB2??

well if you are going to use the System Embedded MQTT Broker than be aware of a little problem. I like to configure everything in .things and .items files. It makes it really easy to upgrade to newer versions of the snapshot. BUT, for some reason if I defined the Moquette System Embedded MQTT Broker in a .things file, the openHAB2 system will say it is OFFLINE and NOT INITIALIZED. BUT, if you go in and MANUALLY add the Moquette System Embedded MQTT Broker thru the PAPER UI and THEN and ONLY THEN enable your .things file with the Generic MQTT Thing defined (the system will complain with an WARN message but still goes ahead and uses the “Moquette System Embedded MQTT Broker” with the Generic MQTT thing. It took some time trying to figure out why the system would not initialize the “Moquette System Embedded MQTT Broker”

so here are some things some of you may not be aware of:

  1. To have openhab2 install a misc addon you include it in your addons.cfg file under the misc = line. So to include the “Moquette System Embedded MQTT Broker” , uncomment out the last line of your addons.cfg and make it look like this: misc = mqttbroker . Ensure that you have mqtt on the line for binding=
  2. There is NO NEED for a Mosquitto.cfg file or Moquette.cfg file !!! PLEASE DO NOT CREATE THEM.

here is an example of using the “Moquette System Embedded MQTT Broker” with a Generic MQTT Thing in the mqtt.things file:

Bridge         mqtt:systemBroker:embedded-mqtt-broker                             "Moquette System Embedded MQTT Broker"        @ "Family Room"                          [host="localhost", secure=false, retain=false, url="localhost:1883" ] {
// IMPORTANT: do not have another process using the 1883 port on your computer if you plan to use the systemBroker:embedded-mqtt-broker "Moquette"
// to use the embedded-mqtt-broker (Moquette) in openhab2, one must first add it MANUALLY via the paper UI then you can add channels to it
// The openhab2 log file will complain because the SystemBroker was added manually so you will see this log message : 
//      [WARN ] [core.common.registry.AbstractRegistry] - Cannot add BridgeImpl with key mqtt:systemBroker:embedded-mqtt-broker. It exists already from provider ManagedThingProvider! Failed to add a second with the same UID from provider GenericThingProvider!
// If you try use the .things file without adding the MQTT SystemBroker manually, openHAB2 will show the MQTT System Broker as UNINITIALIZED(HANDLER_CONFIGURATION_PENDING) or UNINITIALIZED (HANDLER_MISSING_ERROR)
//Therfore, I stress again that one needs to add the embedded-mqtt-broker (Moquette) via the PAPER UI FIRST before using the .things file

Thing  topic Generic_Moquette_MQTT_Thing                                                               "Generic Moquette MQTT Thing"  @ "Family Room"          {                         
        Channels:
                Type    switch : MasterBedroom_EchoPlus_MQTT                                            "Master Bedroom Echo Plus MQTT Channel"                                                                                                                 [ stateTopic="tuya/socket/53023030840d8e59a080/b5beb7145d42ddbb/192.168.0.29/state", commandTopic="tuya/socket/53023030840d8e59a080/b5beb7145d42ddbb/192.168.0.29/command"]                
                Type    switch : Netgear77_MQTT                                                         "NETGEAR 77 MQTT Channel"                                                                                                                               [ stateTopic="tuya/socket/58045435c44f3380fb8d/d0af3bd714dfc0c0/192.168.0.7/state", commandTopic="tuya/socket/58045435c44f3380fb8d/d0af3bd714dfc0c0/192.168.0.7/command"]                
                Type    switch : FamilyRoom_MQTT                                                        "Family Room Echo Plus MQTT Channel"                                                                                                                    [ stateTopic="tuya/socket/15764628dc4f22ff7b04/91939b20b452be9b/192.168.0.19/state", commandTopic="tuya/socket/15764628dc4f22ff7b04/91939b20b452be9b/192.168.0.19/command"]               
                Type    switch : Basement_Garage_MQTT                                                   "Basement Garage Ech Dot MQTT Channel"                                                                                                                  [ stateTopic="tuya/socket/67833830cc50e3d4c9cf/559b607b2f0bac09/192.168.0.37/state", commandTopic="tuya/socket/67833830cc50e3d4c9cf/559b607b2f0bac09/192.168.0.37/command"]                
                Type    switch : Master_Bathroom_MQTT                                                   "Master Bathroom Ech Dot MQTT Channel"                                                                                                                  [ stateTopic="tuya/socket/67833830cc50e3d4e70a/3e75edf0512f34da/192.168.0.36/state", commandTopic="tuya/socket/67833830cc50e3d4e70a/3e75edf0512f34da/192.168.0.36/command"]
                Type    switch : Guest_Bedroom_MQTT                                                     "Guest Bedroom Ech Dot MQTT Channel"                                                                                                                    [ stateTopic="tuya/socket/67833830cc50e3d57761/8afdc92061b67b76/192.168.0.35/state", commandTopic="tuya/socket/67833830cc50e3d57761/8afdc92061b67b76/192.168.0.35/command"]
                Type    switch : Guest_Bathroom_MQTT                                                    "Guest Bathroom Ech Dot MQTT Channel"                                                                                                                   [ stateTopic="tuya/socket/75070011840d8e53bd0f/87559669c5bd8428/192.168.0.10/state", commandTopic="tuya/socket/75070011840d8e53bd0f/87559669c5bd8428/192.168.0.10/command"]
                Type    switch : Netgear86_MQTT                                                         "NETGEAR 86 MQTT Channel"                                                                                                                               [ stateTopic="tuya/socket/05835352dc4f228bf22e/633ad4ce96d1f044/192.168.0.11/state", commandTopic="tuya/socket/05835352dc4f228bf22e/633ad4ce96d1f044/192.168.0.11/command"]
                Type    switch : Netgear86Bridge_MQTT                                                   "NETGEAR 86 BRIDGE MQTT Channel"                                                                                                                        [ stateTopic="tuya/socket/05835352dc4f22f6214e/3f077d0ea34d33a0/192.168.0.4/state", commandTopic="tuya/socket/05835352dc4f22f6214e/3f077d0ea34d33a0/192.168.0.4/command"]        
                Type    switch : Arris_528A_MQTT                                                        "ARRIS-528A MQTT Channel"                                                                                                                               [ stateTopic="tuya/socket/50642000840d8e525b6b/b09888ae51da344c/192.168.0.6/state", commandTopic="tuya/socket/50642000840d8e525b6b/b09888ae51da344c/192.168.0.6/command"]        
                Type    switch : Test_Smart_Plug_MQTT                                                   "Test Smart Plug MQTT Channel"                                                                                                                          [ stateTopic="tuya/socket/67833830cc50e3d57eaa/beb06ceb0fed2edb/192.168.0.46/state", commandTopic="tuya/socket/67833830cc50e3d57eaa/beb06ceb0fed2edb/192.168.0.46/command"]
                Type    switch : Detached_Garage_MQTT                                                   "Detached Garage Echo Dot MQTT Channel"                                                                                                                 [ stateTopic="tuya/socket/67833830cc50e3d4cda8/378c12b4183492d5/192.168.0.41/state", commandTopic="tuya/socket/67833830cc50e3d4cda8/378c12b4183492d5/192.168.0.41/command"]   
                Type    switch : ReadingBulb_MQTT                                                       "Reading Bulb MQTT Channel"                                                                                                                             [ stateTopic="tuya/lightbulb/3543376268c63ae40398/c6b5f65f138f5dd0/192.168.0.39/state", commandTopic="tuya/lightbulb/3543376268c63ae40398/c6b5f65f138f5dd0/192.168.0.39/command/toggle"]
                Type    colorHSB : ReadingBulb_Color_MQTT                                               "Reading Bulb Color MQTT Channel"                                                                                                                       [ stateTopic="tuya/lightbulb/3543376268c63ae40398/c6b5f65f138f5dd0/192.168.0.39/state", commandTopic="tuya/lightbulb/3543376268c63ae40398/c6b5f65f138f5dd0/192.168.0.39/color"]
                Type    switch : TableLamp_MQTT                                                         "Table Lamp MQTT Channel"                                                                                                                               [ stateTopic="tuya/lightbulb/3543376268c63ae3f978/25f1f8c229dacaa6/192.168.0.38/state", commandTopic="tuya/lightbulb/3543376268c63ae3f978/25f1f8c229dacaa6/192.168.0.38/command/toggle"] 
                Type    switch : TableLamp_Control_MQTT                                                 "Control Table Lamp MQTT Channel"                                                                                                                       [ stateTopic="tuya/lightbulb/3543376268c63ae3f978/25f1f8c229dacaa6/192.168.0.38/state", commandTopic="tuya/lightbulb/3543376268c63ae3f978/25f1f8c229dacaa6/192.168.0.38/command", on="on", off="off"]
                Type    colorHSB : TableLamp_Color_MQTT                                                 "Table Lamp Color MQTT Channel"                                                                                                                         [ stateTopic="tuya/lightbulb/3543376268c63ae3f978/25f1f8c229dacaa6/192.168.0.38/state", commandTopic="tuya/lightbulb/3543376268c63ae3f978/25f1f8c229dacaa6/192.168.0.38/color"]
                Type    switch : Light90_MQTT_Light_Bulb                                                "Light90 MQTT Light Bulb Channel"                                                                                                                       [ stateTopic="tuya/lightbulb/5846055184f3eb20bfaf/afb9b013346054e6/192.168.0.30/state", commandTopic="tuya/lightbulb/5846055184f3eb20bfaf/afb9b013346054e6/192.168.0.30/command/toggle"]
    }
}

if you look at the file, I give 4 different examples. You see MQTT channels defined with out a on= or off= these are channels that can be used in mqtt.rules file, You will use the following in the rules file to send the on/off commands:

            val mqttActions = getActions("mqtt", "mqtt:systemBroker:embedded-mqtt-broker")
            mqttActions.publishMQTT("tuya/<tuyaAPI-type>/<tuyaAPI-id>/<tuyaAPI-key>/<tuyaAPI-ip>/command", "<1/0>") 

if you choose to define an on="", and off="" with the topic you can just use an Switch or myItem.sendCommand(ON) or myItem.sendCommand(OFF). Just like you would normally. If you look at my file later on you will see where i defined one on=“on” and off=“off” and it works. You can also define on=“1”, off=“0”, or on="{ \“multiple\”: true, \“data\”: { \“1\”: true, \“2\”: \“scene_1\” } }" off=“0”.

Here is a mqtt.things file that uses (NOT EMBEDDED) Moquitto MQTT Broker that runs locally on the same machine as the openhab2 server:

Bridge         mqtt:broker:mosquitto                             "Mosquitto MQTT Broker"        @ "Family Room"                          [  publickeypin=false, lastwill_qos=0, retain=false, secure=false, certificatepin=false, keep_alive_time=60000, qos=2, host="192.168.0.12", reconnect_time=60000 ] {


Thing  topic Generic_Mosquitto_MQTT_Thing                                                               "Generic Mosquitto MQTT Thing"  @ "Family Room"          {                         
        Channels:
                Type    switch : MasterBedroom_EchoPlus_MQTT                                            "Master Bedroom Echo Plus MQTT Channel"                                                                                                                 [ stateTopic="tuya/socket/53023030840d8e59a080/b5beb7145d42ddbb/192.168.0.29/state", commandTopic="tuya/socket/53023030840d8e59a080/b5beb7145d42ddbb/192.168.0.29/command", on="1",off="0" ]
                Type    switch : MasterBedroom_EchoPlus_GET_MQTT                                        "Master Bedroom Echo Plus MQTT STATUS Channel"                                                                                                          [ stateTopic="tuya/socket/53023030840d8e59a080/b5beb7145d42ddbb/192.168.0.29/state", commandTopic="tuya/socket/53023030840d8e59a080/b5beb7145d42ddbb/192.168.0.29/command", on="{ \"schema\": true }", off="{ \"schema\": true }" ]                           

                Type    switch : Test_Smart_Plug_MQTT                                                   "Test Smart Plug MQTT Channel"                                                                                                                          [ stateTopic="tuya/socket/67833830cc50e3d57eaa/beb06ceb0fed2edb/192.168.0.46/state", commandTopic="tuya/socket/67833830cc50e3d57eaa/beb06ceb0fed2edb/192.168.0.46/command", on="1", off="0" ]
                Type    switch : Test_Smart_Plug_USB_MQTT                                               "Test Smart Plug - USB Port MQTT Channel"                                                                                                               [ stateTopic="tuya/socket/67833830cc50e3d57eaa/beb06ceb0fed2edb/192.168.0.46/state", commandTopic="tuya/socket/67833830cc50e3d57eaa/beb06ceb0fed2edb/192.168.0.46/command", on="{ \"dps\": 7, \"set\": true }", off="{ \"dps\": 7, \"set\": false }" ]
                Type    switch : Test_Smart_Plug_GET_MQTT                                               "Test Smart Plug MQTT STATUS Channel"                                                                                                                   [ stateTopic="tuya/socket/67833830cc50e3d57eaa/beb06ceb0fed2edb/192.168.0.46/state", commandTopic="tuya/socket/67833830cc50e3d57eaa/beb06ceb0fed2edb/192.168.0.46/command", on="{ \"schema\": true }", off="{ \"schema\": true }"
        
                Type    switch : ReadingBulb_MQTT                                                       "Reading Bulb MQTT Channel"                                                                                                                             [ stateTopic="tuya/lightbulb/3543376268c63ae40398/c6b5f65f138f5dd0/192.168.0.39/state", commandTopic="tuya/lightbulb/3543376268c63ae40398/c6b5f65f138f5dd0/192.168.0.39/command", on="1", off="0" ]
                Type    switch : ReadingBulb_GET_MQTT                                                   "Reading Bulb MQTT STATUS Channel"                                                                                                                      [ stateTopic="tuya/lightbulb/3543376268c63ae40398/c6b5f65f138f5dd0/192.168.0.39/state", commandTopic="tuya/lightbulb/3543376268c63ae40398/c6b5f65f138f5dd0/192.168.0.39/command", one="{ \"dps\": 7, \"set\": false }", off="{ \"dps\": 7, \"set\": false }" ]
                Type    colorHSB: ReadingBulb_Color_MQTT                                                 "Reading Bulb Color MQTT Channel"                                                                                                                       [ stateTopic="tuya/lightbulb/3543376268c63ae40398/c6b5f65f138f5dd0/192.168.0.39/state", commandTopic="tuya/lightbulb/3543376268c63ae40398/c6b5f65f138f5dd0/192.168.0.39/color"]
                Type    switch : ReadingBulb_Scene1_MQTT                                                "Reading Bulb Scene 1 MQTT Channel"                                                                                                                     [ stateTopic="tuya/lightbulb/3543376268c63ae40398/c6b5f65f138f5dd0/192.168.0.39/state", commandTopic="tuya/lightbulb/3543376268c63ae40398/c6b5f65f138f5dd0/192.168.0.39/command", on="{ \"multiple\": true, \"data\": { \"1\": true, \"2\": \"scene_1\" } }", off="0" ]
 
                Type    switch : ReadingBulb_Night_MQTT                                                 "Reading Bulb Night Scene MQTT Channel"                                                                                                                 [ stateTopic="tuya/lightbulb/3543376268c63ae40398/c6b5f65f138f5dd0/192.168.0.39/state", commandTopic="tuya/lightbulb/3543376268c63ae40398/c6b5f65f138f5dd0/192.168.0.39/command", on="{ \"multiple\": true, \"data\": { \"1\": true, \"2\": \"scene\", \"6\": \"bd76000168ffff\" } }", off="0" ]

                Type    switch : ReadingBulb_Red_MQTT                                                   "Reading Bulb Color RED Channel"                                                                                                                        [ stateTopic="tuya/lightbulb/3543376268c63ae40398/c6b5f65f138f5dd0/192.168.0.39/state", commandTopic="tuya/lightbulb/3543376268c63ae40398/c6b5f65f138f5dd0/192.168.0.39/command", on="{ \"multiple\": true, \"data\": { \"1\": true, \"2\": \"colour\", \"5\": \"ff00000000ffff\" } }", off="0" ]   
                Type    switch : ReadingBulb_Blue_MQTT                                                  "Reading Bulb Color BLUE Channel"                                                                                                                       [ stateTopic="tuya/lightbulb/3543376268c63ae40398/c6b5f65f138f5dd0/192.168.0.39/state", commandTopic="tuya/lightbulb/3543376268c63ae40398/c6b5f65f138f5dd0/192.168.0.39/command", on="{ \"multiple\": true, \"data\": { \"1\": true, \"2\": \"colour\", \"5\": \"1a00ff00f6ffff\" } }" , off="0" ] 

                Type    switch : ReadingBulb_100_White_MQTT                                             "Reading Bulb 100% White Channel"                                                                                                                       [ stateTopic="tuya/lightbulb/3543376268c63ae40398/c6b5f65f138f5dd0/192.168.0.39/state", commandTopic="tuya/lightbulb/3543376268c63ae40398/c6b5f65f138f5dd0/192.168.0.39/command", on="{ \"multiple\": true, \"data\": { \"1\": true, \"2\": \"white\", \"3\": 255, \"4\": 255 } }", off="0" ]  
 
                Type    switch : ReadingBulb_50_White_MQTT                                              "Reading Bulb 50% White Channel"                                                                                                                        [ stateTopic="tuya/lightbulb/3543376268c63ae40398/c6b5f65f138f5dd0/192.168.0.39/state", commandTopic="tuya/lightbulb/3543376268c63ae40398/c6b5f65f138f5dd0/192.168.0.39/command", on="{ \"multiple\": true, \"data\": { \"1\": true, \"2\": \"white\", \"3\": 140, \"4\": 255 } }", off="0" ]  

                Type    switch : ReadingBulb_10_White_MQTT                                              "Reading Bulb 10% White Channel"                                                                                                                        [ stateTopic="tuya/lightbulb/3543376268c63ae40398/c6b5f65f138f5dd0/192.168.0.39/state", commandTopic="tuya/lightbulb/3543376268c63ae40398/c6b5f65f138f5dd0/192.168.0.39/command", on="{ \"multiple\": true, \"data\": { \"1\": true, \"2\": \"white\", \"3\": 48, \"4\": 255 } }", off="0" ]   
                Type    switch : ReadingBulb_1_White_MQTT                                               "Reading Bulb 1% White Channel"                                                                                                                         [ stateTopic="tuya/lightbulb/3543376268c63ae40398/c6b5f65f138f5dd0/192.168.0.39/state", commandTopic="tuya/lightbulb/3543376268c63ae40398/c6b5f65f138f5dd0/192.168.0.39/command", on="{ \"multiple\": true, \"data\": { \"1\": true, \"2\": \"white\", \"3\": 27, \"4\": 255 } }", off="0" ]  
    }
}

Here is my home.sitemap file:

sitemap MangesHome label="Manges Home Automation" {
    
	Frame label="Family Room"{
        
        Switch item=tuyasw202
        Switch item=tuyasw202_CommStart

        Switch item=tuyasw203
        Switch item=tuyasw203_CommStart

        Switch item=tuyasw288
        Switch item=tuyasw288_CommStart
        

        Colorpicker item=Light16_Color
    }
    Frame label="Send a manual -get command to All Tuya Devices" {

        Switch item=Light1_Toggle

        Switch item=tuyasw291

    }
    Frame label="Table Lamp Tuya Bulb"{
        Switch item=Light1_Toggle
        

        Switch item=tuyasw240_CommStart

        Switch item=tuyasw242
        Switch item=tuyasw245
        Switch item=tuyasw249
        Switch item=tuyasw254
        Switch item=tuyasw262
        // controlled by using mqttActions.publishMQTT
        Switch item=tuyasw240
        // controlled by message on=on, off=off
        Switch item=TableLamp_Control_Tuya_Bulb 
        // controlled topic toggle
        Switch item=TableLamp_Tuya_Smart_Bulb  
        Slider item=tuyasw240_level
        // color picked and sent via mqttActions.publishMQTT
        Colorpicker item=tuyasw240_colorpicker icon="colorpicker"
        // color picked and sent via linked MQTT channel
        Colorpicker item=TableLamp_Tuya_Smart_Bulb_Color icon="colorpicker" sendFrequency=30000
        
    }
}

on to the mqtt.items file, I decided to not implement my topics with the use of MQTT channels per command. I wanted to decrease the size of my mqtt.rules file. So I did build up most of the commands with topic strings and defined most of the things in my mqtt.items file:

The next topic will be a continuation of this one, next is the mqtt.items file and mqtt.rules

@tebore, Here is my mqtt.items file:

//GROUPS 
<lightbulb>
Group                           gTuyaTableLampColor                         "Tuya Table Lamp Color Group"                                   <lightbulb>
Group                           gTuyaTableLampAttrib                        "Tuya Table Lamp Attribute Group"
Group:Switch                    gTuya                                       "Tuya devices with USB ports"
Group:Switch                    gTuya2                                      "Tuya devices"

// Manually update all tuya devices
Switch                          tuyasw291                                   "Manually Establish MQTT Communication for All Tuya Devices"


// Turn off NETGEAR 77 Router
Switch                          NETGEAR77_Tuya_Plug                         "NETGEAR 77 Router Smart Plug"                                                                                                                                                                                                                                              {channel="mqtt:topic:embedded-mqtt-broker:Generic_Moquette_MQTT_Thing:Netgear77_MQTT"} 
Switch                          tuyasw202                                   "NETGEAR 77 Router"                                                                                                                                                                             (gTuya)
Switch                          tuyasw202_CommStart                         "NETGEAR 77 Router Establish MQTT Commuication"                                                                                                                                                 (gTuya2)
String                          tuyasw202_CommStart_str                     "tuyasw202"                                                                                                                                                                                     (gTuya2)
String                          tuyasw202_config                            "tuya/socket/58045435c44f3380fb8d/d0af3bd714dfc0c0/192.168.0.7/command"                                                                                                                         (gTuya,gTuya2)
String                          tuyasw202_on                                "1"                                                                                                                                                                                             (gTuya)
String                          tuyasw202_off                               "0"                                                                                                                                                                                             (gTuya)
String                          tuyasw202_state                             "{ \"schema\": true }"                                                                                                                                                                          (gTuya2)

// Mute the Family Room Echo Plus
Switch                          Family_Room_EchoPlus_Tuya_Plug              "Family Room Echo Plus Smart Plug"                                                                                                                                                      (gMuteFamilyRoom,gMuteFrontOfHouse)                                                 {channel="mqtt:topic:embedded-mqtt-broker:Generic_Moquette_MQTT_Thing:FamilyRoom_MQTT"} 
Switch                          tuyasw203                                   "Mute/UnMute Family Room Echo Plus"                                                                                                                                                             (gTuya)
Switch                          tuyasw203_CommStart                         "Family Room Echo Plus Establish MQTT Commuication"                                                                                                                                             (gTuya2)
String                          tuyasw203_CommStart_str                     "tuyasw203"                                                                                                                                                                                     (gTuya2)
String                          tuyasw203_config                            "tuya/socket/15764628dc4f22ff7b04/91939b20b452be9b/192.168.0.19/command"                                                                                                                        (gTuya,gTuya2)
String                          tuyasw203_on                                "ON"                                                                                                                                                                                            (gTuya)
String                          tuyasw203_off                               "OFF"                                                                                                                                                                                           (gTuya)
String                          tuyasw203_state                             "{ \"schema\": true }"                                                                                                                                                                          (gTuya2)

//Test Smart Plug
Switch                          Test_Smart_Tuya_Plug                        "Test Smart Plug"                                                                                                                                                                                                                                                       {channel="mqtt:topic:embedded-mqtt-broker:Generic_Moquette_MQTT_Thing:Test_Smart_Plug_MQTT"}
Switch                          tuyasw288                                   "Test Plug"                                                                                                                                                                                     (gTuya)
Switch                          tuyasw288_CommStart                         "Test Smart Plug Establish MQTT Commuication"                                                                                                                                                   (gTuya2)
String                          tuyasw288_CommStart_str                     "tuyasw288"                                                                                                                                                                                     (gTuya2)
String                          tuyasw288_config                            "tuya/socket/67833830cc50e3d57eaa/beb06ceb0fed2edb/192.168.0.46/command"                                                                                                                        (gTuya,gTuya2)
String                          tuyasw288_on                                "1"                                                                                                                                                                                             (gTuya)
String                          tuyasw288_off                               "0"                                                                                                                                                                                             (gTuya)
String                          tuyasw288_state                             "{ \"schema\": true }"                                                                                                                                                                          (gTuya2)

Switch                          tuyasw289                                   "Test Plug - USB Port on the Tuya Plug"                                                                                                                                                         (gTuya)
String                          tuyasw289_config                            "tuya/socket/67833830cc50e3d57eaa/beb06ceb0fed2edb/192.168.0.46/command"                                                                                                                        (gTuya)
String                          tuyasw289_on                                "{ \"dps\": 1, \"set\": true }"                                                                                                                                                                 (gTuya)
String                          tuyasw289_off                               "{ \"dps\": 1, \"set\": false }"                                                                                                                                                                (gTuya)

// Table Lamp Lumimen Color Smart Bulb
Switch                          TableLamp_Tuya_Smart_Bulb                   "Table Lamp Lumimen by TOGGLE topic"                                                                                                                                                                                                              {channel="mqtt:topic:embedded-mqtt-broker:Generic_Moquette_MQTT_Thing:TableLamp_MQTT"}
Switch                          tuyasw240                                   "Table Lamp by rules"                                                                                                                                                                           (gAll)
Switch                          tuyasw240_CommStart                         "Table Lamp Lumimen Color Bulb Establish MQTT Commuication"                                                                                                                                     (gTuya2)
String                          tuyasw240_CommStart_str                     "tuyasw240"                                                                                                                                                                                     (gTuya2)
String                          tuyasw240_config                            "tuya/lightbulb/3543376268c63ae3f978/25f1f8c229dacaa6/192.168.0.38/command"                                                                                                                     (gTuya,gTuya2)
String                          tuyasw240_on                                "1"                                                                                                                                                                                             (gTuya)
String                          tuyasw240_off                               "0"                                                                                                                                                                                             (gTuya)
String                          tuyasw240_state                             "{ \"schema\": true }"                                                                                                                                                                          (gTuya2)
String                          tuyasw240_colortopic                        "tuya/lightbulb/3543376268c63ae3f978/25f1f8c229dacaa6/192.168.0.38/color"
Color                           tuyasw240_colorpicker                       "Table Lamp Lumimen Color Picker via rules"
Dimmer                          tuyasw240_level                             "Table Lamp White Level [%s %%]"
String                          tuyasw240_color                             "Table Lamp Color Select [%s]"                                                                                                                                                                  (gTuyaTableLampColor)                                                     {channel="mqtt:topic:embedded-mqtt-broker:Generic_Moquette_MQTT_Thing:TableLamp_Color_MQTT"} 
Color                           TableLamp_Tuya_Smart_Bulb_Color             "Table Lamp Lumimen Color Picker via Link topic"                                                                                                                                                                                                                                         {channel="mqtt:topic:embedded-mqtt-broker:Generic_Moquette_MQTT_Thing:TableLamp_Color_MQTT"}
Switch                          TableLamp_Control_Tuya_Bulb                 "Table Lamp by message on and message off"                                                                                                                                                                                                  {channel="mqtt:topic:embedded-mqtt-broker:Generic_Moquette_MQTT_Thing:TableLamp_Control_MQTT"}

//Scene 1
Switch                          tuyasw241                                   "Table Lamp Smart Bulb, Scene 1"                                                                                                                                                                (gTuya,gTuyaTableLampAttrib)
String                          tuyasw241_config                            "tuya/lightbulb/3543376268c63ae3f978/25f1f8c229dacaa6/192.168.0.38/command"                                                                                                                     (gTuya)
String                          tuyasw241_on                                "{ \"multiple\": true, \"data\": { \"1\": true, \"2\": \"scene_1\" } }"                                                                                                                         (gTuya)
String                          tuyasw241_off                               "0"                                                                                                                                                                                             (gTuya)

//Night
Switch                          tuyasw245                                   "Table Lamp Smart Bulb, Night Scene"                                                                                                                                                            (gTuya,gTuyaTableLampAttrib)
String                          tuyasw245_config                            "tuya/lightbulb/3543376268c63ae3f978/25f1f8c229dacaa6/192.168.0.38/command"                                                                                                                     (gTuya)
String                          tuyasw245_on                                "{ \"multiple\": true, \"data\": { \"1\": true, \"2\": \"scene\", \"6\": \"bd76000168ffff\" } }"                                                                                                (gTuya)
String                          tuyasw245_off                               "0"                                                                                                                                                                                             (gTuya)

//Read
Switch                          tuyasw246                                   "Table Lamp Smart Bulb, Read Scene"                                                                                                                                                             (gTuya,gTuyaTableLampAttrib)
String                          tuyasw246_config                            "tuya/lightbulb/3543376268c63ae3f978/25f1f8c229dacaa6/192.168.0.38/command"                                                                                                                     (gTuya)
String                          tuyasw246_on                                "{ \"multiple\": true, \"data\": { \"1\": true, \"2\": \"scene\", \"6\": \"fffcf70168ffff\" } }"                                                                                                (gTuya)
String                          tuyasw246_off                               "0"                                                                                                                                                                                             (gTuya)

//Red
Switch                          tuyasw249                                   "Table Lamp Smart Bulb, Red Color"                                                                                                                                                              (gTuya,gTuyaTableLampAttrib)
String                          tuyasw249_config                            "tuya/lightbulb/3543376268c63ae3f978/25f1f8c229dacaa6/192.168.0.38/command"                                                                                                                     (gTuya)
String                          tuyasw249_on                                "{ \"multiple\": true, \"data\": { \"1\": true, \"2\": \"colour\", \"5\": \"ff00000000ffff\" } }"                                                                                               (gTuya)
String                          tuyasw249_off                               "0"                                                                                                                                                                                             (gTuya)

//Blue
Switch                          tuyasw250                                   "Table Lamp Smart Bulb, Blue Color"                                                                                                                                                             (gTuya,gTuyaTableLampAttrib)
String                          tuyasw250_config                            "tuya/lightbulb/3543376268c63ae3f978/25f1f8c229dacaa6/192.168.0.38/command"                                                                                                                     (gTuya)
String                          tuyasw250_on                                "{ \"multiple\": true, \"data\": { \"1\": true, \"2\": \"colour\", \"5\": \"1a00ff00f6ffff\" } }"                                                                                               (gTuya)
String                          tuyasw250_off                               "0"                                                                                                                                                                                             (gTuya)

//Yellow
Switch                          tuyasw251                                   "Table Lamp Smart Bulb, Yellow Color"                                                                                                                                                           (gTuya,gTuyaTableLampAttrib)
String                          tuyasw251_config                            "tuya/lightbulb/3543376268c63ae3f978/25f1f8c229dacaa6/192.168.0.38/command"                                                                                                                     (gTuya)
String                          tuyasw251_on                                "{ \"multiple\": true, \"data\": { \"1\": true, \"2\": \"colour\", \"5\": \"ffca00002fffff\" } }"                                                                                               (gTuya)
String                          tuyasw251_off                               "0"                                                                                                                                                                                             (gTuya)

//Green
Switch                          tuyasw252                                   "Table Lamp Smart Bulb, Green Color"                                                                                                                                                            (gTuya,gTuyaTableLampAttrib)
String                          tuyasw252_config                            "tuya/lightbulb/3543376268c63ae3f978/25f1f8c229dacaa6/192.168.0.38/command"                                                                                                                     (gTuya)
String                          tuyasw252_on                                "{ \"multiple\": true, \"data\": { \"1\": true, \"2\": \"colour\", \"5\": \"2cff00006dffff\" } }"                                                                                               (gTuya)
String                          tuyasw252_off                               "0"                                                                                                                                                                                             (gTuya)

//White Light
//White to 100%
Switch                          tuyasw253                                   "Table Lamp Smart Bulb, Color White, Brightness 100%"                                                                                                                                           (gTuya,gTuyaTableLampAttrib)
String                          tuyasw253_config                            "tuya/lightbulb/3543376268c63ae3f978/25f1f8c229dacaa6/192.168.0.38/command"                                                                                                                     (gTuya)
String                          tuyasw253_on                                "{ \"multiple\": true, \"data\": { \"1\": true, \"2\": \"white\", \"3\": 255, \"4\": 255 } }"                                                                                                   (gTuya)
String                          tuyasw253_off                               "0"                                                                                                                                                                                             (gTuya)

//White to 90%
Switch                          tuyasw254                                   "Table Lamp Smart Bulb, Color White, Brightness 90%"                                                                                                                                            (gTuya,gTuyaTableLampAttrib)
String                          tuyasw254_config                            "tuya/lightbulb/3543376268c63ae3f978/25f1f8c229dacaa6/192.168.0.38/command"                                                                                                                     (gTuya)
String                          tuyasw254_on                                "{ \"multiple\": true, \"data\": { \"1\": true, \"2\": \"white\", \"3\": 232, \"4\": 255 } }"                                                                                                   (gTuya)
String                          tuyasw254_off                               "0"                                                                                                                                                                                             (gTuya)


//White to 50%
Switch                          tuyasw258                                   "Table Lamp Smart Bulb, Color White, Brightness 50%"                                                                                                                                            (gTuya,gTuyaTableLampAttrib)
String                          tuyasw258_config                            "tuya/lightbulb/3543376268c63ae3f978/25f1f8c229dacaa6/192.168.0.38/command"                                                                                                                     (gTuya)
String                          tuyasw258_on                                "{ \"multiple\": true, \"data\": { \"1\": true, \"2\": \"white\", \"3\": 140, \"4\": 255 } }"                                                                                                   (gTuya)
String                          tuyasw258_off                               "0"                                                                                                                                                                                             (gTuya)

//White to 40%
Switch                          tuyasw259                                   "Table Lamp Smart Bulb, Color White, Brightness 40%"                                                                                                                                            (gTuya,gTuyaTableLampAttrib)
String                          tuyasw259_config                            "tuya/lightbulb/3543376268c63ae3f978/25f1f8c229dacaa6/192.168.0.38/command"                                                                                                                     (gTuya)
String                          tuyasw259_on                                "{ \"multiple\": true, \"data\": { \"1\": true, \"2\": \"white\", \"3\": 117, \"4\": 255 } }"                                                                                                   (gTuya)
String                          tuyasw259_off                               "0"                                                                                                                                                                                             (gTuya)


//White to 10%
Switch                          tuyasw262                                   "Table Lamp Smart Bulb, Color White, Brightness 10%"                                                                                                                                            (gTuya,gTuyaTableLampAttrib)
String                          tuyasw262_config                            "tuya/lightbulb/3543376268c63ae3f978/25f1f8c229dacaa6/192.168.0.38/command"                                                                                                                     (gTuya)
String                          tuyasw262_on                                "{ \"multiple\": true, \"data\": { \"1\": true, \"2\": \"white\", \"3\": 48, \"4\": 255 } }"                                                                                                    (gTuya)
String                          tuyasw262_off                               "0"                                                                                                                                                                                             (gTuya)

//White to 1%
Switch                          tuyasw263                                   "Table Lamp Smart Bulb, Color White, Brightness 1%"                                                                                                                                             (gTuya,gTuyaTableLampAttrib)
String                          tuyasw263_config                            "tuya/lightbulb/3543376268c63ae3f978/25f1f8c229dacaa6/192.168.0.38/command"                                                                                                                     (gTuya)
String                          tuyasw263_on                                "{ \"multiple\": true, \"data\": { \"1\": true, \"2\": \"white\", \"3\": 27, \"4\": 255 } }"                                                                                                    (gTuya)
String                          tuyasw263_off                               "0"                                                                                                                                                                                             (gTuya)

Finally the mqtt.rules file:

// MQTT rules
import java.util.String;
import org.openhab.core.library.types.HSBType;

var init_flag = false
var startup_flag = false

rule "mqtt System Start"
when
    System started
then
    init_flag=true;
    tuyasw291.postUpdate(OFF);
    tuyasw201_CommStart.postUpdate(OFF);
    tuyasw202_CommStart.postUpdate(OFF);
    tuyasw203_CommStart.postUpdate(OFF);
    tuyasw204_CommStart.postUpdate(OFF);
    tuyasw206_CommStart.postUpdate(OFF);
    tuyasw208_CommStart.postUpdate(OFF);
    tuyasw210_CommStart.postUpdate(OFF);
    tuyasw211_CommStart.postUpdate(OFF);
    tuyasw212_CommStart.postUpdate(OFF);
    tuyasw213_CommStart.postUpdate(OFF);
    tuyasw288_CommStart.postUpdate(OFF);
    tuyasw214_CommStart.postUpdate(OFF);
    tuyasw216_CommStart.postUpdate(OFF);
    tuyasw240_CommStart.postUpdate(OFF);
    tuyasw264_CommStart.postUpdate(OFF);
    init_flag = false;
end

// tuya devices
//---------------------------------------------------------------------------------------------------//
// Tuya Devices that can help establish communications
// use this retouine when when system starts up, send two {"schema": true} commands to each unique tuya device
rule "tuyaDevice_2_Crtl"
when
	Member of gTuya2 received command
then
    if(!init_flag){
        //get the name of the real triggeringItem.name
        val configItem14 = gTuya2.members.findFirst[ GenericItem m | m.name == triggeringItem.name+"_str"] 
        val String configStr10 = configItem14.label
        // now use configStr10 as the name of the triggeringItem.name
        val configItem4 = gTuya2.members.findFirst[ GenericItem t | t.name == configStr10+"_config" ] 
        // configItem4.label will be the TOPIC string
        val configItem5 = gTuya2.members.findFirst[ GenericItem q | q.name == configStr10+"_state" ]
        if (triggeringItem.state == OnOffType::ON) {
	        logInfo("tuyaDevice_2_Crtl", "----------->  tuyaDevice_2_Crtl - Member: " + configStr10 + " to: "+triggeringItem.state.toString+" , TOPIC: "+ configItem4.label+" ")	
            logInfo("tuyaDevice_2_Crtl receives command","The member: "+triggeringItem.label+"The command recieved is: "+receivedCommand.toString)
            val mqttActions = getActions("mqtt", "mqtt:systemBroker:embedded-mqtt-broker")
            mqttActions.publishMQTT(configItem4.label, configItem5.label) 
        }
        else if(triggeringItem.state == OnOffType::OFF) {
	        logInfo("tuyaDevice_2_Crtl", "----------->  tuyaDevice_2_Crtl - Member: " + configStr10 + " to: "+triggeringItem.state.toString+" , TOPIC: "+ configItem4.label+" ")
            val mqttActions = getActions("mqtt", "mqtt:systemBroker:embedded-mqtt-broker")
            logInfo("tuyaDevice_2_Crtl receives command","The member: "+triggeringItem.label+"The command recieved is: "+receivedCommand.toString)
            mqttActions.publishMQTT(configItem4.label, configItem5.label) 
        }
        else {
            logInfo("tuyaDevice_2_Crtl", "============8888888==========> Ended up with a triggeringItem.State as neither ON nor OFF, triggeringItem.getState() is"+triggeringItem.getState)
        }
    }
end

// Tuya Devices that have a on and off command
rule "tuyaDevice_1_Crtl"
when
	Member of gTuya received command 
then
    if(!startup_flag){
        //get the config items
        val configItem1 = gTuya.members.findFirst[ GenericItem t | t.name == triggeringItem.name+"_config" ] 
        if (triggeringItem.state == OnOffType::ON) {
            val configItem2 = gTuya.members.findFirst[ GenericItem q | q.name == triggeringItem.name+"_on" ] 
            //configItem2.label contains the _on string
            logInfo("tuyaDevice_1_Crtl", "----------->  tuyaDevice_1_Crtl - Member: "+ triggeringItem.label+" to "+triggeringItem.state.toString+" TOPIC:  " +configItem1.label + " on string: "+configItem2.label)
            val mqttActions = getActions("mqtt", "mqtt:systemBroker:embedded-mqtt-broker")
            mqttActions.publishMQTT(configItem1.label, configItem2.label) 
        }
        else if(triggeringItem.state == OnOffType::OFF) {
            val configItem3 = gTuya.members.findFirst[ GenericItem m | m.name == triggeringItem.name+"_off" ] 
            //configItem3.label contains the _off string
            logInfo("tuyaDevice_1_Crtl", "----------->  tuyaDevice_1_Crtl - Member: "+ triggeringItem.label+" to "+triggeringItem.state.toString+" TOPIC:  " +configItem1.label + " off string: "+configItem3.label)
            val mqttActions = getActions("mqtt", "mqtt:systemBroker:embedded-mqtt-broker")
            mqttActions.publishMQTT(configItem1.label, configItem3.label) 
        } 
        else {
            logInfo("tuyaDevice_1_Crtl", "============8888888==========> Ended up with a triggeringItem.State as neither ON nor OFF, triggeringItem.getState() is"+triggeringItem.getState)
        }
    }
end

// Table Lamp Lumimen Color Smart Bulb
//if main switch for Tuya  Table Lamp gets switched off,  update all other attribute 
//switches to reflect the off state (attribute switches are color, scene etc)
rule " Table Lamp Lumimen Color Smart Bulb receives command"
when
    Item tuyasw240 received command
then
    if(!startup_flag){
        val mqttActions = getActions("mqtt", "mqtt:systemBroker:embedded-mqtt-broker")
        logInfo(" Table Lamp Lumimen Color Smart Bulb receives command","The command recieved is: "+receivedCommand.toString)
        if(receivedCommand == OnOffType::ON) {
            mqttActions.publishMQTT("tuya/lightbulb/3543376268c63ae3f978/25f1f8c229dacaa6/192.168.0.38/command", "1") 
        } else {
            mqttActions.publishMQTT("tuya/lightbulb/3543376268c63ae3f978/25f1f8c229dacaa6/192.168.0.38/command", "0")
        }
        if(receivedCommand == OnOffType::ON) {
            tuyasw240_level.postUpdate(ON)
            tuyasw240_colorpicker.postUpdate(ON)
            TableLamp_Tuya_Smart_Bulb.postUpdate(ON)
            TableLamp_Control_Tuya_Bulb.postUpdate(ON)
            TableLamp_Tuya_Smart_Bulb_Color.postUpdate(ON)
        }
        // change all attributes to off
        if (receivedCommand == OnOffType::OFF) {
            tuyasw240_level.postUpdate(OFF)
            tuyasw240_colorpicker.postUpdate(OFF)
            TableLamp_Tuya_Smart_Bulb.postUpdate(OFF)
            TableLamp_Control_Tuya_Bulb.postUpdate(OFF)
            TableLamp_Tuya_Smart_Bulb_Color.postUpdate(OFF)
            gTuyaTableLampAttrib.members.forEach[ GenericItem i | {
            i.postUpdate(OFF)
            }]
        }
    }
end

// Table Lamp
rule "For Table Lamp Set HSL value for Tuya Light Bulb"
when
        Item tuyasw240_colorpicker received command
then
    if(!startup_flag){
        var appName3 = "Colorpicker.TableLamp"
        var color3 = receivedCommand.toString;
        logInfo(appName3, "-----Table Lamp color picker sent this color value: "+color3);

        //check to see if the light is even in the group
        var myList3 = gTuyaTableLampColor.members.filter[ GenericItem s | s.name.contains("tuyasw240") && s.state != NULL ]
        if(myList3.size > 0) {

            //get all colors and send it via MQTT if light is enabled
            gTuyaTableLampColor.members.forEach[ GenericItem i |
                var name3 = i.name; 
                var stateName3 = name3.toString.split("_color").get(0);
                var stateItem3 = gAll.allMembers.filter [ GenericItem conf | conf.name.contains(stateName3.toString) ].head;

                if(stateItem3.state == ON) {
                        logInfo(appName3, name3 + " change to color: " + color3);
                        //tuyasw240_colorpicker.state or color2
                        val hsbHue3 = (tuyasw240_colorpicker.state as HSBType).hue.intValue;
                        val hsbSat3 = (tuyasw240_colorpicker.state as HSBType).saturation.intValue;
                        val hsbBrt3 = (tuyasw240_colorpicker.state as HSBType).brightness.intValue;

                        color3 = hsbHue3 + "," + hsbSat3 +"," + hsbBrt3;
                        val mqttActions = getActions("mqtt", "mqtt:systemBroker:embedded-mqtt-broker")
                        logInfo(appName3,"For Table Lamp Set HSL value for Tuya Light Bulb","Sending the new color as: "+color3)
                        mqttActions.publishMQTT("tuya/lightbulb/3543376268c63ae3f978/25f1f8c229dacaa6/192.168.0.38/color", color3) 
                        if(hsbHue3 == 0 && hsbSat3 == 0 && hsbBrt3 == 0 ){
                            //user shut off the light update the other switches that control the light
                            tuyasw240.postUpdate(OFF);
                            TableLamp_Tuya_Smart_Bulb.postUpdate(OFF)
                            TableLamp_Control_Tuya_Bulb.postUpdate(OFF)
                            TableLamp_Tuya_Smart_Bulb_Color.postUpdate(OFF)
                            tuyasw240_level.postUpdate(OFF);
                            // turn off other light attributes 
                            gTuyaTableLampAttrib.members.forEach[ GenericItem i | {
                                i.postUpdate(OFF)
                            }]
                        }
                } else {
                    // turn the light on and change the color
                        logInfo(appName3, name3 + " change to color: " + color3);
                        val mqttActions2 = getActions("mqtt", "mqtt:systemBroker:embedded-mqtt-broker")
                        mqttActions2.publishMQTT("tuya/lightbulb/3543376268c63ae3f978/25f1f8c229dacaa6/192.168.0.38/command", "1") 
                        tuyasw240.postUpdate(ON);
                         // update the other switch to reflect the bulb is on
                        TableLamp_Tuya_Smart_Bulb.postUpdate(ON);
                        TableLamp_Control_Tuya_Bulb.postUpdate(ON);
                        TableLamp_Tuya_Smart_Bulb_Color.postUpdate(ON)
                        tuyasw240_level.postUpdate(ON)
                        // change the color now
                        val hsbHue3 = (tuyasw240_colorpicker.state as HSBType).hue.intValue;
                        val hsbSat3 = (tuyasw240_colorpicker.state as HSBType).saturation.intValue;
                        val hsbBrt3 = (tuyasw240_colorpicker.state as HSBType).brightness.intValue;
                        color3 = hsbHue3 + "," + hsbSat3 +"," + hsbBrt3;
                        val mqttActions3 = getActions("mqtt", "mqtt:systemBroker:embedded-mqtt-broker")
                        logInfo(appName3,"For Table Lamp Set HSL value for Tuya Light Bulb","Sending the new color as: "+color3)
                        mqttActions3.publishMQTT("tuya/lightbulb/3543376268c63ae3f978/25f1f8c229dacaa6/192.168.0.38/color", color3) 
                }
            ]
        }
        else {
            logInfo(appName3, triggeringItem.name + "is not currently available, please check this rule should have never got triggered");

        }
    }
end

// when TableLamp_level triggers the user has selected the dimmer which is attached to the MQTT color Channel, this means dps 2 needs to be 'white', to get this make sure the Hue, and Saturation are "0".
rule "Send White Level command to TableLamp"
when
     Item tuyasw240_level received command
then
    if(!startup_flag){
        var appName6 = "WhiteLevel.TableLamp"
        var white6 = receivedCommand.toString;
        logInfo(appName6, "-----Table Lampdimmer sent this value: "+white6);

        //check to see if the light is even in the group
        var myList6 = gTuyaTableLampColor.members.filter[ GenericItem s | s.name.contains("tuyasw240") && s.state != NULL ]
        logInfo(appName6,"myList6.size = "+myList6.size.toString)
        if(myList6.size > 0) {

            //get all colors and send it via MQTT if light is enabled
            gTuyaTableLampColor.members.forEach[ GenericItem i |
                var name6 = i.name; 
                var stateName6 = name6.toString.split("_color").get(0);
                var stateItem6 = gAll.allMembers.filter [ GenericItem conf | conf.name.contains(stateName6.toString) ].head;
                logInfo(appName6, "name6: "+name6+" stateName6: "+stateName6+" stateItem6.name: "+stateItem6.name+" stateItem6.state: "+stateItem6.state)
                if(stateItem6.state == ON) {
                        logInfo(appName6, name6 + " change to: " + white6);
                        if(white6 == "0") {
                            //turn off the light and update the switches
                            val mqttActions = getActions("mqtt", "mqtt:systemBroker:embedded-mqtt-broker")
                            mqttActions.publishMQTT("tuya/lightbulb/3543376268c63ae3f978/25f1f8c229dacaa6/192.168.0.38/command", "0") 
                            tuyasw240.postUpdate(OFF);
                            tuyasw240_level.postUpdate(OFF);
                            TableLamp_Tuya_Smart_Bulb.postUpdate(OFF);
                            TableLamp_Control_Tuya_Bulb.postUpdate(OFF);
                            TableLamp_Tuya_Smart_Bulb_Color.postUpdate(OFF);
                            // change all attributes to off
                            gTuyaTableLampAttrib.members.forEach[ GenericItem i | {
                                i.postUpdate(OFF)
                            }]
                        } else {
                                white6 = "0" + "," + "0" +"," + white6;
                                val mqttActions = getActions("mqtt", "mqtt:systemBroker:embedded-mqtt-broker")
                                logInfo("Send White Level command to TableLamp","Sending the White Level as: "+white6)
                                mqttActions.publishMQTT("tuya/lightbulb/3543376268c63ae3f978/25f1f8c229dacaa6/192.168.0.38/color", white6) 
                        }
                } else {
                    // turn on the light with the appropriate level
                    logInfo(appName6, "tuyasw240_levelState: "+tuyasw240_levelState);
                    // turn on the light with the appropriate level
                    val mqttActions = getActions("mqtt", "mqtt:systemBroker:embedded-mqtt-broker")
                    mqttActions.publishMQTT("tuya/lightbulb/3543376268c63ae3f978/25f1f8c229dacaa6/192.168.0.38/command", "1") 
                    tuyasw240.postUpdate(ON);
                    tuyasw240_level.postUpdate(ON);
                    TableLamp_Tuya_Smart_Bulb.postUpdate(ON);
                    TableLamp_Control_Tuya_Bulb.postUpdate(ON);
                    TableLamp_Tuya_Smart_Bulb_Color.postUpdate(ON);
                    white6 = "0" + "," + "0" +"," + white6;
                    val mqttActions = getActions("mqtt", "mqtt:systemBroker:embedded-mqtt-broker")
                    logInfo(appName6, "Send White Level command to TableLamp","Sending the White Level as: "+white6)
                    mqttActions.publishMQTT("tuya/lightbulb/3543376268c63ae3f978/25f1f8c229dacaa6/192.168.0.38/color", white6) 
                }
            ]

        }
        else {
            logInfo(appName5, triggeringItem.name + "is not currently available, please check this rule. This rule should have never gotten triggered");

        }
    }
end

rule "Table_Lamp_Attributes_turn_Off2"
when 
    Member of gTuyaTableLampAttrib received command OFF
then 
    if(!startup_flag) {
        // update the main switch for the light to reflect that it is off
        logInfo("rules","88888888888888888888888888888888888888 Table_Lamp_Attributes_turn_Off2: In gTuyaTableLampAttrib group received command OFF")
        tuyasw240.postUpdate(OFF)
        tuyasw240.postUpdate(OFF);
        tuyasw240_level.postUpdate(OFF);
        TableLamp_Tuya_Smart_Bulb.postUpdate(OFF);
        TableLamp_Control_Tuya_Bulb.postUpdate(OFF);
        TableLamp_Tuya_Smart_Bulb_Color.postUpdate(OFF);
        tuyasw240_colorpicker.postUpdate(OFF)
        gTuyaTableLampAttrib.members.forEach[ GenericItem i | {
            i.postUpdate(OFF)
        }]
    }
end 

//if tuya light bulb 90 other items like scene changes or color changes turn on you need to update the main light switch to turn on
rule "Table_Lamp_Attributes_turn_on2"
when 
    Member of gTuyaTableLampAttrib received command ON
then 
    if(!startup_flag) {
        // update the main switch for the light to reflect that it is off
        logInfo("rules","88888888888888888888888888888888888888 Table_Lamp_Attributes_turn_on2: In gTuyaTableLampAttrib group received command ON")
        tuyasw240.postUpdate(ON)
        tuyasw240_level.postUpdate(ON);
        TableLamp_Tuya_Smart_Bulb.postUpdate(ON);
        TableLamp_Control_Tuya_Bulb.postUpdate(ON);
        TableLamp_Tuya_Smart_Bulb_Color.postUpdate(ON);
        tuyasw240_colorpicker.postUpdate(ON)
    }
end 

Hope this helps

There surely must be a more elegant way to simply send {“dps”:3, “set”: Desired brightness} than say 10 items each sending hard coded values.

I could imagine it with MQTT 1.x but I’m having a hardtime putting the rule and the transformation logic together in my head using 2.x.

if you want to send the brightness look at the dimmer example in my Home.sitemap, its called tuyasw240_level. There is a rule that reads the dimmer value and sends it to the color MQTT topic for that bulb

So basically I choose my dimmer % value on my sitemap and the light get dimmed

Took a coffee in the morning to figure it out, but this should be more elegant for dimming control

//Thing File for Room
    Thing mqtt:topic:mqtt:Tuya_Room "Room" (mqtt:broker:mqtt) @ "Tuya"	{
    Channels:
        Type switch : SWITCH 	[ stateTopic="tuya/<tuyAPI-id>/<tuyAPI-key>/<tuyAPI-ip>/state", commandTopic="tuya/<tuyAPI-id>/<tuyAPI-key>/<tuyAPI-ip>/command" ]
    	Type dimmer : DIMMER 	[ stateTopic="tuya/<tuyAPI-id>/<tuyAPI-key>/<tuyAPI-ip>/dps/3", transformationPattern="JS:getTuyaDimmer.js", commandTopic="tuya/<tuyAPI-id>/<tuyAPI-key>/<tuyAPI-ip>/command", min=1, max=255, step=1, formatBeforePublish="{ \"dps\": 3, \"set\": %s }" ]
    } 


 //item file 
     Switch Tuya_Room 		"Room"		<light>	(gTuya, gTuya_Switches)		{ channel="mqtt:topic:mqtt:Tuya_Room:SWITCH" }
     Dimmer Tuya_Room_Dimmer 	"Dimmer" 	<light>	(gTuya, gTuya_Switches)		{ channel="mqtt:topic:mqtt:Tuya_Room:DIMMER" }

add your own js transform for the to go from 1-100 to 1-255 and you’re good to go.

Actually from I gather from OH 2.5 M1 MQTT you don’t need the JS transform so you can remove that and based on the min max values it should do the automatic transform from % to 1-255 value.