type: grid
cards:
  - type: heading
    icon: mdi:ev-station
    heading_style: title
    badges:
      - type: entity
        show_state: true
        show_icon: true
        entity: binary_sensor.openwb_chargepoint_3_ladekabel
        visibility:
          - condition: state
            entity: binary_sensor.openwb_chargepoint_3_ladekabel
            state: "off"
        icon: mdi:power-plug-off
      - type: entity
        show_state: true
        show_icon: true
        entity: select.openwb_chargepoint_3_lademodus
        visibility:
          - condition: state
            entity: binary_sensor.openwb_chargepoint_3_ladekabel
            state: "on"
      - type: entity
        show_state: true
        show_icon: true
        entity: sensor.openwb_chargepoint_3_ladeleistung
        state_content: name
        visibility:
          - condition: state
            entity: binary_sensor.openwb_chargepoint_3_autoladestatus
            state: "off"
          - condition: state
            entity: binary_sensor.openwb_chargepoint_3_ladekabel
            state_not: "off"
        name: lädt nicht
    heading: Wallbox
  - type: tile
    entity: input_boolean.zielladen_eingestellt
    name: Zielladen
    vertical: false
    tap_action:
      action: toggle
    features_position: bottom
    visibility:
      - condition: state
        entity: select.openwb_chargepoint_3_lademodus
        state_not: Instant Charging
  - type: tile
    entity: input_number.soc_zielladen
    name: bis Ladestand
    vertical: false
    features:
      - type: numeric-input
        style: buttons
    features_position: bottom
    visibility:
      - condition: state
        entity: input_boolean.zielladen_eingestellt
        state: "on"
  - type: tile
    visibility:
      - condition: state
        entity: input_boolean.zielladen_eingestellt
        state: "on"
    entity: input_datetime.zielzeit_zielladen
    name: um
    vertical: false
    features_position: bottom
  - type: tile
    grid_options:
      columns: 6
      rows: 1
    visibility:
      - condition: state
        entity: select.openwb_chargepoint_3_lademodus
        state_not: Instant Charging
      - condition: state
        entity: input_boolean.zielladen_eingestellt
        state: "off"
    entity: select.openwb_chargepoint_3_lademodus
    name: Max. Leistung
    icon: mdi:car-electric
    hide_state: true
    vertical: false
    tap_action:
      action: perform-action
      perform_action: script.sofort_laden
      target: {}
    icon_tap_action:
      action: perform-action
      perform_action: script.sofort_laden
      target: {}
    features_position: bottom
  - type: tile
    visibility:
      - condition: state
        entity: select.openwb_chargepoint_3_lademodus
        state: Instant Charging
    entity: number.openwb_chargepoint_3_soc_limit_sofortladen
    name: Max. Leistung bis
    icon: mdi:car-electric
    color: accent
    vertical: false
    tap_action:
      action: none
    features:
      - type: numeric-input
        style: buttons
    features_position: bottom
  - type: gauge
    entity: sensor.openwb_chargepoint_3_ladeleistung
    max: 10700
    min: 0
    needle: false
    severity:
      green: 4000
      yellow: 2000
      red: 1000
    visibility:
      - condition: numeric_state
        entity: sensor.openwb_chargepoint_3_ladeleistung
        above: 0
    grid_options:
      columns: 6
      rows: auto
  - type: markdown
    content: >
      {% set fahrzeug = states('sensor.openwb_chargepoint_3_fahrzeug') %}

      {% set ladeleistung = states('sensor.openwb_chargepoint_3_ladeleistung') |
      float(0) %}

      {% set lademodus = states('select.openwb_chargepoint_3_lademodus') %}

      {% set soc_ziel = states('input_number.soc_zielladen') | float(0) %}

      {% set ladeziel_bmw =
      states('sensor.i5_edrive40_battery_ev_target_state_of_charge') | int %}

      {% set ladeziel_mini =
      states('sensor.cooper_e_battery_ev_target_state_of_charge') | int %}


      {# Batteriekapazität wählen #}

      {% if fahrzeug == 'BMW i5' %}
        {% set kapazitaet = states('input_number.kapazitat_batterie_bmw') | float(0) %}
      {% else %}
        {% set kapazitaet = states('input_number.kapazitat_batterie_mini') | float(0) %}
      {% endif %}


      {# Grundtext #}

      {% if is_state('input_boolean.zielladen_eingestellt', 'on') %}
        {% set startzeit = states('input_datetime.startzeit_zielladen') %}
        {% set zielzeit = states('input_datetime.zielzeit_zielladen') %}
        {% if startzeit == zielzeit %}
      Laden nicht notwendig.
        {% else %}
      🔋 Zielladen beginnt um **{{ startzeit[:5] }}**.
          {% if fahrzeug == 'BMW i5' and ladeziel_bmw < soc_ziel %}
      ⚠️ Das Fahrzeug beendet das Laden bei **{{ ladeziel_bmw }} %**, in der App
      umstellen.
          {% endif %}
          {% if fahrzeug == 'Mini' and ladeziel_mini < soc_ziel %}
      ⚠️ Das Fahrzeug beendet das Laden bei **{{ ladeziel_mini }} %**, in der
      App umstellen.
          {% endif %}
        {% endif %}
      {% endif %}

      {# Fertigzeit berechnen, wenn Laden aktiv #}

      {% if ladeleistung > 0 and lademodus == 'Instant Charging' %}
        {% set ladeeffizienz = states('input_number.fahrzeuge_wirkungsgrad_laden') | float(0) %}
        {% set soc_aktuell = states('sensor.openwb_chargepoint_3_ladung') | float(0) %}
        {% set soc_limit = states('number.openwb_chargepoint_3_soc_limit_sofortladen') | int %}
        {% set rest_soc = soc_limit - soc_aktuell %}
        {% set rest_kwh = kapazitaet * rest_soc / 100 %}
        {% set rest_stunden = rest_kwh / ((ladeeffizienz/100)*(ladeleistung/1000)) %}
        {% set rest_minuten = (rest_stunden * 60) | int %}
        {% set fertig_timestamp = now().timestamp() + rest_minuten * 60 %}
        {% set fertigzeit = fertig_timestamp | timestamp_custom('%H:%M') %}

      🔋 Laden voraussichtlich fertig um **{{ fertigzeit }}**.
        {% if fahrzeug == 'BMW i5' and ladeziel_bmw < soc_limit %}
      ⚠️ Das Fahrzeug beendet das Laden bei **{{ ladeziel_bmw }} %**, in der App
      umstellen.
        {% endif %}
        {% if fahrzeug == 'Mini' and ladeziel_mini < soc_limit %}
      ⚠️ Das Fahrzeug beendet das Laden bei **{{ ladeziel_mini }} %**, in der
      App umstellen.
        {% endif %}

      {% endif %}
    text_only: true
