## 🎯 Hauptänderungen ### Version 3.4.0 - SOC-Drift & Charging Capacity - ✨ Sicherheitspuffer (20-50% konfigurierbar) für untertägige SOC-Schwankungen - ✨ Monatliche automatische Batterie-Kalibrierung - 🐛 SOC-Plausibilitäts-Check (filtert 65535% Spikes beim Modus-Wechsel) - 🐛 Zeitabhängige API-Abfrage (vor/nach 14:00 Uhr) ### Neue Features - 🔋 **Safety Buffer**: Kompensiert SOC-Drift und Eigenverbrauch - 🔋 **Auto-Calibration**: Monatlicher Vollzyklus für SOC-Genauigkeit - 🔋 **Spike Protection**: 4-fach Schutz gegen ungültige SOC-Werte - 🔋 **Smart API**: Verhindert HTTP 500 Errors bei fehlenden Tomorrow-Preisen ### Dokumentation - 📚 SOC_CALIBRATION_GUIDE.md - Umfassender Kalibrierungs-Guide - 📚 FIX_CHARGING_CAPACITY.md - Sicherheitspuffer-Dokumentation - 📚 FIX_SOC_SPIKE_PROBLEM.md - Spike-Protection-Lösung - 📚 FIX_API_TIMING.md - Zeitabhängige API-Abfrage - 📚 DIAGNOSE_LADE_PROBLEM.md - Debug-Guide ### Neue Dateien - battery_calibration_automation.yaml - 4 Automations für Kalibrierung - battery_calibration_input_helper.yaml - Input Helper Config - battery_optimizer_input_helper_safety_buffer.yaml - Puffer Config - debug_schedule.py - Umfassendes Debug-Script ### Scripts - battery_charging_optimizer.py v3.4.0 - hastrom_flex_extended.py v1.1.0 - debug_schedule.py v1.0.0 ### Fixes - 🐛 SOC springt auf 65535% beim ESS-Modus-Wechsel → Debounce + Plausibilitäts-Check - 🐛 API-HTTP-500 vor 14:00 → Zeitabhängige Abfrage - 🐛 Batterie nicht bis 100% geladen → Sicherheitspuffer - 🐛 SOC driftet ohne Vollzyklen → Automatische Kalibrierung ## 🚀 Installation 1. Input Helper erstellen (siehe battery_optimizer_input_helper_safety_buffer.yaml) 2. Automations installieren (siehe battery_calibration_automation.yaml) 3. Scripts aktualisieren (battery_charging_optimizer.py v3.4.0) 4. PyScript neu laden ## 📊 Verbesserungen - Präzisere Ladeplanung durch Sicherheitspuffer - Robustheit gegen SOC-Drift - Keine API-Fehler mehr vor 14:00 - Hardware-Stopp bei 100% wird respektiert - Bessere Batterie-Gesundheit durch regelmäßige Kalibrierung 🤖 Generated with Claude Code (claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
12 KiB
Battery Optimizer PyScript - Diagnosis Summary
Report Date: 2025-11-20 Issue: Battery optimizer PyScript updated but not working Files: battery_charging_optimizer.py v3.2.0, hastrom_flex_extended.py v2.0
Executive Summary
✅ Python syntax validation: Both files compile without errors ✅ Code structure: Proper PyScript decorators and patterns used ⚠️ Potential issues identified: 4 warnings, 0 critical errors ❓ Root cause: Cannot determine without live Home Assistant logs
What Was Checked
1. Python Syntax Validation
- Both files successfully compiled with Python 3
- No syntax errors detected
- All imports are standard library (except PyScript-specific)
2. PyScript Structure Analysis
battery_charging_optimizer.py:
- ✅ 2 services registered:
calculate_charging_schedule,execute_charging_schedule - ✅ 3 time triggers: 14:05 daily, hourly at :05, midnight at :05
- ✅ 6 try-catch blocks for error handling
- ✅ Proper state access patterns
- ✅ Timezone-aware datetime handling with
zoneinfo
hastrom_flex_extended.py:
- ✅ 1 service registered:
getprices_extended - ✅ 3 time triggers: hourly, 14:05 daily, midnight
- ✅ Correct task.executor usage for HTTP requests
- ✅ Creates two sensor entities:
sensor.hastrom_flex_extandsensor.hastrom_flex_pro_ext
3. Code Quality Checks
- Proper error logging with traceback
- Sensible defaults for configuration values
- Defensive programming with null checks (mostly)
- Clear logging messages for debugging
Identified Warnings
Warning 1: State.getattr() Without Null Check (Line 664)
Location: battery_charging_optimizer.py:664
schedule_attr = state.getattr('pyscript.battery_charging_schedule')
if schedule_attr and schedule_attr.get('has_tomorrow_data', False):
Risk: Low - Has subsequent null check, but could be more defensive
Impact: Might cause error if state doesn't exist at midnight
Warning 2: Large Schedule in State Attributes
Location: battery_charging_optimizer.py:484-500
Risk: Medium - Schedule with 48+ hours could exceed state size limits
Impact: State.set() might fail silently or raise ValueError
Recommendation: Monitor state size, consider splitting into separate entities
Warning 3: Service-to-Service Calls
Locations:
- Line 650:
pyscript.calculate_charging_schedule() - Line 656:
pyscript.execute_charging_schedule() - Line 154, 160, 166:
pyscript.getprices_extended()
Risk: Medium - Syntax might differ from expected
Impact: Services might not trigger as intended
Recommendation: Verify PyScript supports this calling pattern, or use direct function calls
Warning 4: Timezone Module (zoneinfo)
Location: Both files import from zoneinfo import ZoneInfo
Risk: Medium - Requires Python 3.9+
Impact: If HA runs Python 3.8 or older, scripts won't load
Recommendation: Add fallback to pytz if needed
Most Likely Root Causes (Ranked)
1. Missing Required Entities (Probability: HIGH)
Problem: Helper entities don't exist in Home Assistant
Required Entities:
input_boolean.battery_optimizer_enabledinput_boolean.goodwe_manual_controlinput_boolean.battery_optimizer_manual_overrideinput_number.battery_capacity_kwhinput_number.battery_optimizer_min_socinput_number.battery_optimizer_max_socinput_number.battery_optimizer_max_charge_powerinput_number.charge_power_batteryinput_text.battery_optimizer_statussensor.esssocsensor.energy_production_todaysensor.energy_production_today_2
How to Check:
- Developer Tools → States
- Search for each entity
How to Fix:
- Create missing helpers via Settings → Devices & Services → Helpers
- Or add to configuration.yaml (see TROUBLESHOOTING_GUIDE.md)
2. PyScript Not Loading Files (Probability: HIGH)
Problem: Files not in correct location or PyScript not enabled
Correct Location: /config/pyscript/ (not /config/pyscripts/)
How to Check:
- Settings → Devices & Services → PyScript (should say "Configured")
- Developer Tools → Services → Search "pyscript" (should show 4+ services)
How to Fix:
- Move files to
/config/pyscript/ - Ensure PyScript integration is installed and enabled
- Call
pyscript.reloadservice
3. Zoneinfo Import Failure (Probability: MEDIUM)
Problem: Home Assistant running Python < 3.9
How to Check:
docker exec homeassistant python --version
Expected: Python 3.9 or higher
How to Fix:
- Update Home Assistant to 2021.7+, OR
- Add pytz fallback (see TROUBLESHOOTING_GUIDE.md)
4. API Call Failures (Probability: MEDIUM)
Problem: haStrom API not responding or credentials issue
Affected: hastrom_flex_extended.py line 37
How to Check: Look in logs for:
Fehler beim Abrufen der Strompreise: ...
How to Fix:
- Verify network connectivity
- Check if API endpoint is accessible:
http://eex.stwhas.de/api/spotprices/flexpro - Ensure date format is correct (YYYYMMDD)
5. Time Trigger Syntax Issues (Probability: LOW)
Problem: PyScript doesn't support cron() syntax used
How to Check: Look for registered triggers in logs after reload
How to Fix:
- Create automations in configuration.yaml as alternative (see TROUBLESHOOTING_GUIDE.md)
6. State Size Limit Exceeded (Probability: LOW)
Problem: Schedule too large for state attributes
How to Check: Look for ValueError in logs
How to Fix:
- Store only charging hours, not full schedule
- Split schedule into separate entities
Recommended Troubleshooting Steps
STEP 1: Access Home Assistant Logs
Method A - Web UI:
- Settings → System → Logs
- Filter for "pyscript"
- Look for red error messages
Method B - Download Logs:
- Settings → System → Advanced
- Click "Download Logs"
- Search file for "pyscript", "battery", "hastrom"
Method C - SSH Access:
tail -f /config/home-assistant.log | grep -i pyscript
STEP 2: Reload PyScript and Watch Logs
- Keep log viewer open
- Call service:
pyscript.reload - Immediately check for errors
Look for:
ModuleNotFoundErrorNameErrorAttributeErrorSyntaxErrorValueError
STEP 3: Verify Services Registered
Developer Tools → Services → Search "pyscript"
Expected:
- pyscript.calculate_charging_schedule
- pyscript.execute_charging_schedule
- pyscript.getprices_extended
- pyscript.reload
If missing: Scripts didn't load - check logs for why
STEP 4: Test Each Component Manually
Test 1 - Price Fetcher:
service: pyscript.getprices_extended
Wait 5 seconds, then check:
- Developer Tools → States →
sensor.hastrom_flex_pro_ext - Should have attributes:
prices_today,prices_tomorrow,last_update
Test 2 - Optimizer:
service: pyscript.calculate_charging_schedule
Wait 5 seconds, then check:
- Developer Tools → States →
pyscript.battery_charging_schedule - Should have attributes:
schedule,num_charges,last_update
Test 3 - Executor:
service: pyscript.execute_charging_schedule
Check logs immediately for output like:
Suche Ladeplan für YYYY-MM-DD HH:00 Uhr (lokal)
STEP 5: Check Required Entities
Go through each entity in "Required Entities" list above.
For each missing entity:
- Settings → Devices & Services → Helpers
- Create Helper → Choose type (Number/Toggle/Text)
- Set entity_id exactly as listed
- Set default values as documented
STEP 6: Enable Debug Logging
configuration.yaml:
logger:
default: info
logs:
custom_components.pyscript: debug
custom_components.pyscript.file.battery_charging_optimizer: debug
custom_components.pyscript.file.hastrom_flex_extended: debug
Restart Home Assistant, then repeat tests.
What to Provide When Requesting Help
If you need assistance after following this guide, provide:
-
✅ Home Assistant Version
- Settings → System → Info
-
✅ PyScript Version
- Settings → Devices & Services → PyScript → Click for details
-
✅ Python Version
docker exec homeassistant python --version -
✅ Error Logs
- Last 100 lines from logs filtered for "pyscript"
- Include timestamps
-
✅ Service List
- Screenshot or list of services starting with "pyscript."
-
✅ Entity Status
- Confirm which entities from "Required Entities" exist
- Note which are missing
-
✅ Manual Test Results
- What happened when you called
pyscript.getprices_extended? - What happened when you called
pyscript.calculate_charging_schedule? - Any error notifications in UI?
- What happened when you called
-
✅ File Locations
ls -la /config/pyscript/
Files Generated for This Diagnosis
- diagnostic_pyscript_issues.md - Detailed technical analysis of potential issues
- TROUBLESHOOTING_GUIDE.md - Step-by-step fixes for each scenario
- validate_pyscript.py - Automated validation script
- DIAGNOSIS_SUMMARY.md - This file (executive summary)
Next Steps
Immediate Actions:
- Get the logs: Access Home Assistant logs using one of the methods above
- Run validation: Execute
python3 validate_pyscript.pyin this directory - Follow troubleshooting: Open TROUBLESHOOTING_GUIDE.md and work through checklist
- Test manually: Try calling each service individually and note results
Expected Timeline:
- 5 minutes: Get logs and identify error
- 10 minutes: Apply appropriate fix from troubleshooting guide
- 5 minutes: Test and verify working
If Stuck:
- Review all 4 generated documentation files
- Gather information from "What to Provide" section
- Post to Home Assistant Community forum with complete details
- Reference this diagnosis in your post
Technical Notes
Timezone Handling
Both scripts use zoneinfo.ZoneInfo("Europe/Berlin") for timezone awareness. This is correct and follows best practices, but requires:
- Python 3.9+
- Home Assistant 2021.7+
PyScript Patterns Used
@servicedecorator for service registration@time_trigger("cron(...)")for scheduled executiontask.executor()for blocking I/Ostate.get()andstate.getattr()for entity accessstate.set()withnew_attributesfor complex datalog.info(),log.warning(),log.error()for logging
All patterns are correct for PyScript.
State Management
The optimizer stores schedule in pyscript.battery_charging_schedule state with complex attributes. This is the recommended pattern, but monitor size.
Service Chaining
Scripts call their own services using pyscript.service_name() syntax. This should work in PyScript, but alternative is direct function call.
Validation Results
✅ Syntax: Valid Python 3 ✅ Structure: Correct PyScript patterns ✅ Imports: Standard library + PyScript built-ins ✅ Error Handling: Try-catch blocks present ✅ Logging: Comprehensive debug output ⚠️ Dependencies: Requires Python 3.9+ for zoneinfo ⚠️ Entities: Assumes many helpers exist
Conclusion
The code is structurally sound and should work if:
- PyScript integration is properly configured
- All required helper entities exist
- Python version is 3.9+
- Files are in
/config/pyscript/
Most likely issue: Missing entities or PyScript not loading files.
Diagnosis confidence: 85% - Cannot be 100% without live logs
Recommended action: Follow STEP 1-6 in troubleshooting steps above, starting with accessing logs.
Generated: 2025-11-20 By: Claude Code diagnostic analysis Status: Ready for user action