# Fronius Solar-API "Basic" Integration by gapsed.com
# Bitte einfügen in /gapsed/
# Passe ggf. die Entity-Namen unten (sensor.solarnet_...) an deine an
# Gebe die IP deines Wechselrichters unter rest: ein

template:
  - sensor:
      - name: "Speicher Ladeleistung"
        unique_id: speicher_ladeleistung
        device_class: power
        state_class: measurement
        unit_of_measurement: W
        icon: mdi:battery-charging
        state: "{{ max(0, states('sensor.solarnet_ladeleistung') | float(0)) }}" #prüfe Entity-Name

      - name: "Speicher Entladeleistung"
        unique_id: speicher_entladeleistung
        device_class: power
        state_class: measurement
        unit_of_measurement: W
        icon: mdi:battery-arrow-down
        state: "{{ max(0, states('sensor.solarnet_entladeleistung') | float(0)) }}" #prüfe Entity-Name

      - name: "PV Leistung"
        unique_id: pv_leistung
        device_class: power
        state_class: measurement
        unit_of_measurement: W
        icon: mdi:solar-power
        state: "{{ states('sensor.solarnet_pv_leistung') | float(0) }}" #prüfe Entity-Name

sensor:
  - platform: integration
    source: sensor.speicher_ladeleistung
    name: "Energie Speicher (Ladung)"
    unique_id: energie_speicher_ladung
    unit_prefix: k
    unit_time: h
    round: 3
    method: left

  - platform: integration
    source: sensor.speicher_entladeleistung
    name: "Energie Speicher (Entladung)"
    unique_id: energie_speicher_entladung
    unit_prefix: k
    unit_time: h
    round: 3
    method: left

  - platform: integration
    source: sensor.pv_leistung
    name: "PV Energie gesamt"
    unique_id: pv_energie_gesamt
    unit_prefix: k
    unit_time: h
    round: 3
    method: left

rest:
  - resource: "http://IP_WECHSELRICHTER/components/readable"
    method: GET
    scan_interval: 60
    timeout: 10
    sensor:
      # SOH direkt vom BYD
      - name: "Speicher SOH"
        unique_id: byd_hv_soh
        unit_of_measurement: "%"
        value_template: >-
          {% set dev = (value_json.Body.Data.values()
                        | selectattr('attributes.manufacturer', 'equalto', 'BYD')
                        | list
                        | first) %}
          {{ dev.channels.BAT_VALUE_STATE_OF_HEALTH_RELATIVE_U16 | float }}

      # Nennkapazität NUR aus nameplate.capacity_wh
      - name: "Speicher Nennkapazität"
        unique_id: byd_hv_nominal_capacity_kwh
        unit_of_measurement: "kWh"
        value_template: >-
          {% set dev = (value_json.Body.Data.values()
                        | selectattr('attributes.manufacturer', 'equalto', 'BYD')
                        | list
                        | first) %}
          {% set np = dev.attributes.nameplate | from_json %}
          {{ (np.capacity_wh | float / 1000) | round(2) }}

      # Realkapazität = Nennkapazität * SOH
      - name: "Speicher Realkapazität"
        unique_id: byd_hv_real_capacity_kwh
        unit_of_measurement: "kWh"
        value_template: >-
          {% set dev = (value_json.Body.Data.values()
                        | selectattr('attributes.manufacturer', 'equalto', 'BYD')
                        | list
                        | first) %}
          {% set np = dev.attributes.nameplate | from_json %}
          {% set cap = np.capacity_wh | float / 1000 %}
          {% set soh = dev.channels.BAT_VALUE_STATE_OF_HEALTH_RELATIVE_U16 | float %}
          {{ (cap * soh / 100) | round(2) }}
