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

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.

@tebore, can you please show me your

Java Scripts getTuyaDimmer.js file

Do you put that in which directory in the openhab2 installation. I have never used transformationPatterns before. Do you have a topic you can point me to so I can read about this?

are you using a local mqtt broker called “mqtt”??

MQTT is the name of the broker yes
No Need for JS Transform see my reply below.

I noticed a couple “bugs” for anyone one OH 2.4 Stable Using MQTT 2.x binding.
Because The steps only transforms for incoming messages we’re going to need to get creative as the binding will send Percent values and it will receive back a value 1-255. Thankfully after digging in to how AgentK handles RGB bulbs.
I noticed the Agent K also uses DPS:3 to set brightness with great error handling in the color handling part.

So If you’re using a dimmer you can leverage the following Things file example to get it to send % values and get back % values (Again thanks to Agent K doing all the % conversion to a value 1-255)
So no need for a JS Transform.

//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", commandTopic="tuya/<tuyAPI-id>/<tuyAPI-key>/<tuyAPI-ip>/color", min=1, max=255, step=1, formatBeforePublish="0,0,%s" ]
    }

@tebore, I am having a hard time getting the dimmer to reset back to the previous dimmer level after the Tuya Bulb has been shut off.

For example, I have a dimmer setup that is connect to the MQTT color Channel.

I set the dimmer level to 55%

then I shut off the Tuya Bulb using the Switch connected to the MQTT channel for switching the Tuya Bulb off and on.

When I turn the Bulb back ON the dimmer level resets to 10% instead of the previous 55%

@tebore, How do I fix this behavior?

Is this using the adjustment in the latest post. Because it sounds like the issue i discovered when using my first post. Which is when reporting directly back from dps/3 you’re getting a value of brightness that’s mapped between 1-255 and not the %. If you use the color topic and adjust the format before send it should work.

FYI you can us JS to transform the data being sent out. TransformationPattern formats data in and TransformationPatternOut formats data going out.

That’s incorrect in 2.4 the binding does not have that. That is only in 2.5 M1

Hello :slight_smile: I have several TECKIN and several Emylo SS-8839-03. No problem with TECKIN, all work fine. But with Emylo SS-8839-03, no command work. Any ideas ? I use MQTT-Binding 2.4 configuration . Thank you very much :slight_smile: (and sorry for my bad english)

Primarily, my post handles the control of colors of my hama WIFI Bulbs @HolgiHab I am very grateful for this tutorial and therefore I want to share my configuration.

The tutorial and those who wrote comments helped me to control the lamps (on, off, brightness). For the colors I had to think for myself :stuck_out_tongue_winking_eye:

My problem is already quite aptly described by @GadgetAngel

I tested the configuration with lamps on which the manufacturer “hama” has stuck his logo. Product Name: “WiFi-LED-Lampe, E27” or “WiFi LED-Bulp, E27” Product Number: 176550 (dimmable, warm white to daylight), 176547 (free color selection, dimmable)

There is no thing needed to control the colors. For completeness, I still post the things file.

tuyamqtt.things
Bridge mqtt:broker:e8dc2f1c "Tuya MQTT Broker" @ "Tuya" [ 
  host="127.0.0.1",
  secure=false,
  port=1883,
  qos=0,
  retain=false,
  clientid="paho3304251554845",
  //certificate="",
  //certificatepin=false,
  //publickey="",
  //publickeypin=false,
  keep_alive_time=30000,
  reconnect_time=60000,
  //lastwill_message="",
  //lastwill_qos=1,
  //lastwill_topic="",
  username="username",
  password="password"
]
{
    Thing mqtt:topic:HAMAColorBulbTwo "HAMABulbSwitch" @ "Tuya"  {
        Channels:
        Type switch : SWITCH 	[ 
            stateTopic="tuya/<tuyAPI-id>/<tuyAPI-key>/<tuyAPI-ip>/state", 
            commandTopic="tuya/<tuyAPI-id>/<tuyAPI-key>/<tuyAPI-ip>/command",
            on="ON", 
            off="OFF"
        ]
        Type dimmer : DIMMER 	[ 
             stateTopic="tuya/<tuyAPI-id>/<tuyAPI-key>/<tuyAPI-ip>/dps/3", 
             commandTopic="tuya/<tuyAPI-id>/<tuyAPI-key>/<tuyAPI-ip>/color", 
             min=1, 
             max=255, 
             step=1, 
             formatBeforePublish="0,0,%s" 
         ]
    }
}

colorbulbs.items

Switch HAMAColorBulbSwitchTwo   "HAMABulbSwitch"        <switch>    ["Switchable"]             {channel="mqtt:topic:HAMAColorBulbTwo:SWITCH"} 
Dimmer HAMAColorBulbDimmerTwo 	"Dimmer" 	            <dimmer>		                       {channel="mqtt:topic:HAMAColorBulbTwo:DIMMER" }
Color HAMAColorBulb_colorTwo    "Colorpicker"

colorbulb.rules

var HSBType hsbValue

rule "LEDColorChangingTwo"

when 
Item HAMAColorBulb_colorTwo received command
then 
    hsbValue = receivedCommand
              
    var red = hsbValue.red.intValue * 255 / 100
    var green = hsbValue.green.intValue * 255 / 100
    var blue = hsbValue.blue.intValue * 255 / 100   

    var String redValue = Long.toHexString(red)
    if (red < 16) redValue = "0" + redValue

    var String greenValue = Long.toHexString(green)
    if (green < 16) greenValue = "0" + greenValue

    var String blueValue = Long.toHexString(blue)
    if (blue < 16) blueValue = "0" + blueValue

    var rgbString = redValue + greenValue + blueValue
   
    val mqttActions = getActions("mqtt", "mqtt:broker:e8dc2f1c")
    mqttActions.publishMQTT("tuya/<tuyAPI-id>/<tuyAPI-key>/<tuyAPI-ip>/command","{ \"multiple\": true, \"data\": { \"1\": true, \"2\": \"colour\", \"5\": \""+rgbString+"002effff\" } }")
    end

I have freed the rule from logging. Feel free to log what you want.

I use the Habpanel to control the lamps. For the MQTT-things a simple switch and the dimmer can be used. The colors I have controlled so far with the ready-made widget (colorpicker).

1 Like

Hi people,
I’m new to this so i’m trying to this step by step, and link some TUYA wall touch switches and PIR sensors.

Did managed to do some installations and obtain the tuya keys, but having issues running the following script:
/usr/bin/node /etc/openhab2/scripts/tuyaapi_mqtt/tuya-mqtt.js

$ /usr/bin/node /etc/openhab2/scripts/tuyaapi_mqtt/tuya-mqtt.js
module.js:549
    throw err;
    ^

    Error: Cannot find module '/etc/openhab2/scripts/tuyaapi_mqtt/tuya-mqtt.js'
    at Function.Module._resolveFilename (module.js:547:15)
    at Function.Module._load (module.js:474:25)
    at Function.Module.runMain (module.js:693:10)
    at startup (bootstrap_node.js:188:16)
    at bootstrap_node.js:609:3

Will appreciate any help.

Many thanks

Hello , update for tuya devise.

tsightler has updated the script of TheAgentK ,now it work with device who used the 3.3 protocol. the script is here : https://github.com/tsightler/tuya-mqtt , but in few days it will be update the script on the officiel page. There is also the discovery ip, no need to put ip on the state topic and command topic.

Example :

tuya/<tuyAPI-id>/<tuyAPI-key>/discover/state
tuya/<tuyAPI-id>/<tuyAPI-key>/discover/command

If you want to put the ip , don"t forget to put ver3.3 between tuya and tuyAPI-id , example :

tuya/ver3.3/<tuyAPI-id>/<tuyAPI-key>/<tuyAPI-ip>/state
tuya/ver3.3/<tuyAPI-id>/<tuyAPI-key>/<tuyAPI-ip>/command

With the version 3.3 protocol you need also tu put on on/open value : 1 or ON and off/closed value : 0 or OFF

For device with DPS, as 2 ways device :

On/open value : { "dps": 1, "set": true }
Off/closed value : { "dps": 1, "set": false } 

and in state topic :
tuya/ver3.3/<tuyAPI-id>/<tuyAPI-key>/<tuyAPI-ip>/dps/1
or
tuya/<tuyAPI-id>/<tuyAPI-key>/discover/dps/1
but here i have a problem, because the tuyapi return true or false value for the state and openhab don’t know true or false state, but work with on or off…

2 Likes

So…after beating my head against the wall for about a fortnight over this it turns out there was a version mismatch.

At least it works now?

Thank you tsightler for the work and Fiftiz for the update! What resources are there around that I can access to improve my knowledge of his this works? I’d like to improve in this area, perhaps even contribute eventually.

Hello, Don’t forget to install the version of today (V2.0.3), there was a leak of memory because of “resolver” in the tuyapi code . but now it’s good. I will try today this new code.