Zigbee Single Gang Tuya Switch

Hi all

Trying to modify existing drivers / write a new one to get this working with @johan_bendz great Tuya Pro app.

When app is on debug, I can see activity and frames being sent to the Homey Pro, but I cannot get the Homey app to “listen” for these events (i.e. status in Homey is not updating when physical switch is pressed). The other way is working fine (i.e. status on switch is changing when Homey app button is pressed).

How do I get Homey to listen for and respond to the data when pressed?

homey recognises wall switch (debug data):

**zigbee-clusters:cluster** ep: 1, cl: basic (0) read attributes result {

attributes: <Buffer 04 00 00 42 10 5f 54 5a 45 32 30 30 5f 67 62 61 67 6f 69 6c 6f 00 00 00 20 03 01 00 00 20 46 05 00 00 42 06 54 53 30 36 30 31 07 00 00 30 01 fe ff 00 ... 2 more bytes>

} +1ms

2024-01-31T07:28:53.818Z [log] [ManagerDrivers] [Driver:wall_switch_1_gang_tuya] [Device:8c2xxx] 🚀 Wall switch booted up!

data on homey when button is physically pressed (on) (debug data):

  zigbee-clusters:cluster ep: 1, cl: tuya (61184) received frame response tuya.response {
  status: 0,
  transid: 47,
  dp: 1,
  datatype: 1,
  length: 1,
  data: <Buffer 01>

data when pressed (off) (debug data):

  zigbee-clusters:cluster ep: 1, cl: tuya (61184) received frame response tuya.response {
  status: 0,
  transid: 51,
  dp: 1,
  datatype: 1,
  length: 1,
  data: <Buffer 00>

current test code for device.js:

'use strict';

const { debug, Cluster } = require('zigbee-clusters');
const TuyaSpecificCluster = require('../../lib/TuyaSpecificCluster');
const TuyaSpecificClusterDevice = require("../../lib/TuyaSpecificClusterDevice");
const TuyaOnOffCluster = require('../../lib/TuyaOnOffCluster');
//const {getDataValue} = require('./helpers');

Cluster.addCluster(TuyaSpecificCluster);
Cluster.addCluster(TuyaOnOffCluster)

const WALL_SWITCH_DATA_POINTS = {
  onOff: 1
}

class wall_switch_1_gang_tuya extends TuyaSpecificClusterDevice {

  async onNodeInit({ zclNode }) {
    await super.onNodeInit({ zclNode });

    this.printNode();
   // debug(true);

    await zclNode.endpoints[1].clusters.basic.readAttributes('manufacturerName', 'zclVersion', 'appVersion', 'modelId', 'powerSource', 'attributeReportingStatus')
    .catch(err => {
        this.error('Error when reading device attributes ', err);
    });

    this.registerCapabilityListener('onoff', async (onOff) => {
      await this.writeBool(WALL_SWITCH_DATA_POINTS.onOff, onOff)
      this.log('device on/off set', onOff)
  });

    zclNode.endpoints[1].clusters.tuya.on("reporting", value => this.processResponse(value));

    this.log("🚀 Wall switch booted up!")

  }

//new code 29/01
//region Tuya Datapoint Functions

async processResponse(data) {
  const dp = data.dp;
  const parsedValue = getDataValue(data);

  switch (dp) {
      
      case WALL_SWITCH_DATA_POINTS.onOff:
          this.log('Wall switch on/off received', parsedValue);

          try {
              await this.setCapabilityValue('onoff', parsedValue);
          } catch (e) {
              this.log("Failed to set on/off", e);
          }

          break;

      default:
          this.log('Data Point', dp, parsedValue)
  }
}
// binding to 61184 / tuya

//end new code
  onDeleted() {
    this.log('1 Gang Wall GPP Switch removed');
  }

}

module.exports = wall_switch_1_gang_tuya;

Tagging @robertklep in case he has any guidance / suggestions as well :slight_smile:

Also worth pointing out, I’m just delving into JS and first time attempting to modify / develop drivers / app for Homey - but willing to learn!

You’re only attaching a listener to the reporting event, but the data that you’re showing as being logged is using a response event:

zigbee-clusters:cluster ep: 1, cl: tuya (61184) received frame response tuya.response {
                                                               ^^^^^^^^

So try this:

zclNode.endpoints[1].clusters.tuya.on("response", value => this.processResponse(value));
1 Like

@robertklep

Thanks so much for this - I think it appears to be working. I will continue coding later today and if all is well will mark the above as a solution!

1 Like

@robertklep trying something similar for a multi switch (which has 1 main gang and other gangs (as subdevices):

 this.log('Wall switch on/off received', parsedValue);

          try {
              await this.setCapabilityValue('onoff', parsedValue);
          } catch (e) {
              this.log("Failed to set on/off", e);
          }

The issue is that when it listens (with the above code you provided for “response”) and it matches the response, it ends up setting all gang switches to on. Do you have any idea how I can only set a particular gang / sub-device on or off?

When using homey app buttons, I’m using this code to control sub-gangs / sub-devices. For example, when second gang button hit on app, second gang on device physically turns on and off. There is a corresponding secondGang config in driver.compose.json

else if (subDeviceId === 'secondGang') {
    this.registerCapabilityListener('onoff', async value => {
      this.log('onoff gang 2:', value);
      await this.writeBool(2, value)
      .catch(err => {
        this.error('Error when writing to device: ', err);
      });
    });

Data being received by homey is similar (this is for off on second gang):

  zigbee-clusters:cluster ep: 1, cl: tuya (61184) received frame response tuya.response {
  status: 0,
  transid: 105,
  dp: 2,
  datatype: 1,
  length: 1,
  data: <Buffer 00>

It looks like the response data contains a property dp that contains the “sub-device ID”, so you have to somehow check if this is a subdevice and if so, if it matches the value of dp.

Hello,

I have something similar to code.
But I am not successful :
I write :


That seems correct, but I got this message when I press the button:

What could I do ?

Regards

@GD91 might be better that you open up a new thread with your own issue and maybe put a link to this thread. This will allow your issue to get resolved more efficiently and allow us to close this thread once the original issue has been resolved :slight_smile:

Marked as solution for 1 Gang Wall Switch.

I’ve pushed my version of code as a response to my own issue (as a PR): Zigbee Tuya 1 Gang Wall Switch (TS0601/_TZE200_gbagoilo) by shahzadsk · Pull Request #708 · JohanBendz/com.tuya.zigbee · GitHub

@johan_bendz

Thanks again @robertklep - FYI I will likely start another topic for the 5 gang switch, as I’m having trouble with the sub-device.