Skip to content

Comments

Change data source for charger energy meter#235

Merged
sveinse merged 5 commits intocustom-components:masterfrom
sveinse:feature-fix-energy-sensor
Jul 24, 2025
Merged

Change data source for charger energy meter#235
sveinse merged 5 commits intocustom-components:masterfrom
sveinse:feature-fix-energy-sensor

Conversation

@sveinse
Copy link
Collaborator

@sveinse sveinse commented Jul 23, 2025

  • Create new ZaptecEnergySensor class
  • Implement a simple OCMF decoder to extract the latest energy meter reading

Fix #233, Fix #219

sveinse added 2 commits July 24, 2025 01:33
* Create new ZaptecEnergySensor class
* Implement a simple OCMF decoder to extract the latest energy meter reading
@sveinse sveinse added this to the v0.8 milestone Jul 23, 2025
Copy link
Contributor

@steinmn steinmn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should work as written, but I think it can be simplified by ignoring time and just using the max value.

Comment on lines 101 to 134
def get_ocmf_latest_reader_value(data: dict) -> int:
"""Return the latest reader value from OCMF data."""

if not isinstance(data, dict):
return 0
if "RD" not in data:
return 0

# Find the latest reading
readings = []
for reading in data["RD"]:
value = reading.get("RV")
if value is None:
continue
ts = None
m = RE_OCMF_TIMESTAMP.match(reading.get("TM", ''))
if m:
ts = datetime(
int(m[1]), # Year
int(m[2]), # Month
int(m[3]), # Day
int(m[4]), # Hours
int(m[5]), # Minutes
int(m[6]), # Seconds
int(m[7]) * 1000, # Milliseconds
tzinfo=timezone(timedelta(hours=int(m[8]), minutes=int(m[9]))), # Time zone
)
readings.append((ts, value))

# Sort readings by timestamp
readings.sort(key=lambda x: x[0])
if readings:
return readings[-1][1] # Return the latest value
return 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aren't we overcomplicating this? This is an increasing value that will never decrease, isn't it? So we just need the maximum value, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I am. Fixed now.

),
ZapSensorEntityDescription(
key="signed_meter_value_kwh",
key="signed_meter_value_kwh", # This key is unused, but kept for <0.7 compatibility
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
key="signed_meter_value_kwh", # This key is unused, but kept for <0.7 compatibility
key="signed_meter_value_kwh", # This key is no longer used to get Zaptec values, but is linked to the entity unique_id, so it is kept for <0.7 compatibility

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added. Thanks.

@sveinse
Copy link
Collaborator Author

sveinse commented Jul 24, 2025

@steinmn Please review now

Copy link
Contributor

@steinmn steinmn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The energy meter values should be floats, otherwise LGTM

raise AttributeError(f"Unknown record type {hex(record_type)}")


def get_ocmf_max_reader_value(data: dict) -> int:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def get_ocmf_max_reader_value(data: dict) -> int:
def get_ocmf_max_reader_value(data: dict) -> float:

if not isinstance(data, dict):
return 0
return max(
int(reading.get("RV", 0))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
int(reading.get("RV", 0))
float(reading.get("RV", 0))

@sveinse
Copy link
Collaborator Author

sveinse commented Jul 24, 2025

Look at this from the positive side: With all these PRs back and forth we get some real hand-on experience with switching branches in git 😁

@sveinse sveinse merged commit aabf631 into custom-components:master Jul 24, 2025
1 check passed
@sveinse sveinse deleted the feature-fix-energy-sensor branch July 24, 2025 18:58
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.

Change source of entity "energy_meter" to use state variable Entity are now from Zaptec sources that updated slowly

2 participants