---
substitutions:
  device: mains-power
  name: Mains Power
  area: Room
  comment: Mains Power Monitoring

esphome:
  name: mcu-${device}
  friendly_name: ${name}
  area: ${area}
  comment: ${comment}
  areas:
    - id: local_area
      name: ${area}
  devices:
    - id: mains
      name: Mains
      area_id: local_area

esp32:
  variant: esp32
  toolchain: esp-idf
  framework:
    type: esp-idf

packages:
  common: !include common/common.yaml

# PINOUTS: M5ATOM
# IR        G12
# NEO       G27
# BUTTON    G39
# I2C0 SDA  G26
# I2C0 SCL  G32
# I2C1 SDA  G25
# I2C1 SCL  G21
# UART0 RX  G3
# UART0 TX  G1
# UART1 RX  G23
# UART1 TX  G33
# 1-WIRE    G22
# NC        G19

modbus:
  uart_id: uart_modbus

uart:
  - id: uart_modbus
    rx_pin: 26
    tx_pin: 32
    baud_rate: 9600
    stop_bits: 2
    # debug:

sensor:
  # - platform: template
  #   name: Total Daily Energy
  #   id: mains_daily_energy
  #   lambda: |-
  #     return (id(mains_l1_total_daily_energy).state + id(mains_l3_total_daily_energy).state + id(mains_l3_total_daily_energy).state);
  #   update_interval: 60s
  # L1
  - platform: pzemac
    address: 10
    id: pzemac_l1
    current:
      name: L1 Current
      device_id: mains
    voltage:
      name: L1 Voltage
      device_id: mains
    energy:
      name: L1 Energy
      id: mains_l1_energy
      device_id: mains
    power:
      name: L1 Power
      id: mains_l1_power
      device_id: mains
    frequency:
      name: L1 Frequency
      device_id: mains
    power_factor:
      name: L1 Power Factor
      device_id: mains
    update_interval: 20s
  # - platform: total_daily_energy
  #   name: L1 Total Daily Energy
  #   power_id: mains_l1_power
  #   id: mains_l1_total_daily_energy
  #   filters:
  #     - multiply: 0.001
  #   unit_of_measurement: kWh
  #   icon: mdi:counter
  # - platform: integration
  #   name: L1 Energy Meter
  #   sensor: mains_l1_power
  #   time_unit: h
  #   filters:
  #     - lambda: return x * (0.001) + 2282;
  #   unit_of_measurement: kWh
  #   icon: mdi:counter
  # L2
  - platform: pzemac
    address: 11
    id: pzemac_l2
    current:
      name: L2 Current
      device_id: mains
    voltage:
      name: L2 Voltage
      device_id: mains
    energy:
      name: L2 Energy
      id: mains_l2_energy
      device_id: mains
    power:
      name: L2 Power
      id: mains_l2_power
      device_id: mains
    frequency:
      name: L2 Frequency
      device_id: mains
    power_factor:
      name: L2 Power Factor
      device_id: mains
    update_interval: 20s
  # - platform: total_daily_energy
  #   name: L2 Total Daily Energy
  #   id: mains_l2_total_daily_energy
  #   power_id: mains_l2_power
  #   filters:
  #     - multiply: 0.001
  #   unit_of_measurement: kWh
  #   icon: mdi:counter
  # - platform: integration
  #   name: L2 Energy Meter
  #   sensor: mains_l2_power
  #   time_unit: h
  #   filters:
  #     - lambda: return x * (0.001) + 2282;
  #   unit_of_measurement: kWh
  #   icon: mdi:counter
  # L3
  - platform: pzemac
    address: 12
    id: pzemac_l3
    current:
      name: L3 Current
      device_id: mains
    voltage:
      name: L3 Voltage
      device_id: mains
    energy:
      name: L3 Energy
      id: mains_l3_energy
      device_id: mains
    power:
      name: L3 Power
      id: mains_l3_power
      device_id: mains
    frequency:
      name: L3 Frequency
      device_id: mains
    power_factor:
      name: L3 Power Factor
      device_id: mains
    update_interval: 20s
  # - platform: total_daily_energy
  #   name: L3 Total Daily Energy
  #   power_id: mains_l3_power
  #   id: mains_l3_total_daily_energy
  #   filters:
  #     - multiply: 0.001
  #   unit_of_measurement: kWh
  #   icon: mdi:counter
  # - platform: integration
  #   name: L3 Energy Meter"
  #   sensor: mains_l3_power
  #   time_unit: h
  #   filters:
  #     - lambda: return x * (0.001) + 2282;
  #   unit_of_measurement: kWh
  #   icon: mdi:counter
  # TOTAL: L1+L2+L3
  - platform: template
    name: Power
    id: total_mains_power
    device_id: mains
    device_class: power
    state_class: measurement
    unit_of_measurement: W
    accuracy_decimals: 1
    lambda: |-
      return id(mains_l1_power).state + id(mains_l2_power).state + id(mains_l3_power).state ;
  # - platform: template
  #   name: Total Daily Energy
  #   id: mains_total_daily_energy
  #   icon: mdi:counter
  #   device_class: energy
  #   # state_class: measurement
  #   state_class: total_increasing
  #   unit_of_measurement: kWh
  #   accuracy_decimals: 1
  #   lambda: |-
  #     return id(mains_l1_total_daily_energy).state + id(mains_l2_total_daily_energy).state + id(mains_l3_total_daily_energy).state ;
  - platform: template
    name: Energy
    device_id: mains
    icon: mdi:counter
    device_class: energy
    state_class: total_increasing
    unit_of_measurement: kWh
    accuracy_decimals: 1
    filters:
      - multiply: 0.001
    lambda: |-
      return id(mains_l1_energy).state + id(mains_l2_energy).state + id(mains_l3_energy).state;

button:
  - platform: template
    name: Energy Zero
    device_id: mains
    on_press:
      - pzemac.reset_energy: pzemac_l1
      - pzemac.reset_energy: pzemac_l2
      - pzemac.reset_energy: pzemac_l3

time:
  - platform: homeassistant
    id: ha_time
    timezone: Europe/Bratislava
    on_time:
      - cron: '0 0 0 * * *'
        then:
          - pzemac.reset_energy: pzemac_l1
          - pzemac.reset_energy: pzemac_l2
          - pzemac.reset_energy: pzemac_l3
