Providing a value for "Return to Grid" in the Energy dashboard

I’m a retired developer but a newb when it comes to HA. I learned quite a bit from getting my Energy Dashboard configured and I thought I’d share it in case it might be helpful to someone.

I have a 30 kW grid-tied solar array with 3 SMA inverters. I monitor energy consumption with 3 Emporia Vues on the entrance panels of my house, garage/pool and shop. These were easy to configure but the one piece still missing from my Energy dashboard was a source for the amount of energy sent back to the grid. I don’t monitor this directly but it’s a straightforward calculation. I thought maybe the dashboard would be able to calculate that easily enough but I ended up having to create a Template Sensor. I hadn’t done this before so it was an interesting exercise and I learned some things along the way.

The value I’m calculating for the export quantity is “the sum of the daily energy production from each of the solar inverters” (SMA integration) minus “the sum of the daily energy consumption from my 3 entrance panels” (Emporia Vue integration), which should equal my net energy export. Of course the energy export figure can’t be a negative. A value less than zero indicates that all the solar production is being consumed onsite.

I thought I might be able to do this as a helper from the UI but it’s too complicated and I ended up doing it in YAML. Here’s the code:

template:
  - sensor:
      - name: "Daily Net Energy Export"
        unique_id: 20250211-011400
        state_class: total
        device_class: energy
        unit_of_measurement: kWh
        state: >
          {% set day_usage_home = states('sensor.home_main_panel_energy_today') | float %}
          {% set day_usage_shop = states('sensor.shop_energy_today') | float %}
          {% set day_usage_garage = states('sensor.garage_energy_today') | float %}
          {% set day_solar_326 = states('sensor.sb7_7_326_daily_yield') | float %}
          {% set day_solar_331 = states('sensor.sb7_7_331_daily_yield') | float %}
          {% set day_solar_333 = states('sensor.sb7_7_333_daily_yield') | float %}
          {% set  day_net_export = (day_solar_326 + day_solar_331 + day_solar_333) - (day_usage_home + day_usage_shop + day_usage_garage) | round(2) %}
          {% if day_net_export > 0 %} day_net_export
          {% else %} 0  
          {% endif %}
        attributes:
          last_reset: >
            {{ state_attr('sensor.home_main_panel_energy_today', 'last_reset') }}

The card the value is plugged into was unhappy if the state_class wasn’t “total” and that it didn’t have a last_reset attribute. I just copied the value for the latter from one of the entities in the calculation.

Along the way I created this under a different name and even though I removed it from my configuration, the entity was still showing up in HA. I finally found a note in the forums that this happens when there is data in the history for a removed entity. The solution is to go to Develoer Tools>Statistics and remove the history. You may also have to clear your browser cache.

So this appears to all be working. I need a sunny day to see if the export energy is actually calculated correctly!

Due to the number of source sensors your template relies on you should definitely implement the availability template option:

Thanks for the suggestion. I saw availability in some examples but didn’t know what it was for.