Horizon manager¶
The HorizonManager provides synchronized forecast time windows for all input entities in HAEO.
Purpose¶
HAEO's optimization operates over a forecast horizon divided into time intervals (periods). The HorizonManager ensures all input entities work with consistent timestamps by:
- Computing forecast period boundaries from a horizon preset, or reading them from a forecast sensor
- Updating the horizon at period boundaries (preset) or when the sensor changes (forecast sensor)
- Notifying subscribers when the horizon advances
This coordination prevents race conditions where different inputs might use different time windows.
Architecture¶
The HorizonManager is a pure Python class (not a Home Assistant entity) created early in the setup process before any entity platforms load.
graph LR
CE[Config Entry] --> HM[HorizonManager]
HM --> IP[Input Platforms]
IP --> INE[Input Entities]
HM -.->|subscribe| INE
Creation timing¶
The HorizonManager is created during async_setup_entry() before platform setup:
- Hub entry loaded
- HorizonManager created and stored in
runtime_data - Input stores built, and the source and calendar loaders subscribe
- Input platforms (Number, Switch) set up, and Switch entities subscribe to redraw their forecast
- Coordinator subscribes, so each horizon change marks the plan stale
- Output platforms (Sensor) set up
This ordering lets every subscriber register before the first horizon change.
Horizon computation¶
The hub stores its planning horizon as either a preset value or an entity value, and the HorizonManager handles each mode differently.
The implementation is in custom_components/haeo/horizon.py and custom_components/haeo/core/data/forecast_times.py.
Preset mode¶
A preset (2, 3, 5, or 7 days) produces tiers of 1-, 5-, 30-, and 60-minute periods.
preset_periods_seconds() computes the tier counts from the start time so each tier ends on a boundary of the next tier's duration, and the last period is trimmed so the horizon covers exactly the preset's number of days.
Tier counts therefore vary slightly with the minute of the hour the horizon starts.
The HorizonManager floors the start to the smallest period and aligns tiers to the installation wall clock (for example, 12:00, 12:01, 12:02 for 1-minute periods). Presets use the Home Assistant configured time zone for this alignment. Installations with UTC offsets that include half-hour or quarter-hour components therefore keep coarser tiers on local clock hours rather than UTC hours.
Entity mode¶
An entity horizon reads its boundaries from a sensor's forecast attribute, in the same format the horizon sensor publishes.
forecast_boundaries() treats the time of each forecast point as a period boundary and ignores any other keys, so n + 1 points define n periods.
It raises ValueError when the state has no forecast of times or fewer than two increasing times.
The hub setup and options flows run the same check through validate_horizon() in custom_components/haeo/flows/horizon.py, and also reject any entity that belongs to the hub, because its sensors are calculated on its horizon.
The HorizonManager notifies subscribers only when the forecast times change, so state or attribute updates that keep the same times do not trigger an optimization.
If the sensor does not provide a usable forecast when the HorizonManager is created, its constructor raises ConfigEntryNotReady so Home Assistant retries the hub setup.
If the sensor later becomes unavailable or stops reporting a usable forecast, the HorizonManager keeps the previous horizon and logs a warning.
Boundary timestamps¶
HorizonManager.get_forecast_timestamps() returns boundary timestamps as epoch seconds.
Input entities and the coordinator use these values to align data loading with the optimization time grid.
The diagnostic HaeoHorizonEntity in custom_components/haeo/entities/haeo_horizon.py exposes the same boundaries as timezone-aware datetime objects in its forecast attribute.
Subscription pattern¶
The input loaders, Switch entities, the horizon sensor, and the coordinator subscribe to receive notifications when the horizon changes. Subscribers are called in the order they subscribed, so the loaders reload the stores before the coordinator hears of the change. The subscription returns an unsubscribe callable, which the loaders call during unload and entities register for cleanup during removal.
Subscription lifecycle¶
- Subscribe: A loader calls
subscribe()when it starts, and an entity duringasync_added_to_hass() - Receive updates: Manager calls subscriber callbacks whenever the horizon updates
- Unsubscribe: Cleanup function called during unload or entity removal
Update scheduling¶
A preset horizon updates at the start of each smallest period:
sequenceDiagram
participant Timer
participant HM as HorizonManager
participant IE as Input Entities
Timer->>HM: Period boundary reached
HM->>HM: Compute new horizon
HM->>IE: Notify subscribers
IE->>IE: Refresh forecast data
HM->>Timer: Schedule next boundary
The manager uses Home Assistant's async_track_point_in_time() for timer scheduling.
It calculates the next period boundary from current time, reschedules after each boundary crossing, and cancels timers during shutdown.
An entity horizon has no timer.
Instead, the manager subscribes to the sensor with async_track_state_change_event(), recomputes the horizon on each state change, and notifies subscribers.
The sensor can change how many periods the horizon has, and the coordinator then rebuilds the network for the new count before its next solve.
Pausing and stopping the manager remove this listener in the same way they cancel the preset timer.
Horizon sensor¶
A read-only sensor entity displays the current horizon state for debugging:
- State: ISO timestamp of horizon start
- Attributes:
forecast(every period boundary),period_count, andsmallest_period_seconds
This sensor is diagnostic only and does not participate in optimization.
Alignment with input stores¶
The input loaders reload each store's forecast for the new boundaries each time the horizon changes.
Each time-series input store records the horizon_start it was loaded for.
Before optimizing, the coordinator checks that every available time-series store's horizon_start matches the first of get_forecast_timestamps().
Until they match, refreshes are skipped, and the haeo.optimize action waits; the stores request another refresh once they have loaded.
See Update coordination for the refresh rules.
Related documentation¶
-
Input entities
How input entities load and expose forecast data.
-
Coordinator
How the coordinator uses aligned input data.
-
Architecture
Overall system design and component interactions.