---
#Küche: Kaffeemaschine
#Board: Onestyle SD-WL-02 Power Monitoring Plug

#GPIO0 	n.a.
#GPIO1 	CSE7766 Tx
#GPIO2 	n.a.
#GPIO3 	CSE7766 Rx
#GPIO4 	n.a.
#GPIO5 	n.a.
#GPIO9 	n.a.
#GPIO10 n.a.
#GPIO12 Push Button (inverted/non-inv?)
#GPIO13 LED (inverted)
#GPIO14 Relay1
#GPIO15 n.a.
#GPIO16 n.a.
#FLAG 	None 

substitutions:
  devicename: powplug4
  friendly_name: "Kaffeemaschine"
  update_interval: 2min
  update_interval_short: 2s
  reboot_timeout: 7d
  #device_mac: "d8:f1:5b:d1:9d:03" # powplug1
  device_mac: "24:62:ab:45:b1:b0" # powplug4
  device_ip: 192.168.178.124
  #domain: .iot
  room: Küche
  upper_room: Wohnung
  toggledev: ${devicename}_relay
  mqtt_topic_prefix: energy/${devicename}/

esphome:
  esp8266_restore_from_flash: true
  name: ${devicename}
  platform: ESP8266
  #board: esp01_1m
  board: esp8285
  board_flash_mode: dout
  build_path: builds/${devicename}

globals:
  - id: activated
    type: bool
    restore_value: true
    initial_value: 'false'

<<: !include common/common.yaml # wifi, api, ota, webserver, textsensors

#logger: !include common/logger.yaml # level info
#logger: !include common/logger_dis.yaml # level warn
#logger: !include common/logger_dbg.yaml # level debug
logger:
  baud_rate: 0

uart:
  rx_pin: RX
  #tx_pin: GPIO1
  #rx_pin: GPIO3
  baud_rate: 4800

time: !include common/time.yaml

binary_sensor:
  - !include common/binarysensor/status.yaml
  # Binary sensor for the Push Button
  - platform: gpio
    name: Button ${devicename}
    device_class: power
    id: ${devicename}_button
    pin:
      number: GPIO12
      #mode: INPUT_PULLUP
      inverted: true
    on_press:
      - switch.toggle: ${devicename}_relay
      #- switch.toggle: ${toggledev}
  - platform: template
    name: ${friendly_name} läuft
    filters:
      - delayed_off: 90s
    lambda: |-
      if (isnan(id(power).state)) {
        // Error reading power consumption
        return {};
      } else if (id(power).state > 2) {
        // Running and heating
        return true;
      } else {
        // Not running, in standby
        return false;
      }

sensor:
  #- !include common/sensor/wifisignal.yaml
  - !include common/sensor/uptime.yaml
  #- !include common/sensor/adc_vcc.yaml
  #- !include common/sensor/hlw8012.yaml
  # Power sensor
  - platform: cse7766
    update_interval: ${update_interval_short}
    # Current sensor
    current:
      force_update: true
      name: Current ${devicename}
      unit_of_measurement: A
      accuracy_decimals: 3
      icon: mdi:flash-auto
      filters:
        # Map from sensor -> measured value
        - calibrate_linear:
            - 0.0 -> 0.012
            - 0.082 -> 0.071
            - 1.342 -> 1.066
            - 5.571 -> 4.408
            - 6.691 -> 5.259
            - 6.971 -> 5.540
        # bis 0.015A Rückmeldung 0A, über 0.012A ist Last, übersteigt den Eigenverbrauch
        - lambda: if (x < 0.015) return 0; else return (x - 0.012);
    # Voltage sensor
    voltage:
      id: voltage
      name: Voltage ${devicename}
      unit_of_measurement: V
      accuracy_decimals: 1
      icon: mdi:flash-circle
      filters:
        # Map from sensor -> measured value
        - calibrate_linear:
            - 0.0 -> 0.0
            - 602.9 -> 229.9
            - 609.8 -> 232.8
    # Power sensor
    power:
      id: power
      name: Power ${devicename}
      unit_of_measurement: W
      accuracy_decimals: 1
      icon: mdi:power-plug
      filters:
        # Map from sensor -> measured value
        - calibrate_linear:
            - 0.0 -> 1.14
            - 62.062 -> 10.9
            - 1503.272 -> 247.6
            - 1599.812 -> 263.7
            - 3923.677 -> 631.4
            - 7109.509 -> 1148.0
            - 7237.086 -> 1193.0
            - 7426.713 -> 1217.0
        # bis 1.5W Rückmeldung 0W, Eigenverbrauch ca. 1.14W
        - lambda: if (x < 1.5) return 0; else return (x - 1.14);
      on_value_range:
        - above: 2.0
          then:
            - light.turn_on: led
            - lambda: id(activated) = true;
        - below: 1.0
          then:
            - light.turn_off: led
            - lambda: id(activated) = false;
  # Total daily energy sensor
  - platform: total_daily_energy
    force_update: true
    #name: daily_energy
    name: Tagesverbrauch ${friendly_name}
    power_id: power
    icon: mdi:calendar-month
    filters:
      # Multiplication factor from W to kW
      - multiply: 0.001
    unit_of_measurement: kWh
    accuracy_decimals: 3


switch:
  - !include common/switch/restart.yaml  # remote device restart
  # Switch to toggle the relay itself
  - platform: gpio
    id: ${devicename}_relay
    #name: Relay ${devicename}
    name: switch
    pin: GPIO14
    restore_mode: RESTORE_DEFAULT_OFF # relay state after reboot/power-loss event
    on_turn_on:
      - light.turn_on: led
      - lambda: id(activated) = true;
    on_turn_off:
      - light.turn_off: led
      - lambda: id(activated) = false;

output:
  # Relay state led
  - platform: esp8266_pwm
    id: state_led
    pin:
      number: GPIO13
      inverted: true

light:
  # Relay state light
  - platform: monochromatic
    output: state_led
    id: led

# additional indicator (system status or error) on LED
status_led: !include common/misc/statusled_13.yaml


