How to change a sensor to an "energy" sensor for energy dashboard

I have many sensors from my solar systems and emporia energy monitors that work on my own dashboards but not on the main HA energy dashboard.
3 of these are calculations from other sensors as you can see in my yaml.config file
how do I make my sensors into energy sensors ? I think they need to have device_class: energy
and state_class: measurement
I am new to HA and would appreciate any help
here is my current yaml
I put the 3 backticks not sure why it did format like yaml?

default_config:
# Text to speech
tts:
  - platform: google_translate
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
ffmpeg:
sensor:
  - platform: template
    sensors:
      gridtie_totalsolar:
        friendly_name: "Grid Tie Solar Generation - Total"
        value_template: "{{ states('sensor.sb6_0_1sp_us_41_511_grid_power') | float(0)+ states('sensor.grid_tie_input_2_1min') | float(0)}}"
        unit_of_measurement: 'W'
       
      gridtie_totalload:
        friendly_name: "Grid Tie Total Load"
        value_template: "{{ states('sensor.bunny_house_totalusage_1min') | float(0)+ states('sensor.grid_system_totalusage_1min') | float(0)}}"
        unit_of_measurement: 'W'
        
      gridtie_return_to_grid:
        friendly_name: "Grid Tie Return to Grid"
        value_template: "{{ states('sensor.gridtie_totalsolar') | float(0)- states('sensor.gridtie_totalload') | float(0)}}"
        unit_of_measurement: 'W'  
    

These are all power sensors (measured in W or kW).

They should have the attributes:

device_class: power
state_class: measurement

The energy dashboard takes energy sensors (measured in Wh or kWh).

If your devices do not report energy consumption you can calculate it from the power sensors using the Riemann Sum integration:

Integrating power with respect to time gives you energy.

Pay particular attention to the Integral method option. You probably want the left method.

These will be always increasing sensors. Which is fine for the energy dashboard but if you want daily/weekly/monthly energy sensors you can feed the sensors created with the Riemann integration to Utility meter helpers:

Thanks for the help Tom.
I was able to add the reimann integration, and it allowed me to create energy entities based on my sensors. just waiting for the data to start flowing.

question…I have a ct sensor that measures watts between the elec meter and my elec panel, it measures grid flow and can be positive or negative. sometimes the grid sends me power and other times my solar sends power to the grid.
the energy dashboard has a section for power from the grid and another section for power returned to grid.
any idea how or if it possible to handle this situation ?
thanks again for the help

You need to stop confusing power and energy they are very different things. The energy dashboard only uses energy sensors. Not power sensors. So, “the energy dashboard has a section for energy from the grid and another section for energy returned to grid”.

You would require a couple of template sensors to split your power sensor into two parts (to grid and from grid). Then feed those two to the Riemann Sum integration to get two energy sensors you can use in the dashboard.

Something like this (i guessed your sensor name, you will probably have to change it):

template:
  - sensor:
      - name: Power to Grid
        unit_of_measurement: W
        state_class: measurement
        device_class: power
        availability: "{{ states('sensor.grid_power_flow')|is_number }}"
        state: >
          {% if states('sensor.grid_power_flow')|float(0) <= 0 %}
            {{ states('sensor.grid_power_flow')|float(0)|abs }}
          {% else %}
            0
          {% endif %}

      - name: Power from Grid
        unit_of_measurement: W
        state_class: measurement
        device_class: power
        availability: "{{ states('sensor.grid_power_flow')|is_number }}"
        state: >
          {% if states('sensor.grid_power_flow')|float(0) >= 0 %}
            {{ states('sensor.grid_power_flow') }}
          {% else %}
            0
          {% endif %}

Remember: These power sensors must go to the Riemann Sum integration to be converted to energy sensors. You can use those in the energy dashboard.

thanks, I will give it a try

good morning Tom
I got the yaml updated and created the new sensors
when the power flows FROM the grid it looks like it is working
when the power flows TO the grid, I am getting a non-numeric error, in the template sensor and the riemann sensor

|abs(0) should just be |abs

thanks, I made the change, and the new energy entities are correct
I added the return to grid on the energy dashboard and hopefully it will update shortly

Its now working
thanks for sharing your expertise

1 Like

Hola ,tengo el mismo problema , yo uso unos pzem4 y tengo uno a la entrada de la acometida y otro a la salida del inversor ,e usado su codigo en en el apartado configuration.yaml pero no me sale lo importado de la red, solo sale lo mismo que genero en solar
lo de | abs (0) hay que quitarlo o ponerlo me podrian ayudar _?? quiero ver los excedentes volcados a la red pero solo los veo sumados a lo consumido de la red .
gracias

English only please.

Hello, I have the same problem, I use some pzem4 and I have one at the input of the feeder and another at the output of the inverter, I used your code in the configuration.yaml section but I don’t get what is imported from the network, only it comes out the same as generated in solar
what of | abs (0) you have to remove it or put it in. Could you help me _?? I want to see the surpluses transferred to the network but I only see them added to what is consumed by the network.
thank you

This would be my config in configuration.yaml
#Tarjeta energia (importar-exportar red)#
template:

  • sensor:
    • name: Power to Grid
      unit_of_measurement: W
      state_class: measurement
      device_class: power
      availability: “{{ states(‘sensor.consumo_casa_energy_power’)|is_number }}”
      state: >
      {% if states(‘sensor.consumo_casa_energy_power’)|float(0) <= 0 %}
      {{ states(‘sensor.consumo_casa_energy_power’)|float(0)|abs(0) }}
      {% else %}
      0
      {% endif %}

    • name: Power from Grid
      unit_of_measurement: W
      state_class: measurement
      device_class: power
      availability: “{{ states(‘sensor.consumo_casa_energy_power’)|is_number }}”
      state: >
      {% if states(‘sensor.consumo_casa_energy_power’)|float(0) >= 0 %}
      {{ states(‘sensor.consumo_casa_energy_power’) }}
      {% else %}
      0
      {% endif %}

It would be good? in network I add: import and in network dump: export
but I only see the graph of the solar panels on both screens. I’ve been waiting for a few hours for it to update but nothing