Skip to content

Fix midnight-crossing charge windows incorrectly capped at 23:59 - #2851

Closed
springfall2008 with Copilot wants to merge 3 commits into
mainfrom
copilot/fix-midnight-charging-issue
Closed

springfall2008 with Copilot wants to merge 3 commits into
mainfrom
copilot/fix-midnight-charging-issue

Conversation

Copilot AI commented Nov 1, 2025 •

Copy link
Copy Markdown
Contributor

Charging windows spanning midnight (e.g., 22:30→05:30) were incorrectly capped at 23:59 for inverters that don't support midnight spanning, causing premature charge termination. The bug used minute arithmetic (minutes_end >= 24*60) instead of comparing actual dates.

Changes

  • Charge/discharge window logic (execute.py:87-97, 295-306): Replace minute-based midnight detection with datetime comparison

Before

if not inverter.inv_can_span_midnight:
    if minutes_start < 24 * 60 and minutes_end >= 24 * 60:
        minutes_end = 24 * 60 - 1  # Always caps, even when same-day

After

if not inverter.inv_can_span_midnight:
    charge_start_datetime = self.midnight_utc + timedelta(minutes=minutes_start)
    charge_end_datetime = self.midnight_utc + timedelta(minutes=minutes_end)
    
    if charge_start_datetime.date() != charge_end_datetime.date():
        end_of_start_date = charge_start_datetime.replace(hour=23, minute=59, second=0, microsecond=0)
        minutes_end = int((end_of_start_date - self.midnight_utc).total_seconds() / 60)

Impact

  • Windows truly crossing midnight (22:30 today → 05:30 tomorrow): Correctly capped at 23:59
  • Same-day windows after midnight (01:00 → 05:30 today): No longer incorrectly capped
  • Existing unit tests charge_midnight1, charge_midnight2 remain compatible
Original prompt

This section details on the original issue you should resolve

<issue_title>Charging spanning midnight errors</issue_title>
<issue_description>When running the latest versions I.e. anything 8.26 onwards I’m getting issues with charging the batteries spanning midnight.

For the last three nights the batteries have been scheduled in the plan to charge from 2230 until 0530 and predbat believes they are charging. However it actually sets the finish time to 23:59 and therefore stops charging and the inverter then uses all of the battery until it runs out before I’m awake.

Would appear the new updates have an issue dealing with times past 23:59 I.e. the next day

Predbat version
8.26 onwards (8.25.14 works without issue)

Environment details

  • Solis 3.68kw hybrid
  • 3x us3000 pylontech batteries
    </issue_description>

Comments on the Issue (you are @copilot in this section)

@gcoan could you provide log files and debug file per the github issue template please to aid diagnosing the problem.

Also if you can provide the plan where predbat plans to charge over midnight and the sensor history to show how predbat didn't program what was planned

thanks</comment_new>


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits November 1, 2025 21:27
Co-authored-by: springfall2008 <48591903+springfall2008@users.noreply.github.com>
Co-authored-by: springfall2008 <48591903+springfall2008@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix charging spanning midnight errors in predbat Fix midnight-crossing charge windows incorrectly capped at 23:59 Nov 1, 2025
Copilot AI requested a review from springfall2008 November 1, 2025 21:38
Comment thread apps/predbat/execute.py
if minutes_start < 24 * 60 and minutes_end >= 24 * 60:
minutes_end = 24 * 60 - 1
# Convert to datetime first to check if it actually crosses midnight
charge_start_datetime = self.midnight_utc + timedelta(minutes=minutes_start)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Don't think this is the problem as minutes here are from midnight anyhow

@springfall2008
springfall2008 deleted the copilot/fix-midnight-charging-issue branch May 4, 2026 11:07
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.

Charging spanning midnight errors

2 participants