# Shelly Pro 3EM + PV calculations:
- sensor:
    - name: "Energy from grid"
      unique_id: power_import_modern
      unit_of_measurement: "W"
      device_class: power
      state_class: measurement
      state: >
        {% set a = states('sensor.shellypro3em_09fe9ef_phase_a_active_power') | float(0) %}
        {% set b = states('sensor.shellypro3em_09fe9ef_phase_b_active_power') | float(0) %}
        {% set c = states('sensor.shellypro3em_09fe9ef_phase_c_active_power') | float(0) %}
        {% set ia = a if a > 0 else 0 %}
        {% set ib = b if b > 0 else 0 %}
        {% set ic = c if c > 0 else 0 %}
        {{ (ia + ib + ic) | round(2) }}

    - name: "Energy to grid"
      unique_id: power_export_modern
      unit_of_measurement: "W"
      device_class: power
      state_class: measurement
      state: >
        {% set a = states('sensor.shellypro3em_09fe9ef_phase_a_active_power') | float(0) %}
        {% set b = states('sensor.shellypro3em_09fe9ef_phase_b_active_power') | float(0) %}
        {% set c = states('sensor.shellypro3em_09fe9ef_phase_c_active_power') | float(0) %}
        {% set ea = a if a < 0 else 0 %}
        {% set eb = b if b < 0 else 0 %}
        {% set ec = c if c < 0 else 0 %}
        {{ (ea + eb + ec) | abs | round(2) }}

    - name: "Estimated Power Consumption"
      unique_id: power_consumption_modern
      unit_of_measurement: "W"
      device_class: power
      state_class: measurement
      state: >
        {% set pv = states('sensor.pv_smartdose_power') | float(0) %}
        {% set imp = states('sensor.energy_from_grid') | float(0) %}
        {% set exp = states('sensor.energy_to_grid') | float(0) %}
        {% if exp > 0 %}
          {% if pv > exp %}
            {{ (pv - exp + imp) | round(2) }}
          {% else %}
            {{ imp | round(2) }}
          {% endif %}
        {% else %}
          {{ (pv + imp) | round(2) }}
        {% endif %}

    - name: "Estimated PV Consumption"
      unique_id: pv_consumption_modern
      unit_of_measurement: "W"
      device_class: power
      state_class: measurement
      state: >
        {% set pv = states('sensor.pv_smartdose_power') | float(0) %}
        {% set exp = states('sensor.energy_to_grid') | float(0) %}
        {% if pv > exp %}
          {{ (pv - exp) | round(2) }}
        {% else %}
          0
        {% endif %}


