Tuya, MQTT and Openhab 3 (Smartlife Switch)

Hi Flip,

Thanks for the help thus far I am definetly making some good progress now. Ive setup as the channels, things etc as per the How-to and it seems as though both Openhab and the device are talking to the same Topic, however i still don’t get to control the switch… See config below:

config.json

image

Device.conf config below:

image

Openhab Channel Config:

Finally Mqtt Eplorer output:
image

I think /state is missing at the end of your MQTT state topic

I’m surprised. The developers actively support it, and it works well for me. You do have to follow their steps exactly, but I assume you did that.

You have to look below the regular binding list. Once you set up the 3rd party JSON, a new group appears at the end of the Binding list in the UI.

HI Flip,

I made the change in OH and now have the following result in MQTT explorer, with the switch not being activated from OH :slight_smile:

Any thoughts?

I just recently setup an outdoor plug that works well but I created the .thing file and didn’t use auto discovery, however possibly it might highlight your issue?

i.e “/command” missing off your command topic?

Thing mqtt:topic:outdoor_gpo1 "Outdoor GPO's" (mqtt:broker:5b21f1715e) {
    Channels:
        Type switch: switch1_power "Power" [ stateTopic="tuya/outdoor_gpo1/dps/1/state", commandTopic="tuya/outdoor_gpo1/dps/1/command", on="true", off="false" ]
        Type switch: switch2_power "Power" [ stateTopic="tuya/outdoor_gpo1/dps/2/state", commandTopic="tuya/outdoor_gpo1/dps/2/command", on="true", off="false" ]
        Type number: wattage "GPO Wattage" [ stateTopic="tuya/outdoor_gpo1/dps/19/state", unit="W" ]
        Type number: voltage "GPO Voltage" [ stateTopic="tuya/outdoor_gpo1/dps/20/state", unit="V" ]
}

Thanks @NicGray and @Flip

So now when I manually turn on the switch either from the Smartlife app or the push button, Openhab is reading the correct state. However i am an unable to send the command to the switch from Openhab at this stage and I’m at a loss as to where i am going wrong. See configs below:

Mqtt Explorer output looks like this:
image

Thanks again,
George

Your command topic - please build it analog to your state topic

Hi @Flip,

Do you mean make my Command topic:

“tuya/switchwall_plate/dps/1/state”

Sorry for the basic questions, i am a novice…

George

No, meant:

tuya/switchwall_plate/dps/1/command

SO with that change i get the following results in MQTT explorer, with both state and command the same noting that the state doesn’t change only when invoke manually by the switch. I still cannot switch the light on with these changes…

image

When invoke by the switch MQTT Explorer looks like this noting the extra {“1”:true}

image

Is it something in my device.conf file?

Can you paste your Thing config after the last change?

Device.conf is just where you define a device and you can create some friendly names for some of the dps numbers.

@NicGray is this what you are after?

UID: mqtt:topic:5c6a8cb4cc:Light_Switch
label: Light_Switch
thingTypeUID: mqtt:topic
configuration:
payloadNotAvailable: “0”
payloadAvailable: “1”
bridgeUID: mqtt:broker:5c6a8cb4cc
location: Bedroom
channels:

  • id: Light_Switch
    channelTypeUID: mqtt:switch
    label: Switch
    description: “”
    configuration:
    commandTopic: tuya/switchwall_plate/dps/1/command
    postCommand: false
    retained: false
    stateTopic: tuya/switchwall_plate/dps/1/state
    off: “false”
    on: “true”

Looks similar to mine… except I have a “formatBeforePublish”… which looks to be set as standard when creating a channel but yours is missing?

UID: mqtt:topic:outdoor_gpo1
label: Outdoor GPO's
thingTypeUID: mqtt:topic
configuration: {}
bridgeUID: mqtt:broker:5b21f1715e
channels:
  - id: switch1_power
    channelTypeUID: mqtt:switch
    label: Power
    description: null
    configuration:
      retained: false
      postCommand: false
      formatBeforePublish: "%s"
      commandTopic: tuya/outdoor_gpo1/dps/1/command
      stateTopic: tuya/outdoor_gpo1/dps/1/state
      off: "false"
      on: "true"
  - id: switch2_power
    channelTypeUID: mqtt:switch
    label: Power
    description: null
    configuration:
      retained: false
      postCommand: false
      formatBeforePublish: "%s"
      commandTopic: tuya/outdoor_gpo1/dps/2/command
      stateTopic: tuya/outdoor_gpo1/dps/2/state
      off: "false"
      on: "true"

@NicGray thanks, i added the formatBeforePublish: “%s” but it made no difference.

Can you paste your devices.conf file again?

Also your config of both your MQTT Broker and MQTT thing in Openhab

Devices.conf:

config.json file:

image

Mqtt Broker:

UID: mqtt:broker:5c6a8cb4cc
label: MQTT Broker
thingTypeUID: mqtt:broker
configuration:
lwtQos: 0
publickeypin: true
clientid: 8195812c-b625-40df-a59d-94680a7bcbfc
keepAlive: 60
qos: 0
reconnectTime: 60000
host: 192.168.0.107
secure: false
certificatepin: true
lwtRetain: true
enableDiscovery: true

Is that what you are after?

@NicGray @Flip any thoughts on this?

I’m away until Monday, will compare my setup then.

@GeeMan some of your config does not seem to relate to your outputs. Let’s go through each bit.

tuya-mqtt script
config.json - topic is tuya/switch but from your MQTT Explorer output I don’t see the /switch in your tree. It’s also limiting to add the switch part as you might add additional tuya devices later. Let’s just make it tuya/

devices.conf - I also don’t get why your config shows the name as “Wall Plate” yet in your mqtt output it’s switchwall_plate. Call me suspicious of spaces in linux but change devices.conf to be switchwall_plate like your output. It does make me wonder how this is working though or if you have multiple instances? Also make sure you are aware how to update the devices.conf as you have to create a new-devices.conf and use the merge-devices.js to have it updated.

So now either you have found your issue or it’s as it was before where the state of the Tuya switch is updating in Openhab (when controlled directly from device or tuya app) but you cannot control it from Openhab.

The next step would be to put the binding and the tuya-mqtt script into debug and see what happens.

Openhab

ssh openhab@localhost -p 8101

password is habopen

log:set DEBUG org.openhab.binding.mqtt

Ctrl+D to exit

tuya-mqtt script

now start the tuya-mqtt script like so (add sudo or whatever is required for how you set it up)

DEBUG=tuya-mqtt:* node tuya-mqtt.js

Now when I switch on from openhab.log I get:

2022-04-26 07:57:22.856 [DEBUG] [qtt.generic.AbstractMQTTThingHandler] - Successfully published value ON to topic tuya/outdoor_gpo1/dps/1/command
2022-04-26 07:57:24.345 [DEBUG] [qtt.generic.AbstractMQTTThingHandler] - Successfully published value OFF to topic tuya/outdoor_gpo1/dps/1/command

and in the screen output from the tuya-mqtt script

  tuya-mqtt:command Received MQTT message ->  {"topic":"tuya/outdoor_gpo1/dps/1/command","message":"true"} +0ms
  tuya-mqtt:command Received command for DPS1:  true +0ms
  tuya-mqtt:tuyapi Set device bff416b919185d3cd79hba -> {"dps":"1","set":true} +796ms
  tuya-mqtt:tuyapi Received JSON data from device bff416b919185d3cd79hba -> {"101":true} +27ms
  tuya-mqtt:state MQTT DPS JSON: tuya/outdoor_gpo1/dps/state ->  {"101":true} +822ms
  tuya-mqtt:state MQTT DPS101: tuya/outdoor_gpo1/dps/101/state ->  true +0ms
  tuya-mqtt:tuyapi Received JSON data from device bff416b919185d3cd79hba -> {"1":true} +6ms
  tuya-mqtt:state tuya/outdoor_gpo1/switch_1 ON +6ms
  tuya-mqtt:state MQTT DPS JSON: tuya/outdoor_gpo1/dps/state ->  {"1":true} +0ms
  tuya-mqtt:state MQTT DPS1: tuya/outdoor_gpo1/dps/1/state ->  true +0ms
  tuya-mqtt:command Received MQTT message ->  {"topic":"tuya/outdoor_gpo1/dps/1/command","message":"false"} +1s
  tuya-mqtt:command Received command for DPS1:  false +1s
  tuya-mqtt:tuyapi Set device bff416b919185d3cd79hba -> {"dps":"1","set":false} +1s
  tuya-mqtt:tuyapi Received JSON data from device bff416b919185d3cd79hba -> {"101":false} +19ms
  tuya-mqtt:state MQTT DPS JSON: tuya/outdoor_gpo1/dps/state ->  {"101":false} +1s
  tuya-mqtt:state MQTT DPS101: tuya/outdoor_gpo1/dps/101/state ->  false +0ms
  tuya-mqtt:tuyapi Received JSON data from device bff416b919185d3cd79hba -> {"1":false} +61ms
  tuya-mqtt:state tuya/outdoor_gpo1/switch_1 OFF +61ms
  tuya-mqtt:state MQTT DPS JSON: tuya/outdoor_gpo1/dps/state ->  {"1":false} +1ms
  tuya-mqtt:state MQTT DPS1: tuya/outdoor_gpo1/dps/1/state ->  false +0ms

Hopefully this should help you find when the breakdown is happening. If after all this it’s still not working, I would suggest creating a thing file rather than letting Paper UI create it as that is the last difference between our setups.

1 Like

@NicGray you are a genius…

Removed Switch from both openhab and config.json and it works like a charm… Thank-you for your help and apologies for the basic questions…