Skip to content

Conversation

jeverley
Copy link
Contributor

@jeverley jeverley commented Feb 1, 2024

Proposed change

Adds support for Tuya Energy Meter devices in 1 and 2 clamp configurations.

Uses zigbee2Mqtt https://github.com/Koenkk/zigbee-herdsman-converters/blob/master/src/devices/tuya.ts as reference for device datapoints (with amendments from community testing in the respective device support issues).

Meter devices providing power direction will report signed values for their supported attributes in accordance with the Zigbee cluster spec (instantaneous demand, active power and reactive power specifically).

Provides control over metering sensor configuration:

  • The quirk allows the user to control the endpoint 3 power calculation on 2 clamp devices using the "channel_configuration" attribute value (calculated as either a+b or a-b).
    • a+b should be used when measuring total consumption from two power sources.
    • a-b should be used to derive a second power value from input power and consumption i.e. clamp A on grid input, clamp B measuring power into the consumer unit.

Some devices report an a+b total natively, however in testing this value is often incorrect due to delay in detecting power direction change on the device.

In the real world when measuring both grid and production an a + b < 0 value would be atypical.
This is mitigated using the power_flow_preempt filter on device readings, this is optionally is enabled by setting 'power_flow_preempt' to True.

The quirk aims to make adding new devices simpler by utilising device specific dp_to_attribute mappings which feed into common core reporting clusters.

image

Addresses device support requests:

2 bidirectional clamps:

1 bidirectional clamp:

1 clamp:

@jmuf I've just spotted that in parallel you've also been working on #2870 for _TZE200_rks0sgb7, shall we pool ideas?

Additional information

Associated with merged pre-requisite ZHA metering and electrical measurement changes:

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

Aims to address device support requests:

2 bidirectional clamps:
zigpy#2549 (_TZE204_81yrt3lo)
zigpy#2650 (_TZE200_rks0sgb7)

1 bidirectional clamp:
zigpy#2420 (_TZE204_ac0fhfiq)

1 clamp:
zigpy#1973 (_TZE204_cjbofhxw)

@jmuf I've just spotted that in parallel you've also been working on zigpy#2870 for 
_TZE200_rks0sgb7, shall we pool ideas?
jeverley and others added 3 commits February 1, 2024 09:33
Copy link

codecov bot commented Feb 1, 2024

Codecov Report

Attention: Patch coverage is 52.88809% with 261 lines in your changes missing coverage. Please review.

Project coverage is 88.66%. Comparing base (d3d43b1) to head (07a600e).
Report is 18 commits behind head on dev.

Files with missing lines Patch % Lines
zhaquirks/tuya/ts0601_energy_meter.py 52.88% 261 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #2961      +/-   ##
==========================================
- Coverage   90.52%   88.66%   -1.87%     
==========================================
  Files         324      325       +1     
  Lines       10631    11185     +554     
==========================================
+ Hits         9624     9917     +293     
- Misses       1007     1268     +261     

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

@jmuf
Copy link

jmuf commented Feb 2, 2024

@jmuf I've just spotted that in parallel you've also been working on #2870 for _TZE200_rks0sgb7, shall we pool ideas?

sure, tell me what I should do. Your "quirk" looks more complete, my "quirk" is a one-off (and "my first quirk".. ).

  • I had to resort to several "replacement clusters" to get the 3x same set of metrics (and buses to route data from the TUYA cluster to go there), that "feels wrong"
  • also have still an issue with naming (all 3 measurements have same user-friendly name, didn't find way to label as Clamp1/Clamp2/Total)

Things I like in "mine":

  • using the constant from ElectricalMeasurement, Metering (instead of random HEX values in the quirk)
  • my quirk (or my meter?) seems to happily report negative values, so your complicated configuration (a+b / a-b) seems not required (also a user could just turn around the clamp to reverse the reading?)
  • double-digit precision on "Watt" sounds not what these meters are made for. Mine says ±2W <100W, else ±2% accuracy

@jeverley
Copy link
Contributor Author

jeverley commented Feb 3, 2024

sure, tell me what I should do. Your "quirk" looks more complete, my "quirk" is a one-off (and "my first quirk".. ).

  • I had to resort to several "replacement clusters" to get the 3x same set of metrics (and buses to route data from the TUYA cluster to go there), that "feels wrong"
  • also have still an issue with naming (all 3 measurements have same user-friendly name, didn't find way to label as Clamp1/Clamp2/Total)

Things I like in "mine":

  • using the constant from ElectricalMeasurement, Metering (instead of random HEX values in the quirk)
  • my quirk (or my meter?) seems to happily report negative values, so your complicated configuration (a+b / a-b) seems not required (also a user could just turn around the clamp to reverse the reading?)
  • double-digit precision on "Watt" sounds not what these meters are made for. Mine says ±2W <100W, else ±2% accuracy

Appreciate the feedback @jmuf , I've started to incorporate some changes based on your thoughts.

Changes so far:

  • Switched to contestants for the attribute IDs
  • Amend power divisor to report 1 decimal place (one of the meters reports that level of precision)

Outstanding:

  • also have still an issue with naming (all 3 measurements have same user-friendly name, didn't find way to label as Clamp1/Clamp2/Total)
    I like the idea, looking at the core integration code I think we need to make some amendments to ZHA's sensor functionality to support this.
  • my quirk (or my meter?) seems to happily report negative values, so your complicated configuration (a+b / a-b) seems not required (also a user could just turn around the clamp to reverse the reading?)
    • Ah that's useful to know yours reports the signed integers natively.
      The other devices report power as an unsigned integer, so we need to apply the direction locally. I can make that logic only apply for the uint devices.
    • On the add/subtract point, that's a fair challenge, it does add additional complexity.
      I still think there's benefit in a-b for non-bidirectional 2 clamp devices because that would be the only way of deriving export to the grid (i.e. consumption minus solar, since both should clamps should always be positive).

@Xesc1714
Copy link

Hi,
Some ZHA quirks to setup and work with this device TZ3000_zrm3oxsh TS011F
image
thank you

@pantonvich
Copy link

Hey thanks for this quirk! I have the _TZE204_81yrt3lo but with only one clamp - some how I managed to order it with only one clamp - lol. My question is that there doesn't appear to be a difference between the power and instantaneous demand so why are both needed? I set mine up on the A and did have to flip my clamp to not get negative watts.

@Vladdy65
Copy link

Thanks @Vladdy65, do you definitely have the arrow inside the clamps facing in the direction of delivered power, and the red cables of the clamps connected to their corresponding S1 terminal on the module?

@jeverley double checked the wiring. Also made sure I had the LIVE cables (brown in UK).
To confirm, I followed this guidance:
image
In my case the arrow is inside the clamp, K->L, as in the image inset, and is pointing to the loads as follows:
image

Wiring:
image

@jeverley
Copy link
Contributor Author

Appreciate the thoroughness, if you flip the orientation of the clamps does the flow direction change?

@reef-actor
Copy link

I have a different meter model with a single clamp (CT-2101A) that is labelled as "S1.Black, S2.Red", does your manual say the other way round? I have tried looking up how these clamps work and I am not sure they would do anything at all if they were the wrong way round, but I thought I would mention it.

@Vladdy65
Copy link

I have a different meter model with a single clamp (CT-2101A) that is labelled as "S1.Black, S2.Red", does your manual say the other way round? I have tried looking up how these clamps work and I am not sure they would do anything at all if they were the wrong way round, but I thought I would mention it.

No mention of it in my manual.

@Vladdy65
Copy link

Appreciate the thoroughness, if you flip the orientation of the clamps does the flow direction change?

Before, with clamps pointing to Load:
image

After, with clamps in opposite direction:
image

Only significant change I see is in the Power Factor. I had to ponder this and brush up on my power factor equations, but I am no wiser as to what just happened. Other than a PF of 39% is not good. I could accept it on channel B where the current is only 0.2A and thus probably close to measurement limits (it is an 80A clamp), but that it flipped and now shows it on channel A is surprising.

@jeverley
Copy link
Contributor Author

jeverley commented Mar 31, 2024

Hmm that's odd, I'd have expected a flow reversal in that scenario - are you able to read the app_version attribute value from the Basic cluster?
That should show if we're running the same firmware version, I've got 74 (or 0x4a).

I can add some debug lines in the quirk to write out the raw reported data point values, possible your device is behaving differently to my own.

@Vladdy65
Copy link

This morning the picture looked different, and more in line with expectations of signs changing. I've left the clamps on 'backward' pointing away from Load. I'm now wondering if it needed some time to 'settle' after changing the clamp direction before taking the 'After' screenshot last night.
As for app_version attribute value from the Basic cluster, I get no response.

image
and it has been taking readings through the night as I had a history graph going to confirm. But now it is frozen. I've removed it but cannot add it back again....

Is there any way to factory reset these devices?

@Vladdy65
Copy link

Its back...
Basic (Endpoint id: 1, Id: 0x0000, Type: in)
app_version (id: 0x0001)
74
Same as yours.

@decrouxtom
Copy link

decrouxtom commented Apr 1, 2024

Thanks for the answer @reef-actor for #2420 (comment)
Power seems ok:
Screenshot_2024-04-01-11-27-13-783_io homeassistant companion android-edit

With the new quirk, total energy seems wrong:
Screenshot_2024-04-01-11-52-38-862_io homeassistant companion android-edit

@ml0renz0
Copy link

Hi guys,

2-clamp device here, same scenario as @Vladdy65

Clamps are set in the proper direction but they are reading negative values just as @Vladdy65 experienced.

Should I reverse the direction of the clamp?

PS: thanks for the great job @jeverley!

@Dominic-070
Copy link

Dominic-070 commented Jul 14, 2024

Sorry about that, here you go:

https://raw.githubusercontent.com/jeverley/zha-device-handlers/ts0601_energy_meter_devices/zhaquirks/tuya/ts0601_energy_meter.py

I have applied this.
it appears that the amperages and VA of both CT terminals are correct, but the wattages are not. The values ​​surrounded in green seem to be correct, the rest are not.
I am trying to measure the consumption of 2 individual devices. So no return only consumption,

image

@drug123
Copy link

drug123 commented Jul 18, 2024

it appears that the amperages and VA of both CT terminals are correct, but the wattages are not.
Same for me - there is no way I have consumed 3 megawatts in two months :))

@Dominic-070
Copy link

it appears that the amperages and VA of both CT terminals are correct, but the wattages are not.
Same for me - there is no way I have consumed 3 megawatts in two months :))

I'm afraid this quirk is specially made for PV distributors with consumption on one side and production on the other. Unfortunately, I have not yet been able to find a good quirk to measure the consumption of the 2 individual devices with the TS0601 _TZE204_81yrt3lo

@Rivenage
Copy link

I had the same issue with positive and negative values. With the clamps mounted in the correct orientation based on the arrows by reversing the wires at the sensor box, I was able to get all the values populating correctly.
Screenshot_20240731-070034
PXL_20240731_110025286

@michaelschnyder
Copy link

I'm working on adding _TZE284_81yrt3lo and plan to base my work on branch from you @jeverley.

Zigbee2Mqtt folks seem to got it working based on

Di you plan to get this one merged upstream anytime soon, or would it make more sense for me to base it on the QuickBuilder?

My Setup

  • Single clamp available: MatSeePlus PJ-1203A (on the case) vs. PJ-1203AN (in the box)
  • Local setup with HAOS in Proxmox

@jeverley
Copy link
Contributor Author

I'm working on adding _TZE284_81yrt3lo and plan to base my work on branch from you @jeverley.

Zigbee2Mqtt folks seem to got it working based on

Di you plan to get this one merged upstream anytime soon, or would it make more sense for me to base it on the QuickBuilder?

My Setup

  • Single clamp available: MatSeePlus PJ-1203A (on the case) vs. PJ-1203AN (in the box)
  • Local setup with HAOS in Proxmox

I'd started to lay the groundwork for a quirk builder based implementation over the weekend using the tuya quirk builder component (https://github.com/zigpy/zha-device-handlers/blob/dev/tuya.md), I'll push these local changes to a new branch this afternoon.

I think we can simplify a lot of what's in my existing v1 quirk, particularly to get a first release of it mainlined.

@jeverley
Copy link
Contributor Author

jeverley commented Jan 28, 2025

Work in progress on the v2 quirk can be found here, should have something testable locally by tomorrow - note this will definitely produce errors right now.

https://github.com/jeverley/zha-device-handlers/blob/ts0601_energy_meter_v2_quirk/zhaquirks/tuya/ts0601_energy_meter.py

@michaelschnyder
Copy link

Great @jeverley! I only was able to get channel A to work, with no modifications, just adding the device information.
Was not able to get Channel B to work, which also deos not not work in Z2M (my current setup)

@jeverley
Copy link
Contributor Author

jeverley commented Feb 2, 2025

Great @jeverley! I only was able to get channel A to work, with no modifications, just adding the device information.
Was not able to get Channel B to work, which also deos not not work in Z2M (my current setup)

The code in the v2 repo should now expose the device using the new quirk format. I currently can see values from both clamps in my testing, and you can select different virtual channel calculations (A plus B, A minus B, B minus A).

There's still some work needed on the power flow handling, but you're welcome to give it a try.
Note you'll likely see quite a few unavailable attributes if you don't first remove and re-pair the device prior to using the V2 quirk.

@jeverley jeverley changed the title ts0601_energy_meter quirk for Tuya power meter devices ts0601_energy_meter quirk for Tuya power meter devices (v1 quirk) Feb 2, 2025
@jeverley
Copy link
Contributor Author

jeverley commented Feb 2, 2025

I'll be progressing this under the new v2 quirk PR #3824

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.