# 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:
      - sensor.envoy_202221032900_inverter_202147113780
      - sensor.envoy_202221032900_inverter_202147116830
      - sensor.envoy_202221032900_inverter_202147117162
      - sensor.envoy_202221032900_inverter_202147117631
      - sensor.envoy_202221032900_inverter_202147122858
      - sensor.envoy_202221032900_inverter_202147123517
      - sensor.envoy_202221032900_inverter_202147125027
      - sensor.envoy_202221032900_inverter_202147125590
      - sensor.envoy_202221032900_inverter_202147125734
      - sensor.envoy_202221032900_inverter_202147125902
      - sensor.envoy_202221032900_inverter_202147126079
      - sensor.envoy_202221032900_inverter_202147126357
      - sensor.envoy_202221032900_inverter_202147126997
      - sensor.envoy_202221032900_inverter_202147128369
      - sensor.envoy_202221032900_inverter_202147129445
      - sensor.envoy_202221032900_inverter_202147130152
      - sensor.envoy_202221032900_inverter_202147130290

  - name: Envoy 202221032900 Inverter 202147113780 Average Power Share
    platform: statistics
    entity_id: sensor.envoy_202221032900_inverter_202147113780_power_share
    state_characteristic: mean
    max_age:
      hours: 24

  - name: Envoy 202221032900 Inverter 202147116830 Average Power Share
    platform: statistics
    entity_id: sensor.envoy_202221032900_inverter_202147116830_power_share
    state_characteristic: mean
    max_age:
      hours: 24

  - name: Envoy 202221032900 Inverter 202147117162 Average Power Share
    platform: statistics
    entity_id: sensor.envoy_202221032900_inverter_202147117162_power_share
    state_characteristic: mean
    max_age:
      hours: 24

  - name: Envoy 202221032900 Inverter 202147117631 Average Power Share
    platform: statistics
    entity_id: sensor.envoy_202221032900_inverter_202147117631_power_share
    state_characteristic: mean
    max_age:
      hours: 24

  - name: Envoy 202221032900 Inverter 202147122858 Average Power Share
    platform: statistics
    entity_id: sensor.envoy_202221032900_inverter_202147122858_power_share
    state_characteristic: mean
    max_age:
      hours: 24

  - name: Envoy 202221032900 Inverter 202147123517 Average Power Share
    platform: statistics
    entity_id: sensor.envoy_202221032900_inverter_202147123517_power_share
    state_characteristic: mean
    max_age:
      hours: 24

  - name: Envoy 202221032900 Inverter 202147125027 Average Power Share
    platform: statistics
    entity_id: sensor.envoy_202221032900_inverter_202147125027_power_share
    state_characteristic: mean
    max_age:
      hours: 24

  - name: Envoy 202221032900 Inverter 202147125590 Average Power Share
    platform: statistics
    entity_id: sensor.envoy_202221032900_inverter_202147125590_power_share
    state_characteristic: mean
    max_age:
      hours: 24

  - name: Envoy 202221032900 Inverter 202147125734 Average Power Share
    platform: statistics
    entity_id: sensor.envoy_202221032900_inverter_202147125734_power_share
    state_characteristic: mean
    max_age:
      hours: 24

  - name: Envoy 202221032900 Inverter 202147125902 Average Power Share
    platform: statistics
    entity_id: sensor.envoy_202221032900_inverter_202147125902_power_share
    state_characteristic: mean
    max_age:
      hours: 24

  - name: Envoy 202221032900 Inverter 202147126079 Average Power Share
    platform: statistics
    entity_id: sensor.envoy_202221032900_inverter_202147126079_power_share
    state_characteristic: mean
    max_age:
      hours: 24

  - name: Envoy 202221032900 Inverter 202147126357 Average Power Share
    platform: statistics
    entity_id: sensor.envoy_202221032900_inverter_202147126357_power_share
    state_characteristic: mean
    max_age:
      hours: 24

  - name: Envoy 202221032900 Inverter 202147126997 Average Power Share
    platform: statistics
    entity_id: sensor.envoy_202221032900_inverter_202147126997_power_share
    state_characteristic: mean
    max_age:
      hours: 24

  - name: Envoy 202221032900 Inverter 202147128369 Average Power Share
    platform: statistics
    entity_id: sensor.envoy_202221032900_inverter_202147128369_power_share
    state_characteristic: mean
    max_age:
      hours: 24

  - name: Envoy 202221032900 Inverter 202147129445 Average Power Share
    platform: statistics
    entity_id: sensor.envoy_202221032900_inverter_202147129445_power_share
    state_characteristic: mean
    max_age:
      hours: 24

  - name: Envoy 202221032900 Inverter 202147130152 Average Power Share
    platform: statistics
    entity_id: sensor.envoy_202221032900_inverter_202147130152_power_share
    state_characteristic: mean
    max_age:
      hours: 24

  - name: Envoy 202221032900 Inverter 202147130290 Average Power Share
    platform: statistics
    entity_id: sensor.envoy_202221032900_inverter_202147130290_power_share
    state_characteristic: mean
    max_age:
      hours: 24

# -----------------------------------------------------------------------------
# Panel/Inverter Power Share Sensors
template:
  sensor:
    - name: Envoy 202221032900 Inverter 202147113780 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_202147113780')|default(0)|float 
                / states('sensor.envoy_inverter_average_power')|float 
              ) | round(1)
          }}
        {% endif %}

    - name: Envoy 202221032900 Inverter 202147116830 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_202147116830')|default(0)|float 
                / states('sensor.envoy_inverter_average_power')|float 
              ) | round(1)
          }}
        {% endif %}

    - name: Envoy 202221032900 Inverter 202147117162 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_202147117162')|default(0)|float 
                / states('sensor.envoy_inverter_average_power')|float 
              ) | round(1)
          }}
        {% endif %}

    - name: Envoy 202221032900 Inverter 202147117631 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_202147117631')|default(0)|float 
                / states('sensor.envoy_inverter_average_power')|float 
              ) | round(1)
          }}
        {% endif %}

    - name: Envoy 202221032900 Inverter 202147122858 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_202147122858')|default(0)|float 
                / states('sensor.envoy_inverter_average_power')|float 
              ) | round(1)
          }}
        {% endif %}

    - name: Envoy 202221032900 Inverter 202147123517 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_202147123517')|default(0)|float 
                / states('sensor.envoy_inverter_average_power')|float 
              ) | round(1)
          }}
        {% endif %}

    - name: Envoy 202221032900 Inverter 202147125027 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_202147125027')|default(0)|float 
                / states('sensor.envoy_inverter_average_power')|float 
              ) | round(1)
          }}
        {% endif %}

    - name: Envoy 202221032900 Inverter 202147125590 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_202147125590')|default(0)|float 
                / states('sensor.envoy_inverter_average_power')|float 
              ) | round(1)
          }}
        {% endif %}

    - name: Envoy 202221032900 Inverter 202147125734 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_202147125734')|default(0)|float 
                / states('sensor.envoy_inverter_average_power')|float 
              ) | round(1)
          }}
        {% endif %}

    - name: Envoy 202221032900 Inverter 202147125902 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_202147125902')|default(0)|float 
                / states('sensor.envoy_inverter_average_power')|float 
              ) | round(1)
          }}
        {% endif %}

    - name: Envoy 202221032900 Inverter 202147126079 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_202147126079')|default(0)|float 
                / states('sensor.envoy_inverter_average_power')|float 
              ) | round(1)
          }}
        {% endif %}

    - name: Envoy 202221032900 Inverter 202147126357 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_202147126357')|default(0)|float 
                / states('sensor.envoy_inverter_average_power')|float 
              ) | round(1)
          }}
        {% endif %}

    - name: Envoy 202221032900 Inverter 202147126997 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_202147126997')|default(0)|float 
                / states('sensor.envoy_inverter_average_power')|float 
              ) | round(1)
          }}
        {% endif %}

    - name: Envoy 202221032900 Inverter 202147128369 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_202147128369')|default(0)|float 
                / states('sensor.envoy_inverter_average_power')|float 
              ) | round(1)
          }}
        {% endif %}

    - name: Envoy 202221032900 Inverter 202147129445 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_202147129445')|default(0)|float 
                / states('sensor.envoy_inverter_average_power')|float 
              ) | round(1)
          }}
        {% endif %}

    - name: Envoy 202221032900 Inverter 202147130152 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_202147130152')|default(0)|float 
                / states('sensor.envoy_inverter_average_power')|float 
              ) | round(1)
          }}
        {% endif %}

    - name: Envoy 202221032900 Inverter 202147130290 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_202147130290')|default(0)|float 
                / states('sensor.envoy_inverter_average_power')|float 
              ) | round(1)
          }}
        {% endif %}

# -----------------------------------------------------------------------------
# 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 10 * *
    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 10 * *

  yearly_consumed:
    source: sensor.total_energy_from_grid
    name: Yearly Energy Consumption
    cron: 0 0 10 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 10 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') }}"

