Use a virtual sensor in energy card for returned to the grid

Hello all,
I would like to add in the energy card the power returned to the grid.
I have a sensor measuring the power consumption of the house, and a sensor of the power produced by solar panels.
I thus create a virtual sensor in the configuration.yaml

 - name: "Corrente restituita"
    unit_of_measurement: "kW"
    icon: mdi:flash
    state: >
      {% set consumo = states('sensor.module_din_connesso_1_power') | float %}
      {% set solare = states('sensor.solaredge_current_power') | float %}
      {% if consumo >= solare %}
        0
      {% else %}
        {{ (solare - consumo) | round(3, default=0) }}
      {% endif %}

This returns the correct difference, I can browse data and see charts, in kW.

I then create a Riemann helper, based on this virtual sensor.
The output is in kWh, and I can see that is summing returned power (keeps summing since the moment I created this helper, now at 131 kWh, not resetting from 0).

Comparing this helper with, for example, the solar panel sensor (named solar lifetime energy, in kWh and never resetting at 0), why can’t I choose my helper in the energy panel as returned to the grid? It does not appear in the drop down menu.

Thank you very much,
Fabio

Add these two lines to your template sensor:

 - name: "Corrente restituita"
    unit_of_measurement: "kW"
    icon: mdi:flash
    device_class: power # this
    state_class: measurement # and this
    state: > etc...

This way your Riemann Sum sensor should gain the following attributes (required for the energy dashboard):

state_class: total
device_class: energy

If it does not have these attributes after a restart you might have to delete the Riemann Sum sensor and create it again. Or just add the attributes using customize.

Also to save you a lot of issues later change your template sensor to have defaults and add an availability template, like this:

    state: >
      {% set consumo = states('sensor.module_din_connesso_1_power') | float(0) %}
      {% set solare = states('sensor.solaredge_current_power') | float(0) %}
      {% if consumo >= solare %}
        0
      {% else %}
        {{ (solare - consumo) | round(3, default=0) }}
      {% endif %}
    availability: >
      {{ states('sensor.module_din_connesso_1_power') | is_number and
         states('sensor.solaredge_current_power') | is_number }}
1 Like

Hello Tom,
thank you very much for your precious help!
I’ve been able modify the sensor and it works!
It’s been running for a couple of days now.
Just one doubt on your reply:

Or just add the attributes using customize.

I deleted and created the Riemann Sum again, because I couldn’t find how to add them manually, but if you could, how would I do this customization manually? Thank you very much!

Fabio

Like this: Customizing entities - Home Assistant

1 Like

Could you please share the full modified sensor setup for this? Thanks. :slight_smile:

Sure! Here is what I wrote in my sensors.yaml file thanks to Tom

  - name: "Corrente restituita"
    unit_of_measurement: "kW"
    icon: mdi:flash
    device_class: power
    state_class: measurement
    state: >
      {% set consumo = states('sensor.module_din_connesso_1_power') | float(0) %}
      {% set solare = states('sensor.solaredge_current_power') | float(0) %}
      {% if consumo >= solare %}
        0
      {% else %}
        {{ (solare - consumo) | round(3, default=0) }}
      {% endif %}
    availability: >
      {{ states('sensor.module_din_connesso_1_power') | is_number
      and
      states('sensor.solaredge_current_power') | is_number }}

Fabio