Custom energy sensor creation

Hello, i would like to create a custom enery sensor to count the correct energy.
“giorno_consumo_prese_pt” is a power meter that takes the all power on the lower flor but of course I have also dedicate energy power sensor which count the own consumption. I would like to remove those from the total but this is the error that i see later on the energy dashboard for the individual entity:
image

this is the custom sensor created:

- platform: template
  scan_interval: 60
  sensors:    
    giorno_consumo_prese_pt_updated:
      friendly_name: "giorno consumo prese pt updated"
      value_template:
        "{{ (states('sensor.giorno_consumo_prese_pt') | float(2) - (
        states('sensor.giorno_consumo_lavatrice_e_asciugatrice') | float(2) +
        states('sensor.giorno_consumo_presa_comandata') | float(2) +
        states('sensor.giorno_consumo_aspirapolvere') | float(2) +
        states('sensor.giorno_consumo_nouvelle_cuisine') | float(2))
        ) | round(2) }}"
      device_class: energy
      unit_of_measurement: kW

this is the helper created:

For starters, kWh is energy and kW is power. So you need to change the device class to power.

For your integration helper, you also want to use left sum instead of trapezoidal.

Assuming you got the unit wrong, and they are energy sensors, not power:

The sensor requires a valid state_class to be used in the energy dashboard.

You can not do this with legacy template sensors. You need to use the newer template integration.

You did not include the multi-line indicator ( >) in your template and you quoted a multi-line template (incorrect).

You also need an availability template or your sensor will generate weird spikes in the energy dashboard.

configuration.yaml (not sensors.yaml)

template:
  - sensor:    
      - name: "giorno consumo prese pt updated"
        state: >
          {{ (states('sensor.giorno_consumo_prese_pt') | float - (
              states('sensor.giorno_consumo_lavatrice_e_asciugatrice') | float +
              states('sensor.giorno_consumo_presa_comandata') | float +
              states('sensor.giorno_consumo_aspirapolvere') | float +
              states('sensor.giorno_consumo_nouvelle_cuisine') | float )
              ) | round(2) }}
        availability: >
          {{ has_value('sensor.giorno_consumo_prese_pt') and
             has_value('sensor.giorno_consumo_lavatrice_e_asciugatrice') and
             has_value('sensor.giorno_consumo_presa_comandata') and
             has_value('sensor.giorno_consumo_aspirapolvere') and
             has_value('sensor.giorno_consumo_nouvelle_cuisine') }}
      device_class: energy
      state_class: total # or total_increasing
      unit_of_measurement: kWh

If they are power sensors (kW) instead of energy sensors (kWh) then you are using the wrong sensors and need to use the energy sensors instead. If you dont have them then you need to make them with the Riemann Sum helper and use those in the template.

modify sensor as your suggestion below + used the left sum… result is that when i try to add the new sensor on the energy page it is not appearing.

    giorno_consumo_prese_pt_updated:
      friendly_name: "giorno consumo prese pt updated"
      value_template:
        "{{ (states('sensor.giorno_consumo_prese_pt') | float(2) - (
        states('sensor.giorno_consumo_lavatrice_e_asciugatrice') | float(2) +
        states('sensor.giorno_consumo_presa_comandata') | float(2) +
        states('sensor.giorno_consumo_aspirapolvere') | float(2) +
        states('sensor.giorno_consumo_nouvelle_cuisine') | float(2))
        ) | round(2) }}"
      device_class: energy
      unit_of_measurement: kWh

Please read my response:

I even gave you an example.

I confim it work only using “total_increasing” instead of “total” on configuration.yaml.
It is not really clear why i have to do it on configuration.yaml and cannot be done below custom_sensor folder or sensor.yaml.

This is the old way of defining template sensors: https://www.home-assistant.io/integrations/template/#legacy-sensor-configuration-format

It does not support a state_class option.

That old method of defining template sensors will no longer get updates. So it will never support a state_class.

The new method is a separate integration, it is not a sensor platform. This is why you can’t put it in your sensors.yaml file.

For new sensors you should use the new template integration to ensure they keep up with possible new features.