-
-
Notifications
You must be signed in to change notification settings - Fork 9
Home
Home Assistant integration for optimizing Huawei solar batteries, inverters, and EV charging.
HSEM is a modular, secure, and highly configurable Home Assistant integration that automates battery charging/discharging, grid export/import, and adapts to dynamic energy prices, solar forecasts, and EV charging events.
- Battery: Refers to your solar battery (e.g., Huawei battery), not your EV battery.
- EV Charger: Any wall charger integration for electric vehicles.
- Grid Import/Export: Import = buying electricity from the grid; Export = selling electricity to the grid.
- Remove any previous Huawei Solar Battery Optimization Project integrations.
- Install HSEM via HACS or manually.
- Configure your sensors for solar battery, inverter, grid, and EV charger (if present) — do not use the Fusion Solar app for scheduling; HSEM's planner drives charge/discharge decisions automatically.
- Let HSEM run for at least 14 days to collect historical data for optimal performance.
- Monitor the Working Mode Sensor for system status and recommendations.
Tip: New user? Enable Read-Only mode to safely observe what HSEM would do before letting it control your system.
HSEM's entities are split across 7 Home Assistant devices — Controller, Battery & Energy, Hourly Consumption Profile, Financial, Forecast, EV Primary, and EV Secondary — instead of one, so each subsystem can be scoped independently in dashboards, automations, and Areas. See Devices in the sensors reference for the full entity-to-device mapping.
Upgrading from an older HSEM version? A one-time migration moves existing entities to their new device on first startup; dashboards or automations that reference the old single device_id will need to be re-pointed to the new devices.
unique_ids and entity history/statistics are preserved automatically.
- Dynamic Grid Export/Import Management — avoids export at negative prices, forces charging at negative import prices.
- EV Charging Optimization — smart TOU mode during EV charging, prevents battery drain into EV unless configured.
- MILP-Optimal Battery Planning — a globally optimal charge/discharge plan is computed every cycle from price, solar, and consumption forecasts; no manual schedules to configure.
- Excess Battery Export — automatically exports excess battery capacity when profitable, differentiating solar-charged vs grid-charged energy.
- Consumption Forecasting — legacy weighted-average (1d/3d/7d/14d with IQR outlier detection) or ML ridge regression.
- Solar Forecast Integration — Solcast PV forecasts for today and tomorrow.
- Seasonal Mode Switching — Maximize Self Consumption (summer) vs Time-of-Use (winter/spring).
- Read-Only Mode — full "dry run" for safe testing without sending commands to devices.
Yes. HSEM is a full replacement. Remove the old optimization project before installing HSEM.
Always your solar battery (e.g., Huawei battery), not your EV battery.
Yes. When "EV Planned Load" is enabled, HSEM's EV planner computes an optimal charging schedule that charges from excess PV first, then from cheapest grid hours, completing before the configured deadline.
HSEM uses the EV charger status and power sensors to determine when your EV is charging. Depending on your sensor setup, it either subtracts or adds EV charger power to net consumption to avoid double-counting or underestimation.
Yes, but the setting is permission, not a command. Huawei exposes one
global battery discharge limit shared by the house battery and every EV,
so by default HSEM forces battery discharge to 0 W whenever an EV is
charging or about to be commanded — 100% of the EV's load then comes from
the grid. Enabling hsem_ev_charger_force_max_discharge_power lifts that
block; hsem_ev_charger_max_discharge_power sets the ceiling. The ceiling
must be above 0 W — enabling the permission while the ceiling stays at its
0 default still caps discharge at 0 W (the config flow rejects that
combination, and existing entries saved with it log a one-time warning
naming both settings — issue #991). The actual
discharge rate is still the planner's own solved value for the slot,
clamped to that ceiling — enabling the option does not by itself create
any discharge.
No. HSEM calculates and manages your battery's charge/discharge behavior automatically. Any time slots in the Fusion Solar app will be overwritten.
HSEM calculates and rewrites the time slots in Fusion Solar as needed. You do not need to manually adjust them.
Allow at least 14 days for HSEM to collect enough historical data for accurate optimization.
Yes. Enable Read-Only mode via the switch entity. HSEM will show proposed actions without sending commands.
Configure hsem_house_power_includes_ev_charger_power to match your setup. HSEM adjusts net consumption accordingly.
Yes. Use the hsem_force_working_mode select entity. Set to "auto" to return to automatic optimization.
HSEM enters a "Missing Entities Input" state with a clear error description. No changes are made until all sensors are available.
| Working Mode | Description |
|---|---|
| Time Passed | No recommendations — the hour has already passed. |
| Force Export | Charges from grid at negative prices, then exports for profit. |
| Force Batteries Charge | Forces discharge to export excess capacity when profitable. |
| EV Smart Charging | Disables battery discharge when EV charger is active. |
| Force Batteries Discharge | Discharges battery during high-cost periods. |
| Batteries Charge Solar | Charges battery from solar surplus. |
| Batteries Charge Grid | Charges from grid during low/negative import prices. |
| Batteries Discharge Mode | Scheduled battery discharge to minimize grid import. |
| Batteries Wait Mode | Battery idle, waiting for optimal conditions. |
| Missing Entities Input | Missing or misconfigured input sensors. |
| Read Only | Dry run mode — no commands sent to devices. |
Every update cycle, the sensor:
- Fetches configuration & sensor states — battery, inverter, grid prices, solar production, EV charger.
- Performs pre-calculations — net consumption, battery capacity, weighted consumption forecasts, solar forecast, hourly net consumption.
- Applies optimization strategy — determines the best action per hour (Force Export, Charge Solar/Grid, EV Smart Charging, Discharge, Wait).
- Applies working mode & TOU periods — sets battery mode and updates TOU periods if not in read-only mode.
- Updates state & attributes — exposes hourly calculations, recommendations, and schedule status.
- Do not configure schedules in the Fusion Solar app. HSEM overwrites them.
- Ensure all required sensors are available and correctly configured.
- Allow HSEM to collect 14 days of data before expecting optimal results.
Automatically exports excess battery capacity to the grid when profitable:
- Calculate required battery — minimum capacity needed for rest-of-day consumption + safety buffer.
- Identify excess — any capacity above requirement is available for export.
- Optimize timing — export at peak price hours, respecting max discharge rate.
- Differentiate energy source — solar-charged energy exports at any positive price; grid-charged only exports if profit ≥ threshold.
- Economic threshold — calculated from battery charging economics:
See Battery Charging Economics for the full derivation.
- Battery: 100 kWh usable, current 95 kWh
- Consumption forecast (rest of day): 60 kWh, buffer 10%
- Required: 70 kWh → Excess: 25 kWh → Export at peak prices
HSEM supports two prediction modes:
- Legacy (default): Weighted averages over 1d/3d/7d/14d windows with IQR outlier detection, baseline capping, and reliability scaling.
- ML (optional): Ridge regression querying the HA recorder directly.
| Mode | 1 Day | 3 Days | 7 Days | 14 Days |
|---|---|---|---|---|
| Balanced (default) | 25 | 30 | 30 | 15 |
| Conservative | 20 | 30 | 35 | 15 |
| Fast-reacting | 30 | 30 | 25 | 15 |
| Option | Purpose |
|---|---|
hsem_ev_charger_force_max_discharge_power |
Permits (does not force) battery discharge while this EV is charging; without it, discharge is 0 W whenever the EV is active |
hsem_ev_charger_max_discharge_power |
Maximum discharge power (W) for EV charging; must be above 0 W when the permission above is enabled (issue #991) |
hsem_force_working_mode |
Manually override to a specific working mode |
This integration was inspired by and derived from the Huawei Solar Battery Optimization Project.
- Home — User-facing overview: features, FAQ, working modes, excess export, consumption sensors
- Battery Charging Economics — How to calculate the minimum charging price for your battery
- Architecture Overview — System context, layered architecture, module map, planning pipeline
- Planner Specification — Normative — all planner invariants, rules, and constraints
- Planner Technical Guide — How the planner works with worked examples
- Cost Function Math — Complete mathematical formulation of the 8-term cost function
- Energy Accounting — Physical energy flow model, SoC simulation, efficiency math
- Candidate Generation — How candidates are generated, assumptions, partial-SoC
- MILP Optimization — Full LP formulation, variable layout, constraints, and solver pipeline
- Consumption Prediction — Weighted-average model, IQR outlier detection, spike suppression
- Safety Modes — Degraded mode, read-only gate, write-verify applier, runtime resolver
- Price Scaling — EDS price cadence auto-detection and raw pass-through
- Services Reference — All 5 HSEM services with examples
- Sensors Reference — Complete entity reference: all sensor, select, switch, number, and time entities
- Dashboard Setup — Step-by-step ApexCharts dashboard with full YAML, layout reference, and troubleshooting
- Config Flow Reference — Every config/options flow step and field
- EV Charge Plan Setup — EV planned load configuration guide
- EV Surplus Charging Automation — Wire your physical EV charger (go-e, Easee, Zaptec) to follow HSEM surplus recommendations
- EV Optimal Charging Template — Legacy Home Assistant template sensor for cost-optimal EV charging
- Forecast Accuracy Tracking — Forecast vs actual tracking system
- Huawei Entities — Canonical HA entity ID reference
- Troubleshooting Guide — Diagnose and fix common problems: missing data, wrong prices, write failures, battery behaviour
- Quality Checks — Static quality tools and CI configuration