Skip to content

Instantly share code, notes, and snippets.

@Koenkk
Last active April 14, 2023 10:20
Show Gist options
  • Save Koenkk/86ea747a683752fd6ff9390abf9fda55 to your computer and use it in GitHub Desktop.
Save Koenkk/86ea747a683752fd6ff9390abf9fda55 to your computer and use it in GitHub Desktop.
const fz = require('zigbee-herdsman-converters/converters/fromZigbee');
const tz = require('zigbee-herdsman-converters/converters/toZigbee');
const exposes = require('zigbee-herdsman-converters/lib/exposes');
const reporting = require('zigbee-herdsman-converters/lib/reporting');
const extend = require('zigbee-herdsman-converters/lib/extend');
const ota = require('zigbee-herdsman-converters/lib/ota');
const tuya = require('zigbee-herdsman-converters/lib/tuya');
const utils = require('zigbee-herdsman-converters/lib/utils');
const e = exposes.presets;
const ea = exposes.access;
const definition = {
fingerprint: tuya.fingerprint('TS0601', ['_TZE200_yojqa8xn']),
model: 'TS0601_gas_sensor',
vendor: 'TuYa',
description: 'Gas sensor',
fromZigbee: [tuya.fzDataPoints],
toZigbee: [tuya.tzDataPoints],
configure: tuya.configureMagicPacket,
exposes: [e.gas()],
meta: {
tuyaDatapoints: [
[1, 'gas', tuya.valueConverter.true0ElseFalse],
[2, 'detected_gas', tuya.valueConverter.raw],
[6, 'alarm_ringtone', tuya.valueConverter.raw],
[7, 'alarm_time', tuya.valueConverter.raw],
[8, 'auto_detect', tuya.valueConverter.raw],
[9, 'auto_detect_result', tuya.valueConverter.raw],
[11, 'preheat', tuya.valueConverter.raw],
],
},
};
module.exports = definition;
@Masterz69
Copy link

Masterz69 commented Nov 30, 2022

[1, 'gas', tuya.valueConverter.true0ElseFalse],
^^ as per my tests this is not working, because dp:1 have type:4 (enum).

Got in log:

debug 2022-11-30 19:01:53: Received Zigbee message from 'sGas Boiler', type 'commandDataReport', cluster 'manuSpecificTuya', data '{"dpValues":[{"data":{"data":[1],"type":"Buffer"},"datatype":4,"dp":1}],"seq":65497}' from endpoint 1 with groupID 0
error 2022-11-30 19:01:53: Exception while calling fromZigbee converter: Cannot read properties of undefined (reading 'from')}
debug 2022-11-30 19:01:53: TypeError: Cannot read properties of undefined (reading 'from')
    at Object.convert (/app/node_modules/zigbee-herdsman-converters/lib/tuya.js:1430:53)
    at Receive.onDeviceMessage (/app/lib/extension/receive.ts:143:51)
    at EventEmitter.emit (node:events:525:35)
    at EventBus.emitDeviceMessage (/app/lib/eventBus.ts:102:22)
    at Controller.<anonymous> (/app/lib/zigbee.ts:106:27)
    at Controller.emit (node:events:513:28)
    at Controller.selfAndDeviceEmit (/app/node_modules/zigbee-herdsman/src/controller/controller.ts:515:14)
    at Controller.onZclOrRawData (/app/node_modules/zigbee-herdsman/src/controller/controller.ts:726:18)
    at ZStackAdapter.<anonymous> (/app/node_modules/zigbee-herdsman/src/controller/controller.ts:144:70)
    at ZStackAdapter.emit (node:events:513:28)

Got it working with combination only:
exposes.binary('gas', ea.STATE, 'detected', 'not detected').withDescription('Indicates whether the device detected gas'),
&
[1, 'gas', tuya.valueConverterBasic.lookup({'detected': tuya.enum(0),'not detected': tuya.enum(1)})],

P.S.

info  2022-11-30 19:01:19: Starting Zigbee2MQTT version 1.28.2 (commit #unknown)
info  2022-11-30 19:01:19: Starting zigbee-herdsman (0.14.68)

@Masterz69
Copy link

Have following expose & entry in map for DP 13 in my external converter:

    exposes: [
            exposes.binary('alarm_switch', ea.STATE_SET, true, false), ...
    ],
    meta: {
        tuyaDatapoints: [
            [13, 'alarm_switch', tuya.valueConverter.raw], ...
        ],
    ],

Unfortunately Z2M show "No converter available for 'alarm_switch (true)'"
As far I see that happens because tuyaTz.datapoints have strict set of keys it can process.....

tuyaFz.datapoints parse data for any DP defined in meta map.
IMHO would be nice toZigbee to process any key defined in meta map.

@Koenkk
Copy link
Author

Koenkk commented Nov 30, 2022

Thanks for looking into this, could you make a PR?

@Masterz69
Copy link

My current version for converter is:

const fz = require('zigbee-herdsman-converters/converters/fromZigbee');
const tz = require('zigbee-herdsman-converters/converters/toZigbee');
const exposes = require('zigbee-herdsman-converters/lib/exposes');
const reporting = require('zigbee-herdsman-converters/lib/reporting');
const extend = require('zigbee-herdsman-converters/lib/extend');
const e = exposes.presets;
const ea = exposes.access;
const tuya = require('zigbee-herdsman-converters/lib/tuya');

const definition = {
	fingerprint: [{modelID: 'TS0601', manufacturerName: '_TZE200_yojqa8xn'}],
	model: 'TS0601_gas_sensor',
    vendor: 'TuYa',
    description: 'Natural gas sensor',
    fromZigbee: [tuya.fzDataPoints],
    toZigbee: [tuya.tzDataPoints],
    configure: tuya.configureMagicPacket,
    exposes: [
	    exposes.binary('gas', ea.STATE, 'detected', 'not detected').withDescription('Indicates whether the device detected gas'),
        exposes.numeric('gas_value', ea.STATE).withDescription('Measured gas concentration').withUnit('LEL'), 
	    exposes.enum('alarm_ringtone', ea.STATE_SET, ['RingTone 1', 'RingTone 2', 'RingTone 3', 'RingTone 4', 'RingTone 5'])
		    .withDescription('Alarm RingTone'),
	    exposes.numeric('alarm_time', ea.STATE_SET).withValueMin(1).withValueMax(180).withValueStep(1)
            .withUnit('s').withDescription('Alarm time'),
        tuya.exposes.selfTest(), tuya.exposes.selfTestResult(), 
        exposes.binary('preheat', ea.STATE, true, false), 
		exposes.binary('alarm_switch', ea.STATE_SET, true, false),
		tuya.exposes.silence()
    ],
    meta: {
        tuyaDatapoints: [
            [1, 'gas', tuya.valueConverterBasic.lookup({'detected': tuya.enum(0),'not detected': tuya.enum(1)})],
            [2, 'gas_value', tuya.valueConverter.divideBy10],
            [6, 'alarm_ringtone', tuya.valueConverterBasic.lookup
			    ({'RingTone 1': 0, 'RingTone 2': 1, 'RingTone 3': 2, 'RingTone 4': 3, 'RingTone 5': 4})],
            [7, 'alarm_time', tuya.valueConverter.raw],
            [8, 'self_test', tuya.valueConverter.raw],
            [9, 'self_test_result', tuya.valueConverter.selfTestResult],
            [10, 'preheat', tuya.valueConverter.raw],
            [13, 'alarm_switch', tuya.valueConverter.raw],
            [16, 'silence', tuya.valueConverter.raw],
        ],
    },
};

module.exports = definition;

BUT, I have an issue f.e. with DP 13 'alarm_switch'.
When trying to send command , got in log:

debug 2022-12-01 21:45:53: Received MQTT message on 'zigbee2mqtt/sGas Boiler/set' with data '{"alarm_switch":true}'
error 2022-12-01 21:45:53: No converter available for 'alarm_switch' (true)

Tried to find where (in code) this error generated, but can't find.
Looks I can't use any other key not included in tuya.tuyaTz.datapoints.key !?!?!

@Koenkk
Copy link
Author

Koenkk commented Dec 2, 2022

I've created the pr, please check the comments: Koenkk/zigbee-herdsman-converters#5065

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment