Skip to content

Conversation

@chrisf4lc0n
Copy link

@chrisf4lc0n chrisf4lc0n commented Nov 27, 2025

This quirk adds support for the Tuya PJ-1203 single channel clamp power meter.
The device reports voltage, current, power and energy used. Apparent power and power factor are calculated from these values.

Manufacturer IDs: _TZE204_cjbofhxw, _TZE284_cjbofhxw
Model: TS0601
Purchase link: https://www.aliexpress.com/item/1005005994777032.html

Datapoints:
DP 18: Current (mA)
DP 19: Power (W × 10)
DP 20: Voltage (V × 10)
DP 101: Energy (Wh)

Exposed entities:
Voltage (V)
Current (A)
Power (W)
Apparent Power (VA) - calculated
Power Factor (%) - calculated
Energy (kWh)

Proposed change:
Add a new quirk for the Tuya PJ-1203 single channel clamp energy meter that maps Tuya datapoints to ZHA ElectricalMeasurement cluster attributes and calculates apparent power and power factor from the reported values.

Additional information:
This is a single channel variant; dual channel PJ-1203A models use different datapoints and would need a separate quirk
The device does not report AC frequency, so that entity will remain unavailable
Tested and working with ZHA on Home Assistant 2025.11.2

Device diagnostics
zha-01KAEDWDH9AYZDVE1HTD8MCW6X-_TZE284_cjbofhxw TS0601-6850c3f422189896ca2536b313d6e7fb.json

Checklist:
The changes are tested and work correctly
pre-commit checks pass / the code has been formatted using Black
Tests have been added to verify that the new code works
Device diagnostics data has been attached

After pulling enough logs and comparing the energy used vs power I found a dip in cumulative energy consumption.
To alleviate that an implementation of energy offset restoration from ZHA's attribute cache to maintain
energy accumulation continuity across quirk reloads/restarts was introduced. When the device
counter is lower than the cached value, the offset is restored to prevent energy loss.

I have monitored the data over 24h period and I am happy to report that there is no longer dips in cumulative energy readings, please find the attached history file. The initial dip around 11:55hrs on the 26/11/25 was from the previous version of the quirk, so it is possible to observe that the fix had really worked.
energy.csv

There are still edge cases when stale cache from crash, multiple resets during offline period or cache loss and a few more even more obscure cases can lead to some data loss and dips in energy used but it is still far better than the original implementation, which was all over the place.

I have made a proper mess with the previous commit so apologies for that! I am still trying to learn how to work with LLMs to help me with the coding tasks.

Screenshot from 2025-11-27 16-26-21 Screenshot from 2025-11-27 16-26-01

This quirk supports the Tuya PJ-1203 single channel clamp power meter, providing accurate energy measurements and handling resets.
This file contains comprehensive tests for the Tuya PJ-1203 Single Channel Clamp Energy Meter quirk, covering various functionalities such as attribute updates, cluster constants, and energy integration features.
@codecov
Copy link

codecov bot commented Nov 27, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.36%. Comparing base (35d1fc1) to head (0208288).

Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #4529      +/-   ##
==========================================
+ Coverage   92.28%   92.36%   +0.08%     
==========================================
  Files         371      372       +1     
  Lines       12158    12292     +134     
==========================================
+ Hits        11220    11354     +134     
  Misses        938      938              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Add tests to achieve 100% code coverage for the PJ-1203 quirk:
- Non-energy attribute updates on metering cluster
- get_compensated_energy_wh() when no device energy received
- Reading metering attributes by string name
- Reading summation_formatting constant (bitmap8 type)
- Reading unit_of_measure constant (enum8 type)
- Reading unsupported metering attributes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant