# Energy Package

# -----------------------------------------------------------------------------
# Energy Integration Sensors
sensor:
  - name: Envoy Total Energy
    platform: integration
    source: sensor.envoy_202221032900_current_power_production
    unit_prefix: k
    unit_time: h

  - name: Emporia Net Energy
    platform: integration
    source: sensor.emporia_d937d0_1min
    unit_prefix: k
    unit_time: h

  - name: Total Energy From Grid
    platform: integration
    source: sensor.emporia_vue_net_power_from_grid
    unit_prefix: k
    unit_time: h

  - name: Total Energy To Grid
    platform: integration
    source: sensor.emporia_vue_net_power_to_grid
    unit_prefix: k
    unit_time: h

  - name: Home Total Energy
    platform: integration
    source: sensor.home_power_consumption
    unit_prefix: k
    unit_time: h

# -----------------------------------------------------------------------------
# Average Panel Power Sensor
  - name: Envoy Inverter Average Power
    platform: min_max
    type: mean
    entity_ids:[% for sn in serial_numbers %]
      - sensor.envoy_202221032900_inverter_[[sn]]
[%- endfor %]
[%- for sn in serial_numbers %]

  - name: Envoy 202221032900 Inverter [[sn]] Average Power Share
    platform: statistics
    entity_id: sensor.envoy_202221032900_inverter_[[sn]]_power_share
    state_characteristic: mean
    max_age:
      hours: 24
[%- endfor %]

# -----------------------------------------------------------------------------
# Panel/Inverter Power Share Sensors
template:
  sensor:[% for sn in serial_numbers %]
    - name: Envoy 202221032900 Inverter [[sn]] Power Share
      unit_of_measurement: "%"
      availability: "{{ states('sensor.envoy_inverter_average_power')|float > 5 }}"
      state: >
        {% if states('sensor.envoy_inverter_average_power')|default(0)|float == 0 %}
          none
        {% else %}
          {{ (100 * 
                  states('sensor.envoy_202221032900_inverter_[[sn]]')|default(0)|float 
                / states('sensor.envoy_inverter_average_power')|float 
              ) | round(1)
          }}
        {% endif %}
[% endfor %]
# -----------------------------------------------------------------------------
# Emporia Vue To/From Grid Sensors
    - name: Emporia Vue Net Power From Grid
      device_class: power
      unit_of_measurement: W
      state: >
        {% set net_power = states('sensor.emporia_d937d0_1min')|float %}{{
          iif(net_power > 0, net_power, 0)
        }}

    - name: Emporia Vue Net Power To Grid
      device_class: power
      unit_of_measurement: W
      state: >
        {% set net_power = states('sensor.emporia_d937d0_1min')|float %}{{
          iif(net_power < 0, -net_power, 0)
        }}

# -----------------------------------------------------------------------------
# Home Total Power Consumption Sensor
    - name: Home Power Consumption
      device_class: power
      unit_of_measurement: W
      state: >
        {{ 
            states('sensor.envoy_202221032900_current_power_production')|float 
          + states('sensor.emporia_d937d0_1min')|float
        }}

# -----------------------------------------------------------------------------
# TOU Schedules
    - name: TOU Season
      state: "{{ ['Winter', 'Summer'][now().month >= 6 and now().month < 10] }}"
      icon: mdi:weather-cloudy-clock

    - name: TOU Period
      icon: mdi:calendar-clock
      state: >
        {% set is_weekend = now().strftime("%w") == "0" or now().strftime("%w") == "6" %}
        {% if states('sensor.tou_season') == "Summer" %}
          {% if now().hour >= 16 and now().hour < 21 %}
            {% if is_weekend %}
              {{ "Mid-Peak" }}
            {% else %}
              {{ "On-Peak" }}
            {% endif %}
          {% else %}
            {{ "Off-Peak" }}
          {% endif %}
        {% else %}
          {% if now().hour >= 16 and now().hour < 21 %}
            {{ "Mid-Peak" }}
          {% elif now().hour >= 21 or now().hour < 8 %}
            {{ "Off-Peak" }}
          {% else %}
            {{ "Super Off-Peak" }}
          {% endif %}
        {% endif %}

# -----------------------------------------------------------------------------
# Utility Meters
utility_meter:
  daily_energy:
    source: sensor.total_energy_from_grid
    name: Daily Energy Consumption
    cycle: daily
    tariffs:
      - Super Off-Peak
      - Off-Peak
      - Mid-Peak
      - On-Peak

  monthly_consumed:
    source: sensor.total_energy_from_grid
    name: Monthly Energy Consumption
    cron: 0 0 8 * *
    tariffs:
      - Super Off-Peak
      - Off-Peak
      - Mid-Peak
      - On-Peak

  monthly_generated:
    source: sensor.total_energy_to_grid
    name: Monthly Energy Generation
    cron: 0 0 8 * *

  yearly_consumed:
    source: sensor.total_energy_from_grid
    name: Yearly Energy Consumption
    cron: 0 0 15 7 *
    tariffs:
      - Super Off-Peak
      - Off-Peak
      - Mid-Peak
      - On-Peak

  yearly_generated:
    source: sensor.total_energy_to_grid
    name: Yearly Energy Generation
    cron: 0 0 15 7 *


automation:
# -----------------------------------------------------------------------------
# Utility Meter Automations
  - alias: Set Utility Meter TOU Tariff
    trigger:
      - platform: state
        entity_id: sensor.tou_period
      - platform: homeassistant
        event: start
    action:
      - service: select.select_option
        target:
          entity_id: select.daily_energy
        data:
          option: "{{ states('sensor.tou_period') }}"

      - service: select.select_option
        target:
          entity_id: select.monthly_consumed
        data:
          option: "{{ states('sensor.tou_period') }}"

      - service: select.select_option
        target:
          entity_id: select.yearly_consumed
        data:
          option: "{{ states('sensor.tou_period') }}"


