LW IT Solutions
« Blog Overview /Smart Home/Tutorials / Automating Solar Energy in Home Assistant: Dynamic...

Automating Solar Energy in Home Assistant: Dynamic Surplus Charging

Automating Solar Energy in Home Assistant: Dynamic Surplus Charging
Contents
  1. 1. Architectural Principles of Real-Time Surplus Calculation
  2. 2. Step-by-Step Template Sensor Configuration
  3. 3. Step-by-Step Automation for Dynamic Battery & Appliance Stepping
  4. 4. Summary & Architectural Value
  5. Sources

Maximizing the self-consumption rate of a residential photovoltaic (PV) installation requires real-time synchronization between solar power generation, household base loads, and high-capacity electrical consumers. When excess solar energy is fed back into the public grid at low feed-in tariffs, financial efficiency drops significantly. Implementing a dynamic surplus charging architecture within Home Assistant—leveraging Modbus or MQTT telemetry from hybrid inverters, smart energy meters, and battery storage units—allows high-demand consumers (such as battery banks, EV chargers, and heat pump boilers) to modulate their power draw dynamically based on real-time solar surplus.

1. Architectural Principles of Real-Time Surplus Calculation

Dynamic surplus management relies on calculating the net available power at the grid interconnection point before triggering or modulating load devices. The system architecture is built on three foundational metrics:

  • Grid Export Power (sensor.grid_export_power): Real-time wattage measured by a bidirectional smart meter at the main electrical intake. A positive export value indicates surplus solar energy leaving the household.
  • Dynamic Surplus Thresholds: High-demand appliances must only activate when export power exceeds a defined hysteresis threshold (e.g., > 1,500 W for at least 300 seconds) to prevent short-cycling caused by passing clouds.
  • Priority-Based Load Modulation: Available surplus must be allocated hierarchically: first to home battery storage (via hybrid inverter charging limits), second to variable-rate EV charging, and third to thermal storage or resistive heating elements.
Day curve of solar production against base load with the shaded surplus window, threshold, moving average and the priority order of the loads
Only the shaded window is genuinely surplus: production above base load, smoothed by a moving average and above the threshold. Within it the loads start in a fixed order.

2. Step-by-Step Template Sensor Configuration

To provide a clean, noise-filtered signal for Home Assistant automations, a dedicated template sensor must be configured within configuration.yaml to calculate the exact net surplus wattage:

  1. Define the Template Sensor: Create a modern template sensor entity named sensor.solar_surplus_power that evaluates grid export power and sets negative values (grid import) strictly to zero.
  2. Apply Hysteresis Smoothing: Implement a low-pass filter or moving average using the Home Assistant Filter integration to eliminate rapid wattage spikes caused by momentary appliance activation.
  3. Verify Unit Consistency: Ensure all power metrics are standardized to Watts (W) and include explicit device_class: power and state_class: measurement definitions for Riemann sum integration compatibility.

Production-Ready YAML Template Sensor

template:
  - sensor:
      - name: "Solar Surplus Power"
        unique_id: "solar_surplus_power_dynamic"
        unit_of_measurement: "W"
        device_class: "power"
        state_class: "measurement"
        state: >
          {% set export_power = states('sensor.grid_meter_export_power') | float(0) %}
          {% set import_power = states('sensor.grid_meter_import_power') | float(0) %}
          {% if export_power > import_power %}
            {{ (export_power - import_power) | round(0) }}
          {% else %}
            0
          {% endif %}
        availability: >
          {{ has_value('sensor.grid_meter_export_power') and has_value('sensor.grid_meter_import_power') }}

3. Step-by-Step Automation for Dynamic Battery & Appliance Stepping

With a reliable surplus sensor operational, an automation must be deployed to modulate charging current or trigger smart switches without causing grid oscillation:

  1. Trigger Definition: Configure a Numeric State trigger observing sensor.solar_surplus_power exceeding 1,500 W for a continuous duration of 5 minutes.
  2. Condition Evaluation: Add conditions requiring household battery State of Charge (SoC) to be below 95% and the EV charger to be plugged in.
  3. Action Execution (Dynamic Amp Tuning): Execute a service call to adjust battery charging power or increment EV charging current (e.g., from 6A up to 16A per phase) dynamically matching the available surplus delta.
  4. Graceful Deactivation: Build a secondary shut-off automation that ramps down or pauses charging when grid import exceeds 300 W for more than 120 seconds.

Production-Ready Automation YAML (Surplus Activation)

alias: "Solar Surplus: Dynamic Load Engagement"
description: "Engages surplus charging when export power is stable above threshold."
trigger:
  - platform: numeric_state
    entity_id: sensor.solar_surplus_power
    above: 1500
    for:
      minutes: 5
condition:
  - condition: numeric_state
    entity_id: sensor.battery_state_of_charge
    below: 95
action:
  - service: number.set_value
    target:
      entity_id: number.inverter_max_charge_current
    data:
      value: >
        {% set surplus = states('sensor.solar_surplus_power') | float(0) %}
        {% set voltage = 50.0 %}
        {{ [ ((surplus * 0.9) / voltage) | round(0), 40 ] | min }}
mode: single

4. Summary & Architectural Value

What this tutorial achieves: The deployment of a deterministic, hysteresis-protected solar surplus management system in Home Assistant using custom template sensors and automated power modulation scripts.

Resulting value: Photovoltaic self-consumption rates are maximized, frequently exceeding 80% to 90%. Dependency on grid power imports during peak evening tariff periods is drastically minimized by prioritizing home battery charging and heavy household appliances during peak midday irradiance. Furthermore, hysteresis timers prevent mechanical relay wear and electrical grid oscillation caused by fluctuating cloud cover.

Lukas Wojcik

Lukas Wojcik

Systems architect and technology enthusiast specializing in scalable tracking solutions, GMP Stack (GA4 & GTM), and robust backend architectures. Advocate for clean code and privacy-first design.

Get in Touch

Briefly describe your project or inquiry for a tailored response. This site is protected by reCAPTCHA.

Write a comment

The email address is not published. Required fields are marked with an asterisk.

ALL ARTICLES & CATEGORIES

CCTV

Follow this category by RSS

Cloud & AI

Follow this category by RSS

Data Privacy

All 12 articles in this category Follow this category by RSS

Digital Analytics

All 46 articles in this category Follow this category by RSS

Digital Marketing

All 25 articles in this category Follow this category by RSS

IT & Networks

All 15 articles in this category Follow this category by RSS

Raspberry PI

Follow this category by RSS

Smart Home

All 14 articles in this category Follow this category by RSS

Web Development

Follow this category by RSS

WordPress Plugins & Tricks

Follow this category by RSS