# letzter Zählertstand:  input_number.zaehlerstand_letze_abrechnung
# GPIO2 als UART für IR-Transistor

esphome:
  name: stromzaehlerhaus
  friendly_name: Stromzähler Haus

esp8266:
  board: esp01_1m


# alles loggen
logger:
  esp8266_store_log_strings_in_flash: false
  level: ERROR
  baud_rate: 0
  

# Enable Home Assistant API
api:



# OTA zum Einslielen von Updates per WLAN
ota:
  - platform: esphome


# WLAN
wifi:
  networks:
    - ssid: !secret secret_wifi_ssid
      password: !secret secret_wifi_password
  manual_ip:
    static_ip: 192.168.1.81
    gateway: 192.168.1.1
    subnet: 255.255.255.0
    dns1: 192.168.1.254


  # Fallback-Hotspot, wenn kein WLAN
  ap:
    ssid: "StromZHaus Fallback Hotspot"
    password: !secret secret_fallback

# Weboberfläche zur Einstellung der Verbindugsdaten, wenn WLAN nicht gefunden wird
captive_portal:


# eigene Weboberfläche zur Anzeige    
web_server:
  port: 80
  ota: False
  include_internal: True

time:
  - platform: homeassistant
    id: homeassistant_time


external_components:
#  - source: github://alengwenus/esphome_components@main
  - source: github://cgommel/esphome_components@main
    refresh: 0s
    components: [sml]


# IR-LED an GPIO2
uart:
  rx_pin: GPIO2
  baud_rate: 9600
  data_bits: 8
  parity: NONE
  stop_bits: 1



# SML aktivieren
sml:
  id: mysml


# Sensordaten
sensor:
  # Zählerstand letzte Abrechnung
  # kommt von einem Helferlein
  # wird manuell auf Grund der Abrechnung gesetzt
  - platform: homeassistant
    id: zstandabrech
    name: "Zählerstand letzte Abrechnung"
    entity_id: input_number.zaehlerstand_letze_abrechnung
    unit_of_measurement: kWh
    accuracy_decimals: 1
    device_class: energy
    state_class: total_increasing

  # aktueller Strompreis
  # kommt von einem Helferlein
  # wird manuell auf Grund der Abrechnung gesetzt
  - platform: homeassistant
    id: preis
    name: "aktueller Strompreis"
    entity_id: input_number.strom_abrech_arb_strompreis
    unit_of_measurement: €
    accuracy_decimals: 2


  # Verbrauch total
  - platform: sml
    name: "Zählerstand"
    sml_id: mysml
    obis_code: "1-0:1.8.0"
    unit_of_measurement: kWh
    accuracy_decimals: 1
    device_class: energy
    state_class: total_increasing
    id: energieverbrauch_total_kwh
    filters:
      - multiply: 0.0001

  # Verbrauch seit letzter Abrechnung
  - platform: sml
    name: "Verbrauch seit letzter Abrechnung"
    sml_id: mysml
    obis_code: "1-0:1.8.0"
    unit_of_measurement: kWh
    accuracy_decimals: 1
    device_class: energy
    state_class: total_increasing
    id: energieverbrauch_abrzeitraum_kwh
    filters:
      #  alter Zählerstand in HA gespeichert
      - lambda: 'return (0.0001 * x)-id(zstandabrech).state;'

  # Momentanverbrauch
  - platform: sml
    name: "aktuelle Leistung"
    sml_id: mysml
    obis_code: "1-0:16.7.0"
    unit_of_measurement: W
    accuracy_decimals: 2
    device_class: power
    state_class: measurement
    id: energieverbrauch_aktuell_w
    filters:
      - multiply: 0.01



  - platform: sml
    name: "aktuelle Leistung X"
    sml_id: mysml
    obis_code: "1-0:16.7.0"
    unit_of_measurement: W
    device_class: power
    state_class: measurement
    id: energieverbrauch_aktuell_w_x



  # Verbrauch Tag
  - platform: total_daily_energy
    name: "Energieverbrauch Tag"
    unit_of_measurement: 'kWh'
    accuracy_decimals: 1
    device_class: energy
    id: energieverbrauch_tag_kwh
    state_class: total
    filters:
      - multiply: 0.0001
    power_id: energieverbrauch_aktuell_w


# Einspeisung aus Photovoltaik
  - platform: sml
    name: "Einspeisung total"
    sml_id: mysml
    obis_code: "1-0:2.8.0"
    unit_of_measurement: kWh
    accuracy_decimals: 1
    device_class: energy
    id: einspeisung_total_kwh
    state_class: total_increasing
    filters:
      - multiply: 0.0001


  - platform: template
    name: "Kosten seit letzter Abrechnung"
    id: luft_abs
    accuracy_decimals: 2
    update_interval: 60s
    icon: 'mdi:currency-eur'
    unit_of_measurement: '€'
    lambda: |-
      float x1;
      int x2;
      x1 = id(preis).state * id(energieverbrauch_abrzeitraum_kwh).state;
      // x2 = (int)(x1 *100 + .5);
      // x1 = (float)x1 / 100;
      return x1;


text_sensor:
  - platform: sml
    name: "Zählertyp"
    sml_id: mysml
    obis_code: "1-0:96.50.1*1"
    format: text


  - platform: sml
    name: "Hersteller"
    sml_id: mysml
    obis_code: "129-129:199.130.3"
    format: text


button:
  - platform: restart
    name: "Reset"
