From 39558bf906a0414d8cba62cd39cbd00f3d04e6ec Mon Sep 17 00:00:00 2001 From: sharky Date: Fri, 9 Oct 2026 20:02:24 +0200 Subject: [PATCH] =?UTF-8?q?SofarBTS-Steuerung=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SofarBTS-Steuerung | 617 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 617 insertions(+) create mode 100644 SofarBTS-Steuerung diff --git a/SofarBTS-Steuerung b/SofarBTS-Steuerung new file mode 100644 index 0000000..26e8c1e --- /dev/null +++ b/SofarBTS-Steuerung @@ -0,0 +1,617 @@ +# SOFAR BTS V4 – Home Assistant Package +# HYD 10KTL-3PH V120005 / ha-solarman v25.08.16 +# BITTE vor dem Einsatz komplette Konfiguration sichern. +# Automatische Erkennung der beiden physisch verfuegbaren Batterieanschluesse: +# ID 0 -> BMS Adresse 1; ID 1 -> BMS Adresse 17. +# Eine dritte ID (2) wurde am Wechselrichter bereits als unzulaessig abgewiesen. +# Register 4175 = konfigurierte Kapazitaet in Ah; BTS 5K = 100 Ah/Modul. +# Es gibt keine Bedienfunktion zum Aendern von Stromgrenzen. +# Technisch benoetigter FC16-Block (19 Register) sendet sie jedoch unveraendert mit. +# ACHTUNG: EOD/EPS-Schreiben ist an dieser Anlage NOCH NICHT praktisch bestaetigt. +# Dieses Paket ersetzt V1/V2/V3, niemals parallel aktivieren. + +input_boolean: + sofar_bts_turm0_erkannt: + name: SOFAR BTS BAT1 erkannt + icon: mdi:battery-check + sofar_bts_turm1_erkannt: + name: SOFAR BTS BAT2 erkannt + icon: mdi:battery-check + +input_text: + sofar_bts_turm0_reg19: + name: SOFAR BTS BAT1 Registerabbild + max: 255 + sofar_bts_turm1_reg19: + name: SOFAR BTS BAT2 Registerabbild + max: 255 + +input_datetime: + sofar_bts_turm0_zeit: + name: SOFAR BTS BAT1 letzter Abruf + has_date: true + has_time: true + sofar_bts_turm1_zeit: + name: SOFAR BTS BAT2 letzter Abruf + has_date: true + has_time: true + +# Gemeinsame Sollwerte sind lediglich Eingabefelder. Erst die jeweilige +# 'Auf beide anwenden'-Schaltflaeche sendet Aenderungen an den Wechselrichter. +input_number: + sofar_bts_alle_dod: + name: SOFAR BTS DOD fuer beide BAT + min: 1 + max: 90 + step: 1 + initial: 85 + unit_of_measurement: '%' + mode: slider + sofar_bts_alle_eod: + name: SOFAR BTS EOD fuer beide BAT + min: 1 + max: 90 + step: 1 + initial: 90 + unit_of_measurement: '%' + mode: slider + sofar_bts_alle_eps: + name: SOFAR BTS EPS fuer beide BAT + min: 5 + max: 100 + step: 1 + initial: 5 + unit_of_measurement: '%' + mode: slider + +script: + sofar_bts_gateway: + alias: SOFAR BTS – serielles Gateway V4 + description: >- + Fuer Batterie-ID 0 oder 1 Auswahl und komplette 19-Register-Lesung. + Im Modus 'erkennen' wird ungueltige Batterie-ID toleriert und Slot deaktiviert. + Nur DOD/EOD/EPS mit anschliessender Lesebestaetigung schreiben. + mode: queued + max: 30 + fields: + turm: + name: Batterie-ID + required: true + selector: + select: + options: + - label: BAT1 / ID 0 + value: '0' + - label: BAT2 / ID 1 + value: '1' + parameter: + name: Operation + required: false + selector: + select: + options: [erkennen, lesen, dod, eod, eps] + ziel: + name: Neuer Prozentwert + required: false + selector: + number: + min: 1 + max: 100 + step: 1 + mode: box + sequence: + - variables: + tid: "{{ turm | int(-1) }}" + par: "{{ parameter | default('lesen', true) }}" + z: "{{ ziel | float(-1) if ziel is defined else -1 }}" + - if: + - condition: template + value_template: "{{ tid not in [0,1] or par not in ['erkennen','lesen','dod','eod','eps'] }}" + then: + - stop: Ungueltige Batterie-ID oder Operation. Es wurde nichts geaendert. + error: true + - variables: + idx: "{{ {'dod':9,'eod':10,'eps':14}.get(par, -1) }}" + val: "{{ z | int(-1) }}" + - if: + - condition: template + value_template: >- + {{ par in ['dod','eod','eps'] and + (z != val or (par in ['dod','eod'] and not (1 <= val <= 90)) + or (par == 'eps' and not (5 <= val <= 100))) }} + then: + - stop: Sollwert ungueltig; es wurde nichts geaendert. + error: true + # Die Leseauswahl (4164, ein Wert) ist fuer ID 0 und ID 1 getestet. + # Bei Erkennung werden erwartete Fehler abgefangen. Wenn eine Auswahl + # fehlschlaegt, muss die nachfolgende Antwort ZWINGEND dieselbe ID haben. + - action: solarman.write_multiple_holding_registers + continue_on_error: true + data: + device: 9402bac3581421a90aa1b963306c0d01 + register: 4164 + values: "{{ [tid | int] }}" + - delay: + milliseconds: 750 + - action: solarman.read_holding_registers + continue_on_error: true + data: + device: 9402bac3581421a90aa1b963306c0d01 + address: 4164 + count: 19 + response_variable: vr + - variables: + regs: >- + {% set r = vr if vr is defined and vr is mapping else {} -%} + {% set ns = namespace(v=[]) -%} + {% for a in range(4164, 4183) -%} + {% set x = r.get(a, r.get(a | string, -1)) -%} + {% set ns.v = ns.v + [x | int(-1)] -%} + {% endfor -%} + {{ ns.v }} + valid: >- + {{ regs | count == 19 and regs | min >= 0 and regs | max <= 65535 + and regs[0] == tid and regs[1] == (1 if tid == 0 else 17) + and regs[2] == 11 and regs[3] == 5000 + and regs[11] >= 100 and regs[11] <= 1500 + and regs[11] % 100 == 0 + and regs[15] == 0 and regs[18] == 32 }} + - if: + - condition: template + value_template: "{{ not valid }}" + then: + - if: + - condition: template + value_template: "{{ par == 'erkennen' }}" + then: + - choose: + - conditions: "{{ tid == 0 }}" + sequence: + - action: input_boolean.turn_off + target: + entity_id: input_boolean.sofar_bts_turm0_erkannt + - action: input_text.set_value + target: + entity_id: input_text.sofar_bts_turm0_reg19 + data: + value: '[]' + default: + - action: input_boolean.turn_off + target: + entity_id: input_boolean.sofar_bts_turm1_erkannt + - action: input_text.set_value + target: + entity_id: input_text.sofar_bts_turm1_reg19 + data: + value: '[]' + - stop: Batterieeingang nicht erkannt oder Lesefehler; sicher uebersprungen. + - stop: Registerblock unvollstaendig/ungueltig. Keine Aenderung! + error: true + # Nur eine plausible Antwort mit korrekter Batterie-ID wird gespeichert. + - choose: + - conditions: "{{ tid == 0 }}" + sequence: + - action: input_text.set_value + target: + entity_id: input_text.sofar_bts_turm0_reg19 + data: + value: "{{ regs | to_json }}" + - action: input_datetime.set_datetime + target: + entity_id: input_datetime.sofar_bts_turm0_zeit + data: + datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}" + - action: input_boolean.turn_on + target: + entity_id: input_boolean.sofar_bts_turm0_erkannt + default: + - action: input_text.set_value + target: + entity_id: input_text.sofar_bts_turm1_reg19 + data: + value: "{{ regs | to_json }}" + - action: input_datetime.set_datetime + target: + entity_id: input_datetime.sofar_bts_turm1_zeit + data: + datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}" + - action: input_boolean.turn_on + target: + entity_id: input_boolean.sofar_bts_turm1_erkannt + # Bei reinen Leseaufrufen unterbleibt jeder Konfigurations-Schreibzugriff. + - if: + - condition: template + value_template: "{{ par in ['dod','eod','eps'] and regs[idx] != val }}" + then: + # Ersetze ausschliesslich den gewuenschten Wert und das Commit-Flag. + # Insbesondere 4171/4172: GAR KEINE eigenstaendige Einstelloption. + - variables: + neu: "{{ regs[:idx] + [val | int] + regs[idx+1:15] + [1] + regs[16:] }}" + - action: solarman.write_multiple_holding_registers + data: + device: 9402bac3581421a90aa1b963306c0d01 + register: 4164 + values: "{{ neu }}" + - delay: + seconds: 2 + - action: solarman.write_multiple_holding_registers + data: + device: 9402bac3581421a90aa1b963306c0d01 + register: 4164 + values: "{{ [tid | int] }}" + - delay: + milliseconds: 750 + - action: solarman.read_holding_registers + data: + device: 9402bac3581421a90aa1b963306c0d01 + address: 4164 + count: 19 + response_variable: nr + - variables: + rueck: >- + {% set r = nr if nr is defined and nr is mapping else {} -%} + {% set ns = namespace(v=[]) -%} + {% for a in range(4164, 4183) -%} + {% set x = r.get(a, r.get(a | string, -1)) -%} + {% set ns.v = ns.v + [x | int(-1)] -%} + {% endfor -%} + {{ ns.v }} + - if: + - condition: template + value_template: >- + {{ rueck | count != 19 or rueck | min < 0 or + rueck[0] != tid or + (rueck[:15] + rueck[16:]) != (neu[:15] + neu[16:]) or + rueck[15] != 0 }} + then: + - stop: Nachpruefung fehlgeschlagen! Nicht erneut schreiben. + error: true + - choose: + - conditions: "{{ tid == 0 }}" + sequence: + - action: input_text.set_value + target: + entity_id: input_text.sofar_bts_turm0_reg19 + data: + value: "{{ rueck | to_json }}" + - action: input_datetime.set_datetime + target: + entity_id: input_datetime.sofar_bts_turm0_zeit + data: + datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}" + default: + - action: input_text.set_value + target: + entity_id: input_text.sofar_bts_turm1_reg19 + data: + value: "{{ rueck | to_json }}" + - action: input_datetime.set_datetime + target: + entity_id: input_datetime.sofar_bts_turm1_zeit + data: + datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}" + + sofar_bts_erkennen: + alias: SOFAR BTS – Anschluesse erkennen und Einstellungen aktualisieren + icon: mdi:refresh + mode: single + sequence: + # Der WR hat nur BAT1 und BAT2. ID 2 wurde bereits als ungueltig bestaetigt. + - action: script.sofar_bts_gateway + data: {turm: 0, parameter: erkennen} + - action: script.sofar_bts_gateway + data: {turm: 1, parameter: erkennen} + + sofar_bts_aktualisieren: + alias: SOFAR BTS – bekannte BAT aktualisieren + icon: mdi:refresh + mode: single + sequence: + - if: + - condition: state + entity_id: input_boolean.sofar_bts_turm0_erkannt + state: 'on' + then: + - action: script.sofar_bts_gateway + data: {turm: 0, parameter: lesen} + - if: + - condition: state + entity_id: input_boolean.sofar_bts_turm1_erkannt + state: 'on' + then: + - action: script.sofar_bts_gateway + data: {turm: 1, parameter: lesen} + + sofar_bts_alle_dod_anwenden: + alias: SOFAR BTS – DOD auf alle erkannten BAT + icon: mdi:battery-lock + mode: single + sequence: + - variables: + ziel: "{{ states('input_number.sofar_bts_alle_dod') | int(-1) }}" + - if: + - condition: template + value_template: >- + {{ not is_state('input_boolean.sofar_bts_turm0_erkannt','on') + and not is_state('input_boolean.sofar_bts_turm1_erkannt','on') }} + then: + - stop: Es wurde kein Batterieeingang erkannt. + error: true + - if: + - condition: state + entity_id: input_boolean.sofar_bts_turm0_erkannt + state: 'on' + then: + - action: script.sofar_bts_gateway + data: {turm: 0, parameter: dod, ziel: "{{ ziel }}"} + - if: + - condition: state + entity_id: input_boolean.sofar_bts_turm1_erkannt + state: 'on' + then: + - action: script.sofar_bts_gateway + data: {turm: 1, parameter: dod, ziel: "{{ ziel }}"} + + sofar_bts_alle_eod_anwenden: + alias: SOFAR BTS – EOD auf alle erkannten BAT + icon: mdi:battery-lock + mode: single + sequence: + - variables: + ziel: "{{ states('input_number.sofar_bts_alle_eod') | int(-1) }}" + - if: + - condition: template + value_template: >- + {{ not is_state('input_boolean.sofar_bts_turm0_erkannt','on') + and not is_state('input_boolean.sofar_bts_turm1_erkannt','on') }} + then: + - stop: Es wurde kein Batterieeingang erkannt. + error: true + - if: + - condition: state + entity_id: input_boolean.sofar_bts_turm0_erkannt + state: 'on' + then: + - action: script.sofar_bts_gateway + data: {turm: 0, parameter: eod, ziel: "{{ ziel }}"} + - if: + - condition: state + entity_id: input_boolean.sofar_bts_turm1_erkannt + state: 'on' + then: + - action: script.sofar_bts_gateway + data: {turm: 1, parameter: eod, ziel: "{{ ziel }}"} + + sofar_bts_alle_eps_anwenden: + alias: SOFAR BTS – EPS-Puffer auf alle erkannten BAT + icon: mdi:battery-lock + mode: single + sequence: + - variables: + ziel: "{{ states('input_number.sofar_bts_alle_eps') | int(-1) }}" + - if: + - condition: template + value_template: >- + {{ not is_state('input_boolean.sofar_bts_turm0_erkannt','on') + and not is_state('input_boolean.sofar_bts_turm1_erkannt','on') }} + then: + - stop: Es wurde kein Batterieeingang erkannt. + error: true + - if: + - condition: state + entity_id: input_boolean.sofar_bts_turm0_erkannt + state: 'on' + then: + - action: script.sofar_bts_gateway + data: {turm: 0, parameter: eps, ziel: "{{ ziel }}"} + - if: + - condition: state + entity_id: input_boolean.sofar_bts_turm1_erkannt + state: 'on' + then: + - action: script.sofar_bts_gateway + data: {turm: 1, parameter: eps, ziel: "{{ ziel }}"} + +# Beim Start Erkennung (auch nach Modul-Erweiterung); im Betrieb werden nur +# bereits erkannte Eingaenge regelmaessig aktualisiert. Manueller Button +# 'Erkennen/Aktualisieren' prueft auch bisher fehlende Eingaenge erneut. +automation: + - id: sofar_bts_v4_start + alias: SOFAR BTS V4 – nach HA-Start erkennen + triggers: + - trigger: homeassistant + event: start + actions: + - delay: + seconds: 30 + - action: script.sofar_bts_erkennen + - id: sofar_bts_v4_zyklus + alias: SOFAR BTS V4 – alle 10 Minuten lesen + triggers: + - trigger: time_pattern + minutes: '/10' + actions: + - action: script.sofar_bts_aktualisieren +# Anzeige der erkannter Module/Tuerme, unabhaengig von der Solarman- +# Standard-Entitaet 'Battery DOD', die jeweils die zuletzt gewaehlte ID zeigt. +template: + - sensor: + - name: SOFAR BTS erkannte Tuerme + unique_id: sofar_bts_v4_erkannte_tuerme + default_entity_id: sensor.sofar_bts_erkannte_tuerme + icon: mdi:battery-multiple + state: >- + {{ (1 if is_state('input_boolean.sofar_bts_turm0_erkannt','on') else 0) + + (1 if is_state('input_boolean.sofar_bts_turm1_erkannt','on') else 0) }} + - name: SOFAR BTS Module gesamt + unique_id: sofar_bts_v4_module_gesamt + default_entity_id: sensor.sofar_bts_module_gesamt + icon: mdi:battery-multiple + state: >- + {% set a = states('input_text.sofar_bts_turm0_reg19') | from_json([]) %} + {% set b = states('input_text.sofar_bts_turm1_reg19') | from_json([]) %} + {{ (a[11] // 100 if is_state('input_boolean.sofar_bts_turm0_erkannt','on') and a | count == 19 else 0) + + (b[11] // 100 if is_state('input_boolean.sofar_bts_turm1_erkannt','on') and b | count == 19 else 0) }} + - name: SOFAR BTS BAT1 Module + unique_id: sofar_bts_v4_turm0_module + default_entity_id: sensor.sofar_bts_turm0_module + icon: mdi:battery + availability: "{{ is_state('input_boolean.sofar_bts_turm0_erkannt','on') and (states('input_text.sofar_bts_turm0_reg19') | from_json([])) | count == 19 }}" + state: >- + {% set x = states('input_text.sofar_bts_turm0_reg19') | from_json([]) %} + {{ x[11] // 100 if x | count == 19 else none }} + - name: SOFAR BTS BAT1 Kapazitaet Ah + unique_id: sofar_bts_v4_turm0_kap_ah + default_entity_id: sensor.sofar_bts_turm0_kap_ah + unit_of_measurement: Ah + availability: "{{ is_state('input_boolean.sofar_bts_turm0_erkannt','on') and (states('input_text.sofar_bts_turm0_reg19') | from_json([])) | count == 19 }}" + state: >- + {% set x = states('input_text.sofar_bts_turm0_reg19') | from_json([]) %} + {{ x[11] if x | count == 19 else none }} + - name: SOFAR BTS BAT1 Mindest SOC + unique_id: sofar_bts_v4_turm0_min_soc + default_entity_id: sensor.sofar_bts_turm0_min_soc + unit_of_measurement: '%' + availability: "{{ is_state('input_boolean.sofar_bts_turm0_erkannt','on') and (states('input_text.sofar_bts_turm0_reg19') | from_json([])) | count == 19 }}" + state: >- + {% set x = states('input_text.sofar_bts_turm0_reg19') | from_json([]) %} + {{ 100 - x[9] if x | count == 19 else none }} + - name: SOFAR BTS BAT2 Module + unique_id: sofar_bts_v4_turm1_module + default_entity_id: sensor.sofar_bts_turm1_module + icon: mdi:battery + availability: "{{ is_state('input_boolean.sofar_bts_turm1_erkannt','on') and (states('input_text.sofar_bts_turm1_reg19') | from_json([])) | count == 19 }}" + state: >- + {% set x = states('input_text.sofar_bts_turm1_reg19') | from_json([]) %} + {{ x[11] // 100 if x | count == 19 else none }} + - name: SOFAR BTS BAT2 Kapazitaet Ah + unique_id: sofar_bts_v4_turm1_kap_ah + default_entity_id: sensor.sofar_bts_turm1_kap_ah + unit_of_measurement: Ah + availability: "{{ is_state('input_boolean.sofar_bts_turm1_erkannt','on') and (states('input_text.sofar_bts_turm1_reg19') | from_json([])) | count == 19 }}" + state: >- + {% set x = states('input_text.sofar_bts_turm1_reg19') | from_json([]) %} + {{ x[11] if x | count == 19 else none }} + - name: SOFAR BTS BAT2 Mindest SOC + unique_id: sofar_bts_v4_turm1_min_soc + default_entity_id: sensor.sofar_bts_turm1_min_soc + unit_of_measurement: '%' + availability: "{{ is_state('input_boolean.sofar_bts_turm1_erkannt','on') and (states('input_text.sofar_bts_turm1_reg19') | from_json([])) | count == 19 }}" + state: >- + {% set x = states('input_text.sofar_bts_turm1_reg19') | from_json([]) %} + {{ 100 - x[9] if x | count == 19 else none }} + - number: + - name: SOFAR BTS BAT1 DOD + unique_id: sofar_bts_v4_turm0_dod + default_entity_id: number.sofar_bts_turm0_dod + icon: mdi:battery-cog + unit_of_measurement: '%' + min: 1 + max: 90 + step: 1 + optimistic: false + availability: "{{ is_state('input_boolean.sofar_bts_turm0_erkannt','on') and (states('input_text.sofar_bts_turm0_reg19') | from_json([])) | count == 19 }}" + state: >- + {% set x = states('input_text.sofar_bts_turm0_reg19') | from_json([]) %} + {{ x[9] if x | count == 19 else none }} + set_value: + - action: script.sofar_bts_gateway + data: + turm: 0 + parameter: dod + ziel: "{{ value }}" + - name: SOFAR BTS BAT1 EOD + unique_id: sofar_bts_v4_turm0_eod + default_entity_id: number.sofar_bts_turm0_eod + icon: mdi:battery-cog + unit_of_measurement: '%' + min: 1 + max: 90 + step: 1 + optimistic: false + availability: "{{ is_state('input_boolean.sofar_bts_turm0_erkannt','on') and (states('input_text.sofar_bts_turm0_reg19') | from_json([])) | count == 19 }}" + state: >- + {% set x = states('input_text.sofar_bts_turm0_reg19') | from_json([]) %} + {{ x[10] if x | count == 19 else none }} + set_value: + - action: script.sofar_bts_gateway + data: + turm: 0 + parameter: eod + ziel: "{{ value }}" + - name: SOFAR BTS BAT1 EPS + unique_id: sofar_bts_v4_turm0_eps + default_entity_id: number.sofar_bts_turm0_eps + icon: mdi:battery-cog + unit_of_measurement: '%' + min: 5 + max: 100 + step: 1 + optimistic: false + availability: "{{ is_state('input_boolean.sofar_bts_turm0_erkannt','on') and (states('input_text.sofar_bts_turm0_reg19') | from_json([])) | count == 19 }}" + state: >- + {% set x = states('input_text.sofar_bts_turm0_reg19') | from_json([]) %} + {{ x[14] if x | count == 19 else none }} + set_value: + - action: script.sofar_bts_gateway + data: + turm: 0 + parameter: eps + ziel: "{{ value }}" + - name: SOFAR BTS BAT2 DOD + unique_id: sofar_bts_v4_turm1_dod + default_entity_id: number.sofar_bts_turm1_dod + icon: mdi:battery-cog + unit_of_measurement: '%' + min: 1 + max: 90 + step: 1 + optimistic: false + availability: "{{ is_state('input_boolean.sofar_bts_turm1_erkannt','on') and (states('input_text.sofar_bts_turm1_reg19') | from_json([])) | count == 19 }}" + state: >- + {% set x = states('input_text.sofar_bts_turm1_reg19') | from_json([]) %} + {{ x[9] if x | count == 19 else none }} + set_value: + - action: script.sofar_bts_gateway + data: + turm: 1 + parameter: dod + ziel: "{{ value }}" + - name: SOFAR BTS BAT2 EOD + unique_id: sofar_bts_v4_turm1_eod + default_entity_id: number.sofar_bts_turm1_eod + icon: mdi:battery-cog + unit_of_measurement: '%' + min: 1 + max: 90 + step: 1 + optimistic: false + availability: "{{ is_state('input_boolean.sofar_bts_turm1_erkannt','on') and (states('input_text.sofar_bts_turm1_reg19') | from_json([])) | count == 19 }}" + state: >- + {% set x = states('input_text.sofar_bts_turm1_reg19') | from_json([]) %} + {{ x[10] if x | count == 19 else none }} + set_value: + - action: script.sofar_bts_gateway + data: + turm: 1 + parameter: eod + ziel: "{{ value }}" + - name: SOFAR BTS BAT2 EPS + unique_id: sofar_bts_v4_turm1_eps + default_entity_id: number.sofar_bts_turm1_eps + icon: mdi:battery-cog + unit_of_measurement: '%' + min: 5 + max: 100 + step: 1 + optimistic: false + availability: "{{ is_state('input_boolean.sofar_bts_turm1_erkannt','on') and (states('input_text.sofar_bts_turm1_reg19') | from_json([])) | count == 19 }}" + state: >- + {% set x = states('input_text.sofar_bts_turm1_reg19') | from_json([]) %} + {{ x[14] if x | count == 19 else none }} + set_value: + - action: script.sofar_bts_gateway + data: + turm: 1 + parameter: eps + ziel: "{{ value }}"