# 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: Total Energy From Grid
    platform: integration
    device_class: energy
    source: sensor.emporia_vue_net_power_from_grid
    unit_prefix: k
    unit_time: h

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

  - name: Home Total Energy
    platform: integration
    device_class: energy
    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
        }}

