// Converter for ZM-RT201 ZM-35ZH-Q with battery now. https://www.youtube.com/channel/UCJRaKbgv8u1O4OwG-0D3uRw 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 globalStore = require('zigbee-herdsman-converters/lib/store'); const e = exposes.presets; const ea = exposes.access; const { setTimeout, clearTimeout } = require("timers"); const motionSensorDetector = { cluster: 'ssIasZone', type: ['attributeReport'], convert: (model, msg, publish, options, meta) => { if (msg.data.zoneStatus !== 1) { // In case of 0 no occupancy is reported. // https://github.com/Koenkk/zigbee2mqtt/issues/467 return; } // The occupancy sensor only sends a message when motion detected. // Therefore we need to publish the no_motion detected by ourselves. const timeout = options && options.hasOwnProperty('occupancy_timeout') ? options.occupancy_timeout : 20; // occupancy delay in seconds // Stop existing timers because motion is detected and set a new one. globalStore.getValue(msg.endpoint, 'timers', []).forEach((t) => clearTimeout(t)); globalStore.putValue(msg.endpoint, 'timers', []); if (timeout !== 0) { const timer = setTimeout(() => { publish({occupancy: false}); }, timeout * 1000); globalStore.getValue(msg.endpoint, 'timers').push(timer); } // No occupancy since if (options && options.no_occupancy_since) { options.no_occupancy_since.forEach((since) => { const timer = setTimeout(() => { publish({no_occupancy_since: since}); }, since * 1000); globalStore.getValue(msg.endpoint, 'timers').push(timer); }); } if (options && options.no_occupancy_since) { return {occupancy: true, no_occupancy_since: 0}; } else { return {occupancy: true}; } } }; const definition = { zigbeeModel: ['TS0202', '_TYZB01_dl7cejts'], model: 'ZM-RT201', vendor: 'TuYa', description: 'Motion sensor (Custom driver version)', fromZigbee: [ motionSensorDetector, fz.battery ], //fromZigbee: [ motionSensorDetector, fz.battery, fz.ignore_basic_report ], toZigbee: [], exposes: [e.occupancy(), e.battery_low(), e.battery()], }; module.exports = definition;