Skip to content

Migrate to calendar_history API and fix data handling#102

Closed
gbassaragh wants to merge 1 commit intoMikeBishop:masterfrom
gbassaragh:fix/calendar-history-api-migration
Closed

Migrate to calendar_history API and fix data handling#102
gbassaragh wants to merge 1 commit intoMikeBishop:masterfrom
gbassaragh:fix/calendar-history-api-migration

Conversation

@gbassaragh
Copy link

Summary

Tesla deprecated the /history endpoint in favor of /calendar_history. This PR migrates all API calls and fixes the resulting data handling issues that were causing broken charts.

Changes

API Migration (node_helper.js):

  • Add getCalendarHistoryDates() helper for consistent date parameter formatting
  • Migrate all history endpoints to use calendar_history with required start_date, end_date, and time_zone parameters

Data Handling Fixes (MMM-Powerwall.js):

  • SelfConsumption Handler: Handle single-entry API responses gracefully (fixes self-powered ring)
  • generateDaystart Function: Aggregate granular time-series data (~170+ 5-minute interval entries) into daily totals (fixes Energy To/From chart)

Problem

The old /history endpoint returned daily aggregate values, but the new /calendar_history endpoint returns:

  • self_consumption: May return only today's data (1 entry) instead of yesterday + today (2 entries)
  • energy: Returns granular 5-minute interval time-series data instead of daily aggregates

This caused:

  • Self-powered ring to fail (code expected payload.selfConsumption[1] but array only had 1 element)
  • Energy To/From bar chart to display incorrect data (code expected 2 aggregate entries but received 170+ time-series entries)

Testing

  • ✅ Self-powered ring: Displays correct solar/battery/grid percentages
  • ✅ Power To/From histogram: Shows real-time power flow
  • ✅ Energy To/From bar chart: Shows cumulative daily energy usage
  • ✅ Current Usage display: Shows live consumption data

🤖 Generated with Claude Code

Tesla deprecated the /history endpoint in favor of /calendar_history. This
commit migrates all API calls and fixes the resulting data handling issues.

## API Migration (node_helper.js)

- Add `getCalendarHistoryDates()` helper for consistent date parameter formatting
- Migrate all history endpoints to use calendar_history:
  - energy: /history?period=day&kind=energy → /calendar_history?kind=energy&period=day
  - power: /history?period=day&kind=power → /calendar_history?kind=power&period=day
  - backup: /history?kind=backup → /calendar_history?kind=backup
  - self_consumption: /history?kind=self_consumption&period=day → /calendar_history?kind=self_consumption&period=day
- Add required start_date, end_date, and time_zone parameters

## Data Handling Fixes (MMM-Powerwall.js)

### SelfConsumption Handler
The calendar_history API may return only today's data (1 entry) instead of
yesterday + today (2 entries). Updated the handler to detect single-entry
responses and gracefully default yesterday values to zero.

### generateDaystart Function
The calendar_history energy endpoint returns granular time-series data
(5-minute intervals, ~170+ entries per day) instead of daily aggregates.
Rewrote the function to:
- Aggregate all time-series entries to calculate today's cumulative totals
- Handle variable response lengths gracefully
- Set yesterday baseline to zero (API only provides today's granular data)

This fixes the Energy To/From bar chart which was broken due to expecting
only 2 daily aggregate entries.

## Testing
- Self-powered ring: ✓ Displays correct solar/battery/grid percentages
- Power To/From histogram: ✓ Shows real-time power flow
- Energy To/From bar chart: ✓ Shows cumulative daily energy usage
- Current Usage display: ✓ Shows live consumption data

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@MikeBishop
Copy link
Owner

Largely duplicate of #100, other than staying on the owner-api endpoint. If you've tested that the same API works on the old API, I can add the option to retain it.

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.

2 participants

Comments