Simple PID Controller

1. Install the Integration

First, install the Simple PID Controller in Home Assistant:

  • Go to Settings > Devices & Services
  • Click Add Integration → Search for Simple PID Controller

2. Initial Configuration

When setting up the integration, enter:

  • Name: Battery Grid Charge Controller
  • Input Sensor Entity: sensor.power_import
  • Input Range Min: -10 (your sensor’s minimum)
  • Input Range Max: 30 (your sensor’s maximum)
  • Output Range Min: 0 (minimum charging current)
  • Output Range Max: 30 (maximum charging current)

3. Configure PID Parameters

After setup, go to Settings > Devices & Services → Find your new controller → Click Configure or adjust the entities:

Critical Settings:

  • Setpoint: 16 (kW - your target grid import)
  • Kp: -2.0 (negative for inverse control)
  • Ki: -0.2 (negative for inverse control)
  • Kd: -0.5 (negative for inverse control)
  • Sample Time: 5 seconds (fast response for load changes)
  • Output Min: 0 A
  • Output Max: 30 A
  • Auto Mode: ON
  • Windup Protection: ON (prevents integral windup)

4. Why Negative Gains?

This is an inverse control problem:

  • When grid import increases (approaches 17 kW) → reduce battery charging
  • When grid import decreases (below 16 kW) → increase battery charging

Using negative Kp, Ki, and Kd inverts the controller action to achieve this behavior.

5. Create Automation to Apply Output

The PID controller creates a sensor (e.g., sensor.battery_grid_charge_controller_pid_output) that outputs the calculated charging current. You need an automation to apply this to your battery:

automation:
  - alias: "Apply PID Battery Charging Control"
    trigger:
      - platform: state
        entity_id: sensor.battery_grid_charge_controller_pid_output
    action:
      - service: number.set_value
        target:
          entity_id: number.max_battery_grid_charge_current
        data:
          value: "{{ states('sensor.battery_grid_charge_controller_pid_output') | float }}"

6. Enable Diagnostic Sensors (for tuning)

Go to Settings > Devices & Services → Your controller → Enable these sensors:

  • PID P Contribution
  • PID I Contribution
  • PID D Contribution
  • Error

These help you monitor how the controller responds to your loads.

7. Fine-Tuning

Start with the suggested values and observe:

  • If the controller is too aggressive (oscillates): Reduce the magnitude of Kp, increase Kd
  • If the controller is too slow to respond: Increase the magnitude of Kp
  • If there’s steady-state error (settles above/below 16 kW): Increase the magnitude of Ki
  • If loads change very frequently: Consider increasing Sample Time to 10-15 seconds

8. Safety Considerations

  • The controller will try to keep grid import at 16 kW, but sudden large loads may briefly exceed 17 kW
  • Consider setting Output Max to something like 28 A instead of 30 A to leave a safety margin
  • Monitor the system for a few days and adjust as needed

Expected Behavior

With this setup:

  • Grid import at 14 kW → PID increases charging current
  • Grid import at 16 kW → PID holds steady
  • Grid import at 18 kW → PID reduces charging current
  • Large sudden load (e.g., +5 kW) → PID quickly reduces battery charging to compensate
3 Likes