Koenkk / zigbee2mqtt

Zigbee 🐝 to MQTT bridge 🌉, get rid of your proprietary Zigbee bridges 🔨
https://www.zigbee2mqtt.io
GNU General Public License v3.0
11.88k stars 1.66k forks source link

[New device support]: Motion Sensor TS0202, _TZ3000_lf56vpxj #19105

Closed stefanstidlffg closed 1 year ago

stefanstidlffg commented 1 year ago

Link

https://aliexpress.com/item/1005006010327384.html

Database entry

{"id":11,"type":"EndDevice","ieeeAddr":"0xa4c1384fd508fc30","nwkAddr":23461,"manufId":4417,"manufName":"_TZ3000_lf56vpxj","powerSource":"Battery","modelId":"TS0202","epList":[1],"endpoints":{"1":{"profId":260,"epId":1,"devId":1026,"inClusterList":[1,1280,3,0],"outClusterList":[4096,6,25,10],"clusters":{"genBasic":{"attributes":{"65506":51,"65508":1,"65534":0,"stackVersion":0,"dateCode":"","manufacturerName":"_TZ3000_lf56vpxj","zclVersion":3,"appVersion":70,"modelId":"TS0202","powerSource":3}},"ssIasZone":{"attributes":{"61441":1,"iasCieAddr":"0xe0798dfffea84200","zoneState":1,"currentZoneSensitivityLevel":1}},"genPowerCfg":{"attributes":{"batteryPercentageRemaining":200,"batteryVoltage":30}}},"binds":[{"cluster":1,"type":"endpoint","deviceIeeeAddress":"0xe0798dfffea84200","endpointID":1}],"configuredReportings":[{"cluster":1,"attrId":33,"minRepIntval":3600,"maxRepIntval":62000,"repChange":0,"manufacturerCode":null}],"meta":{}}},"appVersion":70,"stackVersion":0,"hwVersion":1,"dateCode":"","zclVersion":3,"interviewCompleted":true,"meta":{"configured":-1692722804},"lastSeen":1695904425465,"defaultSendRequestWhen":"immediate"}

Comments

The square version of the motion detector with 2 AAA Batteries

image

thank you for adding!

External converter

const definition = {
    fingerprint: [
        {modelID: 'TS0202', manufacturerName: '_TZ3000_lf56vpxj'},
        ],
    model: 'TS0202',
    vendor: 'TuYa',
    description: 'Motion sensor',
    fromZigbee: [fz.ias_occupancy_alarm_1, fz.battery, fz.ignore_basic_report, fz.ias_occupancy_alarm_1_report],
    toZigbee: [],
    exposes: [e.occupancy(), e.battery_low(), e.battery(), e.battery_voltage()],
    configure: async (device, coordinatorEndpoint, logger) => {
        const endpoint = device.getEndpoint(1);
        await reporting.bind(endpoint, coordinatorEndpoint, ['genPowerCfg']);
        try {
            await reporting.batteryPercentageRemaining(endpoint);
        } catch (error) {/* Fails for some https://github.com/Koenkk/zigbee2mqtt/issues/13708*/}
    },
};

Supported color modes

No response

Color temperature range

No response

serot23 commented 1 year ago

Hi i tried adding the code you provided as external converter and changed some stuff in my database file, but it still doesn't recognize the device, do you have any suggestions?

sstidl commented 1 year ago

I shortened the script, sorry!

Try to put this

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: [
        {modelID: 'TS0202', manufacturerName: '_TZ3000_lf56vpxj'},
        ],
    model: 'ZP01',
    vendor: 'TuYa',
    description: 'Motion sensor',
    fromZigbee: [fz.ias_occupancy_alarm_1, fz.battery, fz.ignore_basic_report, fz.ias_occupancy_alarm_1_report],
    toZigbee: [],
    exposes: [e.occupancy(), e.battery_low(), e.battery(), e.battery_voltage()],
    configure: async (device, coordinatorEndpoint, logger) => {
        const endpoint = device.getEndpoint(1);
        await reporting.bind(endpoint, coordinatorEndpoint, ['genPowerCfg']);
        try {
            await reporting.batteryPercentageRemaining(endpoint);
        } catch (error) {/* Fails for some https://github.com/Koenkk/zigbee2mqtt/issues/13708*/}
    },
};

module.exports = definition;

in the data directory and add it in configuration.yaml:

external_converters:
  - my-motion.js
serot23 commented 1 year ago

Thank you, this worked!

Dreyk007 commented 1 year ago

Thank you! It helped me too

Koenkk commented 1 year ago

Device will be detected correctly in todays release!

Suxsem commented 11 months ago

@stefanstidlffg I bought this exactly same sensor and it's driving me crazy. often it remain stuck in the "occupied" state also if there are no more people in the room for hours. Sometimes, when it's in the clear state and someone briefly pass in front of the sensor, the red light blinks once but no update it's triggered on Z2M (and no logs either). The coordinator it's in the room next to it so it has very good signal. Are you experiencing the same issues?

Dreyk007 commented 11 months ago

@Suxsem same issue for me... Have 2 sensors like this. Planning to change to another model.

Suxsem commented 11 months ago

too bad. I think this could be an hardware problem :( any suggestion for another cheap but reliable model from aliexpress?

stefanstidlffg commented 11 months ago

@stefanstidlffg I bought this exactly same sensor and it's driving me crazy. often it remain stuck in the "occupied" state also if there are no more people in the room for hours. Sometimes, when it's in the clear state and someone briefly pass in front of the sensor, the red light blinks once but no update it's triggered on Z2M (and no logs either). The coordinator it's in the room next to it so it has very good signal. Are you experiencing the same issues?

Mine is working okay. Sorry to hear that yours is defective.

Suxsem commented 11 months ago

I don't think it's detective. Try to very quickly move in front of the sensor (maybe just wave your hand while hiding your body). Sometimes (like 50% of the time) the red light blinks but the state is not updated. Can you please try that?

serot23 commented 11 months ago

I have also 2 of them and have no problems with them. If i wave my hand in front of it, the red light blinks and i get motion detected. Maybe delete and try to re add them?

Dreyk007 commented 11 months ago

In my case, the problem looks like the sensor is working, but for some reason the signal is not reaching the coordinator. Maybe unstable operation with a certain coordinator model or selected zigbee channel?

I am using "Sonoff Zigbee 3.0 USB Dongle Plus ZBDongle-E" with 25 channel.

@serot23 @Suxsem @stefanstidlffg What coordinator and channel do you use?

serot23 commented 11 months ago

I'm using a sonoff zigbee zbdongle P, this one. https://www.amazon.nl/dp/B09KZX4WSB?ref_=pe_28126711_487767311_302_E_DDE_dt_1

I also have a router(main powered zigbee device) in all my rooms, i use channel 15, so my konke smoke detectors work. I believe it is always recommend to use, 15 to 22 range if I'm not mistaken

stefanstidlffg commented 11 months ago

I think I have the same coordinator as you write. SONOFF Zigbee 3.0 USB Dongle Plus V2

Coordinator type EZSP v8 Coordinator revision 7.0.2.0 build 406

Dreyk007 commented 11 months ago

My dongle is based on EFR32MG21 Also with 7.0.2.0 firmware.

maybe the problem really is the channel...

Suxsem commented 11 months ago

Umh, the issue never happens when I show my full body in front of the sensor, only when I briefly show a small part of it, so I don't think it's radio related. This is my guess: the module wakes up when motion is detected and blinks the red led; then it reads the sensor status and send the zigbee message. If the the object is removed while the module is still booting, then when the sensor status is read it seems that no object is there. But this is just my guess.

Can I ask you a question? Did you first connected the module through the tuya hub and maybe the firmware got updated? Can you tell me your module firmware version please?

Anyway I found that the MCU is tuya ZTU, aka telink 8258. I reversed engineered the pinout (basically one wire for the led, one wire for the sensor and one wire for the button) and found the telink SDK. I think I'll try to write a custom firmware (flashing is done using the exposed SWS pin), it may be useful because I think a lot of tuya zigbee devices are based on this MCU and the principle is the same for most of the sensors (motion, light, contact, switch, ecc...)

Suxsem commented 11 months ago

My "currentFileVersion" is 70

Dreyk007 commented 11 months ago

I have not tuya hub. FW was never updated. Don't know how to check FW version of device

serot23 commented 11 months ago

I have a Tuya zigbee gateway, but i am not using it. It is in a box somewhere. I also don't know how to check the firmware without using the tuya app. I just installed it first to the code the OP had and when the update came with the device already in the code, i deleted the files, deleted the device, restarted and added the device again

Suxsem commented 11 months ago

@Dreyk007 @serot23

just go to the DEV CONSOLE tab and select cluster OTA and attribute currentFileVersion. Then activate the sensor (maybe remove and reinstert the battery) and immediately please READ. Thank you!

image

Dreyk007 commented 11 months ago

@Suxsem thanks for the explanation. Unfortunately my sensors can't be dismantled quickly yet. Without this it is not possible to read the attribute. When I replace the glitchy sensor, I will try again.

serot23 commented 11 months ago

@Suxsem Thanks, learned something. I've currentfileversion 70

Suxsem commented 10 months ago

Custom firmware done! The device is working flawlessly now. Source code is available at https://github.com/Suxsem/TS0202-PIR-Sensor-Zigbee-firmware but I do not plan to provide instruction on how to compile and flash because it's fairly involved and the firmware itself it's almost completely untested so I do not feel confident to do so. Maybe in a month or so when I had the chance to properly test it. Thank you for the discussion, regards!

Dreyk007 commented 10 months ago

but I do not plan to provide instruction on how to compile and flash

Thanks, but it's a little sad that there are no instructions. It would be nice if there were instructions on how to compile and the necessary dependencies for this. Also where to solder to for flashing. I haven't disassembled this device so I don't know what's inside, there are probably UART pins out there?

serot23 commented 10 months ago

I'm interested in your results, i hadn't have any bad readings so far and i have 3 of those things and today came the 4th one. And i just randomly ordered it from different sellers.

Suxsem commented 10 months ago

I'm interested in your results, i hadn't have any bad readings so far and i have 3 of those things and today came the 4th one. And i just randomly ordered it from different sellers.

While I was developing my firmware for the sensor, I also changed my zigbee network configuration changing the firmware of the zigbee dongle from RCP to NCP. So I cannot say for sure if that was enough to resolve the initial issue (that was: sometimes, when there was a double SUDDEN change in occupancy status, such as showing in front of the sensor JUST after it came back to clear, the value stay to clear instead of occupied). Nevertheless, I'm more confident in my firmware (I'm using the sensor for the home alarm system, so a missing reading is unacceptable) simply because I know the exact code that runs on it.

but I do not plan to provide instruction on how to compile and flash

Thanks, but it's a little sad that there are no instructions. It would be nice if there were instructions on how to compile and the necessary dependencies for this. Also where to solder to for flashing. I haven't disassembled this device so I don't know what's inside, there are probably UART pins out there?

In the repository there is a link to the main repository I based my work on; inside it you can find various documentation for building and flashing. I also found a convenient method to flash the firmware using just an USB-UART converter, you can find more information here: https://github.com/Ai-Thinker-Open/TBXX_Flash_Tool. With this method problem is that backupping the original firmware is not yet supported (there is a bug in the read flash implementation). Without backupping the original firmware I do not feel confident providing detailed information on how to overwrite it. I think I know how to fix the read flash issue. If I can do that, are you available to dump your original firmware and share it to me? I didn't do that and I would like to compare the performance of my firmware against the original one. A USB-UART converter and a couple of 470ohm resistors are needed (and a soldering iron of course, but soldering is very very simple due to the provided pads on the PCB).

Dreyk007 commented 10 months ago

If I can do that, are you available to dump your original firmware and share it to me? I didn't do that and I would like to compare the performance of my firmware against the original one. A USB-UART converter and a couple of 470ohm resistors are needed (and a soldering iron of course, but soldering is very very simple due to the provided pads on the PCB).

I think I could help with that. The only thing is, it would be within a month. Since I need to free up enough time to replace the sensor with another one, and get this one opened up and prepped. Let me know when the dump script is ready.

Suxsem commented 10 months ago

@Dreyk007, sure I can wait, thank you! I'll let you know when the script is ready (hope in week or so)

Suxsem commented 10 months ago

@Dreyk007 done! In order to not spam this thread anymore I created a new issue here https://github.com/Suxsem/TS0202-PIR-Sensor-Zigbee-firmware/issues/1 with the instructions for dumping the original tuya firmware. Thank you for the collaboration!

Suxsem commented 9 months ago

So... After a couple of weeks of comparison between my firmware and original one, I can confirm that the original firmware SOMETIMES get stuck in the occupied state indefinitely (until someone pass in front of the sensor again), while this never occurred to me with my modified firmware. Stability is good, I have run my modified firmware for two weeks without any issue. Battery is always 100% so power consumption should be equal to the original one (I will try to make addition tests on the battery). I think I'm going to publish the compiled version of my firmware, I feel confident enough to have other people using it. Thank you for all the support!

TaylorTWBrown commented 9 months ago

Thank you so much for your work on making this more stable. I'm thinking of picking one up since they're only $5 CAD. I'm curious: why isn't OTA flashing practical? Can't bin files be applied by Zigbee2Mqtt?

Suxsem commented 9 months ago

@TaylorTWBrown i don't think that the original firmware implemented OTA updates... (i will investigate). Nevertheless my custom firmware has different "manufacturer" and "model" strings so without further changes OTA update from stock firmware would definitely fail