Need help with yaml configuration for integrated sensor

hi
there is a situation a bit unclear to me.
i have the following config in yaml

    homeassistant:
      customize:
        sensor.dom_osveshchenie_moshchnost:
          friendly_name: дом:освещение:мощность
          unit_of_measurement: W
          device_class: power
          icon: mdi:laptop
        sensor.dom_osveshchenie_energiia:
          friendly_name: дом:освещение:энергия
          icon: mdi:chart-line
          state_class: total
          device_class: energy

    sensor:
      - platform: integration
        name: 'dom_osveshchenie_energiia'
        source: sensor.dom_osveshchenie_moshchnost
        unit_prefix: k
        round: 2

    template:

      - sensor:
          - name: 'dom_osveshchenie_moshchnost'
            state: >
                {% set all = 0 %}
                {% if states('switch.dom_kukhnia_liustra') == 'on' %}
                {% set all = all|float + states('input_number.dom_kukhnia_osveshchenie_moshchnost')|float %}
                {% endif %}
                {% if states('switch.dom_kukhnia_malenkii_svet') == 'on' %}
                {% set all = all|float + states('input_number.dom_kukhnia_malenkii_svet_moshchnost')|float %}
                {{ all }}

2 sensors are created - power and energy. The power sensor is a composite of two entities, and the energy sensor is an integral of the power sensor.
everything works, except for one thing: the energy sensor is not visible as a consumption source for the Energy dashboard.
when I create an integral using the UI for the power sensor above - this energy sensor is available in the Energy dashboard.
I conclude that something is missing in yaml, but my skills are not enough to solve it.
can someone help?

You have to specify the device_class as power on your template sensor.
The integration will then automatically become energy, and you’ll be able to use it in the energy dashboard.

EDIT: I see you set it via customization. Not really needed, and it might be the issue. Try setting it directly on the template sensor (same for the energy sensor, btw).

Hi! I understand the situation. It sounds like you’ve got your power sensor calculating correctly, and the integration platform is generating an energy sensor, but Home Assistant’s Energy dashboard isn’t recognizing it as a consumption source. You’re right, there’s likely a specific attribute or configuration missing in your YAML setup that the Energy dashboard expects.

The key difference between an integration created via YAML and one created via the UI for the Energy dashboard is often the set of device class and state class attributes. While you’ve already defined these in your homeassistant.customize section, it’s possible the integration platform isn’t carrying them over in a way the Energy dashboard fully recognizes.

Let’s try explicitly defining the device_class and state_class directly within your sensor platform configuration for the integration sensor. Here’s how you can modify your sensor configuration:

homeassistant:
  customize:
    sensor.dom_osveshchenie_moshchnost:
      friendly_name: дом:освещение:мощность
      unit_of_measurement: W
      device_class: power
      icon: mdi:laptop
    sensor.dom_osveshchenie_energiia:
      friendly_name: дом:освещение:энергия
      icon: mdi:chart-line
      state_class: total
      device_class: energy

sensor:
  - platform: integration
    name: 'dom_osveshchenie_energiia'
    source: sensor.dom_osveshchenie_moshchnost
    unit_prefix: k
    round: 2
    device_class: energy  # Explicitly defining device_class here
    state_class: total    # Explicitly defining state_class here

template:
  - sensor:
      - name: 'dom_osveshchenie_moshchnost'
        state: >
            {% set all = 0 %}
            {% if states('switch.dom_kukhnia_liustra') == 'on' %}
            {% set all = all|float + states('input_number.dom_kukhnia_osveshchenie_moshchnost')|float %}
            {% endif %}
            {% if states('switch.dom_kukhnia_malenkii_svet') == 'on' %}
            {% set all = all|float + states('input_number.dom_kukhnia_malenkii_svet_moshchnost')|float %}
            {{ all }}

By adding device_class: energy and state_class: total directly under the sensor platform for your dom_osveshchenie_energiia sensor, you’re ensuring that the integration entity is created with these crucial attributes. The Energy dashboard relies on these attributes to correctly identify energy consumption sensors.

After making this change, you’ll need to restart Home Assistant for the configuration to take effect. Once restarted, check your Energy dashboard configuration to see if sensor.dom_osveshchenie_energiia is now available as a consumption source.

Best regards,
Rosie
eztag

when i add

    device_class: energy  # Explicitly defining device_class here
    state_class: total    # Explicitly defining state_class here

directly under the sensor platform, I get an error when validating

Invalid config for 'integration' from integration 'sensor':
'device_class' is an invalid option for 'sensor.integration', check: device_class
'state_class' is an invalid option for 'sensor.integration', check: state_class

yes, that helped, thanks.

    template:

      - sensor:
          - name: 'dom_osveshchenie_moshchnost'
            state: >
                {% set all = 0 %}
                {% if states('switch.dom_kukhnia_liustra') == 'on' %}
                {% set all = all|float + states('input_number.dom_kukhnia_osveshchenie_moshchnost')|float %}
                {% endif %}
                {% if states('switch.dom_kukhnia_malenkii_svet') == 'on' %}
                {% set all = all|float + states('input_number.dom_kukhnia_malenkii_svet_moshchnost')|float %}
                {{ all }}
            device_class: power