# Full EVCC config — Schön Consulting homelab
#
# Includes: grid meter, 3 PV inverters, 2 batteries, custom charger via HA,
# site config, Tibber tariff.
#
# Use this as a reference when modifying the EVCC config. Replace __HA_TOKEN__
# with a long-lived HA access token.
#
# Installation: Stop HA VM (qm stop 106), mount RBD disk, write to
# /mnt/haos/supervisor/homeassistant/evcc.yaml, unmount, start VM.
# Then restart EVCC add-on via Supervisor API.
#
# WARNING: EVCC add-on overwrites this file on restart. Stop VM, edit, start VM.

network:
  schema: http
  host: 0.0.0.0
  port: 7070

log: info
interval: 30s

# ─── METERS (grid, PV, batteries) ───────────────────────────

meters:
  # Grid meter — SMA Energy Meter via Modbus
  - name: grid
    type: custom
    power:
      source: modbus
      id: 3
      uri: 10.0.50.201:502
      register:
        address: 30865
        type: input
        decode: int32nan
    energy:
      source: modbus
      id: 3
      uri: 10.0.50.201:502
      register:
        address: 30529
        type: input
        decode: uint32nan
      scale: 0.001

  # PV inverter 1 — SMA via Modbus
  - name: pv1
    type: template
    template: sma-inverter-modbus
    usage: pv
    modbus: tcpip
    host: 10.0.50.201
    port: 502

  # PV inverter 2 — SMA via Modbus
  - name: pv2
    type: template
    template: sma-inverter-modbus
    usage: pv
    modbus: tcpip
    host: 10.0.50.203
    port: 502

  # SMA Sunny Boy Storage battery via Modbus
  - name: battery
    type: template
    template: sma-sbs-modbus
    usage: battery
    modbus: tcpip
    host: 10.0.50.202
    port: 502
    maxchargepower: 4200
    watchdog: 60s

  # Marstek Venus E battery — bridged through HA REST API
  # Direct Modbus (10.0.50.113:502) is closed from EVCC's network.
  # HA's Marstek Modbus integration reads it fine, so bridge via HA entities.
  # NOTE: Do NOT add 'usage: battery' — custom type doesn't accept it.
  #       The 'soc:' field implicitly marks this as a battery.
  - name: marstek
    type: custom
    capacity: 5
    maxchargepower: 2500
    maxdischargepower: 2500
    power:
      source: http
      uri: http://10.0.30.10:8123/api/states/sensor.marstek_venus_modbus_ac_leistung
      headers:
        Authorization: "Bearer __HA_TOKEN__"
      jq: '.state | tonumber'
    soc:
      source: http
      uri: http://10.0.30.10:8123/api/states/sensor.marstek_venus_modbus_batterie_ladezustand
      headers:
        Authorization: "Bearer __HA_TOKEN__"
      jq: '.state | tonumber'
    energy:
      source: http
      uri: http://10.0.30.10:8123/api/states/sensor.marstek_venus_modbus_gesamte_entladeenergie
      headers:
        Authorization: "Bearer __HA_TOKEN__"
      jq: '.state | tonumber'

  # Hoymiles micro-inverter via OpenDTU
  - name: hoymiles
    type: template
    template: hoymiles-opendtu
    usage: pv
    host: 10.0.40.55

# ─── CHARGER (custom, bridged through HA REST API) ──────────
# SMA EV Charger (10.0.50.205) — direct connection fails (TLS cert
# without IP-SANs, SMA API returns 400). Native HA SMA integration
# works via different network path. Bridge through HA entities.

chargers:
  - name: wallbox
    type: custom
    status:
      source: http
      uri: http://10.0.30.10:8123/api/states/sensor.ev_charger_schon_status_ladevorgang
      headers:
        Authorization: "Bearer __HA_TOKEN__"
      jq: 'if .state == "active_mode" then "C" elif .state == "sleep_mode" then "B" elif .state == "not_connected" then "A" else "F" end'
    enabled:
      source: http
      uri: http://10.0.30.10:8123/api/states/switch.ev_charger_schon_manuelle_ladefreigabe
      headers:
        Authorization: "Bearer __HA_TOKEN__"
      jq: '.state == "on"'
    enable:
      source: http
      uri: http://10.0.30.10:8123/api/services/switch/{{ if .enable }}turn_on{{ else }}turn_off{{ end }}
      method: POST
      headers:
        Authorization: "Bearer __HA_TOKEN__"
        Content-Type: application/json
      body: '{"entity_id": "switch.ev_charger_schon_manuelle_ladefreigabe"}'
    maxcurrent:
      source: http
      uri: http://10.0.30.10:8123/api/services/number/set_value
      method: POST
      headers:
        Authorization: "Bearer __HA_TOKEN__"
        Content-Type: application/json
      body: '{"entity_id": "number.ev_charger_schon_ac_strom_begrenzung", "value": {{.maxcurrent}}}'
    power:
      source: http
      uri: http://10.0.30.10:8123/api/states/sensor.ev_charger_schon_leistung_ladestation
      headers:
        Authorization: "Bearer __HA_TOKEN__"
      jq: '.state | tonumber'
    energy:
      source: http
      uri: http://10.0.30.10:8123/api/states/sensor.ev_charger_schon_zahlerstand_ladestation
      headers:
        Authorization: "Bearer __HA_TOKEN__"
      jq: '.state | tonumber'
      scale: 0.001

# ─── LOADPOINT ──────────────────────────────────────────────

loadpoints:
  - title: Wallbox
    charger: wallbox
    mode: pv
    phases: 0
    mincurrent: 6
    maxcurrent: 16
    enable:
      threshold: 0
      delay: 1m
    disable:
      threshold: 500
      delay: 3m

# ─── SITE ───────────────────────────────────────────────────

site:
  title: Zuhause
  meters:
    grid: grid
    pv:
      - pv1
      - pv2
      - hoymiles
    battery:
      - battery
      - marstek
  residualPower: 100

# ─── TARIFFS ────────────────────────────────────────────────

tariffs:
  currency: EUR
  grid:
    type: tibber
    token: "__TIBBER_TOKEN__"
