Skip to content

SMA Sunny Island: allow PV charging in hold mode - #36279

Draft
mb4711 wants to merge 24 commits into
evcc-io:masterfrom
mb4711:master
Draft

mb4711 wants to merge 24 commits into
evcc-io:masterfrom
mb4711:master

Conversation

@mb4711

@mb4711 mb4711 commented Nov 15, 2026 •

Copy link
Copy Markdown

fixes #36020, depends on #36548

Hold should stop discharging but let PV keep charging the battery. The Sunny Island template set a fixed 0 W setpoint (40149 WSpt = 0, 40151 = 802) instead, which also stopped PV charging and sent the surplus to the grid.

Hold now uses the BMS power limits instead of an active power setpoint, since the approach from #36038 is not supported on older Sunny Island generations.

  • hold: 40799 BatDschMaxW = 0 first, then 40795 BatChaMaxW = charge limit, then 40151 = 803 (setpoint off). No discharge, PV keeps charging
  • normal: 40151 = 803 and restores 40795/40799 to the charge/discharge limits, so the hold limit doesn't stick
  • charge: setpoint is the charge limit, 40799 is restored to the discharge limit
  • maxchargepower/maxdischargepower are optional and suggested from the device maximum (40189 BatConv.WMaxCha / 40191 BatConv.WMaxDsch) in the UI. Left empty, the device maximum is used via the fallback plugin (Plugin: add fallback source for device-provided defaults #36548), read once, both for the optimizer and for the registers above. The previous 4200 W default is gone
  • The fallback lives in a new sma-modbus.tpl include, also used by the other SMA battery templates in SMA: device maximum as battery limit fallback, unify SBS and hybrid battery control #36469. Includes the plugin commit from Plugin: add fallback source for device-provided defaults #36548 until that is merged
  • 40795/40799 are volatile on the Sunny Island and fall back to defaults after 180 s

Tested on SI 6.0H-11 and 6.0H-12. Not tested on generation 13.

TODO

  • Retest on hardware with empty and with configured power limits

🤖 Generated with Claude Code

Update on Battery hold/charge/normal
Use DC side control instead of active power values, because not supported on older SI (Gen 11 und 12)
Should also work on Gen 13 - not tested
- Fix calculation
- added discharge value
verified SI Gen 13 compatibility with SMA Modbus SPEC MODBUS-HTML_SI44M-80H-13_34003R_V10
removed unnecessary stuff
convert value in charge section to int after mulf which results in float
@github-actions github-actions Bot added bug Something isn't working devices Specific device support labels Nov 15, 2026

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 6 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="templates/definition/meter/sma-si-modbus.yaml" line_range="121" />
<code_context>
+              source: modbus
+              {{- include "modbus" . | indent 12 }}
+              register:
+                address: 40799 # Maximale Batterieentladeleistung
+                type: writemultiple
+                decode: uint32
</code_context>
<issue_to_address>
**Battery keeps discharging in hold**

When hold is requested while the battery is discharging, the sequence disables the setpoint before zeroing register 40799, leaving the previous discharge limit active in the meantime. If an earlier write fails, the sequence stops before 40799 is zeroed, so the battery continues discharging.

Write zero to register 40799 before disabling the setpoint or applying the other hold-sequence writes.
</issue_to_address>

### Comment 2
<location path="templates/definition/meter/sma-si-modbus.yaml" line_range="129" />
<code_context>
           set:
           - source: const
-            value: -{{ .maxchargepower }} # Wirkleistungsvorgabe
+            value: -{{ mulf .maxchargepower 1.1 | int64 }} # Wirkleistungsvorgabe ---> SunnyIsland berechnet intern die max Wirkleistung via Ampere. 
+                                               # im Falle des SI6.0H-1x sind das exemplarisch 20A, also 4600/230 = 20A. Der Max Wert ergibt sich aus dem maximal zulässigen 
+                                               # Spannung im Europäischen Stromnetz (+/-10%), somit kann auf den Max Wert des Batteriestellers 10% aufgeschlagen werden 
</code_context>
<issue_to_address>
**Charge boost remains DC-capped**

When the charge mode is entered after normal mode and the Sunny Island enforces 40795 as its maximum battery charging power, when charge follows normal, register 40795 remains capped at `maxchargepower`, while the charge case only raises the AC setpoint at 40149 to 1.1 times that value and does not update 40795. The stated 10% increase therefore cannot increase the battery charging power above the configured cap.

If the 10% increase is intended to raise battery charging power, update the 40795 limit consistently in charge mode, while preserving any required battery safety limit.

Also at `templates/definition/meter/sma-si-modbus.yaml:76`.
</issue_to_address>

### Comment 3
<location path="templates/definition/meter/sma-si-modbus.yaml" line_range="22" />
<code_context>
   - preset: battery-params
-  - name: maxchargepower
+  - name: maxchargepower  # could be read at 40189 on on SI Gen 11, 12 and 13. Don't know how to implemet it here yet
+    default: 4200
+    required: true
+  - name: maxdischargepower  # async value, depending on battery, could also be read at 40191 on SI Gen 11, 12 and 13. Don't know how to implemet it here yet
</code_context>
<issue_to_address>
**Higher-rated batteries are capped**

When a Sunny Island supports more than 4200 W and the user leaves the defaults unchanged, normal mode writes the 4200 W defaults to the battery charge and discharge limits, capping the unit at 4200 W.

Choose defaults that do not impose a 4200 W cap on higher-rated units.

Also at `templates/definition/meter/sma-si-modbus.yaml:25`, `templates/definition/meter/sma-si-modbus.yaml:76-86`.
</issue_to_address>

### Comment 4
<location path="templates/definition/meter/sma-si-modbus.yaml" line_range="129-133" />
<code_context>
+            value: -{{ mulf .maxchargepower 1.1 | int64 }} # Wirkleistungsvorgabe ---> SunnyIsland berechnet intern die max Wirkleistung via Ampere. 
</code_context>
<issue_to_address>
**Charge explanation exceeds comment limit**

When maintainers read the charge-case setpoint comment, the five-line explanation attached to the charge setpoint exceeds the repository’s one-to-two-line limit for comments, making the rationale harder to scan and maintain.

Condense the rationale to at most two lines while retaining the key constraint.
</issue_to_address>

### Comment 5
<location path="templates/definition/meter/sma-si-modbus.yaml" line_range="63" />
<code_context>
       switch:
       - case: 1 # normal
         set:
-          source: const
-          value: 803 # inaktiv (Ina)
</code_context>
<issue_to_address>
**issue (review_instructions):** This duplicates the `set` key and makes the sequence structure ambiguous.

There are two consecutive `set:` keys under `source: sequence` here. Please remove the extra key so the template has an unambiguous structure.

<details>
<summary>Review instructions:</summary>

**Path patterns:** `templates/**/*.yaml`

**Instructions:**
Verify that the changes comply to the rules defined in templates/README.md. Also check consistence (wording) with other files inside templates directory.

[Edit this rule](https://app.sourcery.ai/dashboard/review-settings?connection=github&owner=evcc-io&from_surface=github_bot&intent=review_rules&activeTab=review-rules&rule=74ec55f5-149d-4827-9376-6dccef98d305)

</details>
</issue_to_address>

### Comment 6
<location path="templates/definition/meter/sma-si-modbus.yaml" line_range="21" />
<code_context>
       de: Der ModbusTCP Server muss aktiviert sein.
   - preset: battery-params
-  - name: maxchargepower
+  - name: maxchargepower  # could be read at 40189 on on SI Gen 11, 12 and 13. Don't know how to implemet it here yet
+    default: 4200
+    required: true
</code_context>
<issue_to_address>
**nitpick (review_instructions):** The new comment contains the typo “implemet.”

Please correct the spelling to “implement” to keep the added wording consistent and polished.

<details>
<summary>Review instructions:</summary>

**Path patterns:** `templates/**/*.yaml`

**Instructions:**
Also check consistence (wording) with other files inside templates directory.

[Edit this rule](https://app.sourcery.ai/dashboard/review-settings?connection=github&owner=evcc-io&from_surface=github_bot&intent=review_rules&activeTab=review-rules&rule=74ec55f5-149d-4827-9376-6dccef98d305)

</details>
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨

@andig

andig commented Nov 15, 2026

Copy link
Copy Markdown
Member

There's already #36038?

@mb4711

mb4711 commented Nov 15, 2026

Copy link
Copy Markdown
Author

@andig It wasn't allowed to add my change to PR #36038 by Github because I'm not the owner. Thats why I created a new PR.

@andig

andig commented Nov 15, 2026

Copy link
Copy Markdown
Member

Ah, closed the other one.

@premultiply premultiply left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this and for testing on real hardware. The core idea (803 + blocking discharge via 40799 = 0 in hold) is right and fixes #36020. A few things need to change before merge:

  1. normal now hard-limits the device to the evcc params. Previously normal only wrote 803. Now it also writes maxchargepower/maxdischargepower into 40795/40799. These params are meant as optimizer planning values, so users who set them conservatively will now be throttled. On top of that, the new maxdischargepower default of 4200 W silently caps discharge on SI 8.0H or clusters. In normal, please restore the device limits (e.g. the device maximum from 40189/40191) or a non-restricting value instead.

  2. Please drop the mulf .maxchargepower 1.1 change from this PR. It is unrelated to the fix. maxchargepower is the power the user asked for, so the template shouldn't add 10% on top. Users who want more can raise the value. It is also inconsistent: 40795 stays at maxchargepower from normal/hold, so if the SI honours it in setpoint mode the boost has no effect. The five-line comment is also too long for our comment style. If you want to pursue this, please open a separate PR.

  3. Write 40799 = 0 first in hold. Writing 803 first means the SI can briefly discharge after charge, and if a later write fails, discharge stays open.

  4. Are 40795/40799 volatile on the Sunny Island? The watchdog rewrites the whole sequence every timeout/2 (30s). This is documented as fine for SBS, but please confirm these registers aren't persisted to EEPROM on the SI.

  5. charge leaves 40799/40795 as they were. After hold, 40799 = 0 sticks during charge. Probably harmless for forced charging, but the state now depends on the previous mode. Please make that a deliberate choice.

  6. Cleanup:

    • Remove the comments on the params ("Don't know how to implemet it here yet", "on on").
    • required: true is redundant with a default.
    • Please rename the PR to e.g. SMA Sunny Island: allow PV charging in hold mode.

The red UI check is unrelated: tests pass, and make ui fails with git describe because there are no tags on the fork branch.

🤖 Generated with Claude Code

@premultiply

Copy link
Copy Markdown
Member

Please also use the descriptions from the SMA Modbus Profile documentation.

@sourcery-ai

sourcery-ai Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Linked issues

Checked at commit 3a6ba1e

#36020 Optimizer issue: SMA Sunny Island stops charging in battery hold mode · 1 of 1 met

# Requirement Status Notes
1 Stop battery discharge in hold mode while allowing PV to charge the battery. ✅ Met The hold sequence sets the discharge limit (40799) to zero, restores the charge limit (40795), then disables the active-power setpoint (40151 = 803).

- added helper function to read 40189/40191 and write it back to 40795/40799 
- added same function to reset discharge if change from hold to charge
- cleanup code
changed sequence of register writing
@mb4711

mb4711 commented Nov 18, 2026

Copy link
Copy Markdown
Author
  1. normal now hard-limits the device to the evcc params. Previously normal only wrote 803. Now it also writes maxchargepower/maxdischargepower into 40795/40799. These params are meant as optimizer planning values, so users who set them conservatively will now be throttled. On top of that, the new maxdischargepower default of 4200 W silently caps discharge on SI 8.0H or clusters. In normal, please restore the device limits (e.g. the device maximum from 40189/40191) or a non-restricting value instead.

Updated to read device maximum from 40189/40191 and write to 40795/40799

  1. Please drop the mulf .maxchargepower 1.1 change from this PR. It is unrelated to the fix. maxchargepower is the power the user asked for, so the template shouldn't add 10% on top. Users who want more can raise the value. It is also inconsistent: 40795 stays at maxchargepower from normal/hold, so if the SI honours it in setpoint mode the boost has no effect. The five-line comment is also too long for our comment style. If you want to pursue this, please open a separate PR.
  2. Write 40799 = 0 first in hold. Writing 803 first means the SI can briefly discharge after charge, and if a later write fails, discharge stays open.

Change the order of writing the register and removed the mulf and restored origin.

  1. Are 40795/40799 volatile on the Sunny Island? The watchdog rewrites the whole sequence every timeout/2 (30s). This is documented as fine for SBS, but please confirm these registers aren't persisted to EEPROM on the SI.

Same for SI, the register are volatile and will be fall back to default after 180sec. Working with it für more than 6 years in an older project for ioBroker.

  1. charge leaves 40799/40795 as they were. After hold, 40799 = 0 sticks during charge. Probably harmless for forced charging, but the state now depends on the previous mode. Please make that a deliberate choice.

As mentioned not really necessary, will be fall back to default after 180s but added for clean exit.

  1. Cleanup:

Done

Please also use the descriptions from the SMA Modbus Profile documentation.

Done

@mb4711 mb4711 changed the title add PV charge in hold mode on SMA SunnyIsland SMA Sunny Island: allow PV charging in hold mode Nov 18, 2026
@mb4711
mb4711 requested a review from premultiply November 20, 2026 15:26
@mb4711

mb4711 commented Nov 20, 2026

Copy link
Copy Markdown
Author

Tested the change for 2 days now. Works perfect on my Hardware

@andig

andig commented Nov 20, 2026

Copy link
Copy Markdown
Member

@premultiply good to merge?

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Firmware-dependent register sequencing, hold/charge/normal transitions, and watchdog recovery still require hardware validation, while Sunny Island generation 13 compatibility remains untested.

No open issues 🎉

What changed in this PR

Updates the SMA Sunny Island meter integration so battery hold mode blocks discharge while allowing PV charging, addressing #36020 without relying on registers unsupported by older generations.

Changes:

  • Replaces hold mode’s fixed 0 W setpoint with battery power limits.
  • Restores power limits when returning to normal or forced-charge operation.
  • Requires explicit maximum charge and discharge power values, with device-based prefilling in the UI. Existing YAML configurations must supply both values.
File Description
templates/​definition/​meter/​sma-si-modbus.yaml Updates battery-mode register writes and adds required power-limit parameters with device read services.

🤖 Generated with GitHub Copilot

Review effort: Balanced

@andig

andig commented Nov 28, 2026

Copy link
Copy Markdown
Member

Pls make the params explicit

@andig
andig marked this pull request as draft November 28, 2026 07:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working devices Specific device support

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Optimizer issue: SMA Sunny Island stops charging in battery hold mode

4 participants