##########################################################################################
# GLOBAL VARIABLES
# https://esphome.io/guides/automations.html?highlight=globals#global-variables
##########################################################################################
globals:
  ####################################################
  # total_energy: cumulative power
  # Not restored, so each boot starts fresh at 0.
  ####################################################
  - id: total_energy
    type: float
    restore_value: yes
    initial_value: '0.0'
    
##########################################################################################
# GENERAL COMMON SENSORS
# https://esphome.io/components/sensor/
##########################################################################################
sensor:
  #############################################
  # CSE7766 POWER SENSOR
  # https://esphome.io/components/sensor/cse7766.html
  #############################################
  - platform: hlw8012
    id: athom_hlw8012
    sel_pin:
      number: GPIO12
      inverted: True
    cf_pin: GPIO4
    cf1_pin: GPIO5
    voltage_divider: 780

    current:
      name: "Current"
      id: current
      unit_of_measurement: A
      accuracy_decimals: 2
      icon: mdi:current-ac
      filters:
          - calibrate_linear:
            - 0.0000 -> 0.0110 # Relay off no load
            - 0.0097 -> 0.0260 # Relay on no load
            - 0.9270 -> 0.7570
            - 2.0133 -> 1.6330
            - 2.9307 -> 2.3750
            - 5.4848 -> 4.4210
            - 8.4308 -> 6.8330
            - 9.9171 -> 7.9830
          # Normalize for plug load
          - lambda: if (x < 0.0260) return 0; else return (x - 0.0260);
      on_value_range:
        - above: ${local_current_limit}
          then:
            - switch.turn_off: relay      

    voltage:
      name: "Voltage"
      id:   voltage
      unit_of_measurement: V
      accuracy_decimals: 1
      icon: mdi:sine-wave
      filters:
        - skip_initial: 2

    power:
      name: "Power"
      id: power_sensor
      unit_of_measurement: W
      accuracy_decimals: 1
      icon: mdi:power
      filters:
          - calibrate_linear:
            - 0.0000 -> 0.5900 # Relay off no load
            - 0.0000 -> 1.5600 # Relay on no load
            - 198.5129 -> 87.8300
            - 434.2469 -> 189.5000
            - 628.6241 -> 273.9000
            - 1067.0067 -> 460.1000
            - 1619.8098 -> 699.2000
            - 2043.0282 -> 885.0000
          # Normalize for plug load
          - lambda: if (x < 1.5600) return 0; else return (x - 1.5600);
    change_mode_every: 1
    update_interval: 5s

    # Shows the Energy kWh since the device was last started
    energy:
      name: "Energy (Since Restart)"
      id: energy
      icon: mdi:lightning-bolt
      unit_of_measurement: kWh
      accuracy_decimals: 3
      filters:
        # Multiplication factor from W to kW is 0.001
        - multiply: 0.001
      on_value:
        then:
          - lambda: |-
              static float previous_energy_value = 0.0;
              float current_energy_value = id(energy).state;
              id(total_energy) += current_energy_value - previous_energy_value;
              previous_energy_value = current_energy_value;
              id(total_energy_sensor).update();
      # internal: ${hide_energy_sensor}
  
  #############################################
  # Total Energy (All Time)
  #############################################
  - platform: template
    name: "Total Energy"
    id: total_energy_sensor
    unit_of_measurement: kWh
    device_class: "energy"
    state_class: "total_increasing"
    icon: mdi:lightning-bolt
    accuracy_decimals: 3
    lambda: |-
      return id(total_energy);
    update_interval: "60s"

  #############################################
  # Total Daily Energy
  # https://esphome.io/components/sensor/total_daily_energy.html
  #############################################
  - platform: total_daily_energy
    name: "Total Daily Energy"
    restore: true
    power_id: power_sensor
    unit_of_measurement: kWh
    icon: mdi:hours-24
    accuracy_decimals: 3
    filters:
      - multiply: 0.001

##########################################################################################
# STATUS LED
# https://esphome.io/components/status_led.html
##########################################################################################
status_led:
  pin:
    number: GPIO13
    inverted: True