Skip to content

Instantly share code, notes, and snippets.

@schauveau
Last active April 14, 2024 11:31
Show Gist options
  • Save schauveau/08fca627b37edf3ffdc5c3d7478b0897 to your computer and use it in GitHub Desktop.
Save schauveau/08fca627b37edf3ffdc5c3d7478b0897 to your computer and use it in GitHub Desktop.
TS0601_air_filter external converter for Z2M with cache
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 utils = require('zigbee-herdsman-converters/lib/utils');
const legacy = require('zigbee-herdsman-converters/lib/legacy');
const e = exposes.presets;
const ea = exposes.access;
const STARTUP_DELAY = 4.0 ; // how much to wait before reporting for the first time
const PUBLISH_DELAY = 10.0 ; // And the delay before
const legacy_tuya_air_quality_with_cache = {
cluster: 'manuSpecificTuya',
type: ['commandDataReport', 'commandDataResponse'],
convert: (model, msg, publish, options, meta) => {
// return legacy.fromZigbee.tuya_air_quality.convert(model, msg, publish, options, meta);
const now = Date.now(); // current time in ms
if ( ! ('_priv' in meta.device) ) {
// Initialize the cache at startup
meta.device.private_cache = {} ;
meta.device.private_next_update = now + STARTUP_DELAY*1000;
}
const changes = legacy.fromZigbee.tuya_air_quality.convert(model, msg, publish, options, meta);
Object.assign( meta.device.private_cache, changes );
if ( now >= meta.device.private_next_update ) {
// Every PUBLISH_DELAY seconds, publish the attributes accumulated in the cache.
const result = meta.device._cache;
meta.device.private_next_update = now + PUBLISH_DELAY*1000;
meta.device.private_cache = {};
return result;
} else {
return {} ;
}
}
};
const definition =
{
fingerprint: [{ modelID: 'TS0601', manufacturerName: '_TZE200_8ygsuhe1' },
{ modelID: 'TS0601', manufacturerName: '_TZE200_yvx5lh6k' },
{ modelID: 'TS0601', manufacturerName: '_TZE200_ryfmq5rl' },
{ modelID: 'TS0601', manufacturerName: '_TZE200_c2fmom5z' },
{ modelID: 'TS0601', manufacturerName: '_TZE200_mja3fuja' }],
model: 'TS0601_air_quality_sensor',
vendor: 'TuYa',
description: 'Air quality sensor (WITH DELAYED OUTPUT)',
// fromZigbee: [legacy.fromZigbee.tuya_air_quality],
fromZigbee: [ legacy_tuya_air_quality_with_cache ],
toZigbee: [],
exposes: [e.temperature(), e.humidity(), e.co2(), e.voc().withUnit('ppm'), e.formaldehyd()],
} ;
module.exports = definition;

This is my attempt to solve Koenkk/zigbee2mqtt#8154

I do not have the device but it should not be that hard to cache all attribute and publish them together after a delay.

I was not able to test the converter but it compiles and it it successfully loaded on my end.

I will keep the latest version in TS0601_air_filter.js below.

HOW TO INSTALL:

  • Drop the file TS0601_air_filter.js in the directory containing configuration.yaml (e.g. /opt/zigbee2mqtt/data/)
  • Add the external converter either in the Z2M web frontend or in configuration.yaml
external_converters:
  - TS0601_air_filter.js
  • (OPTIONAL) Check that the converter compiles successfully by running node TS0601_air_filter.js. That only works for files in the Z2M directory.
  • Restart Z2M.
  • If the device description now read Air quality sensor (WITH DELAYED OUTPUT) then the external converter was successully loaded.
  • Otherwise, the default converter was used. The log file probably contains some error messages. Please post them below.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment