Adding sensor template do Energy Dashboard

I have created a sensor template, that sums total energy from multiple sockets, working with valves and pumps for my house heating. I would like to add it to Energy Dashboard, as devices usage, but I’m not able to select it from the list - all the sockets are there, but not my template sensor. My code, for the sensor looks like that:

sensor:
- platform: template
  sensors:
    # zużycie ogrzewania
    energy_total_heating:
      friendly_name: 'Ogrzewanie (Zużycie)'
      value_template: "{{ (states('sensor.gosund_002_energy_total')|float + states('sensor.gosund_003_energy_total')|float + states('sensor.gosund_004_energy_total')|float + states('sensor.gosund_005_energy_total')|float + states('sensor.gosund_006_energy_total')|float)}}"
      unit_of_measurement: kWh  
      device_class: energy

It’s missing the state_class option.

However, you have defined this Template Sensor using what is now known as the “legacy” style which doesn’t directly support the state_class option. You will have to add it as a custom attribute in the customize.yaml file and then restart Home Assistant.

sensor.energy_total_heating:
  state_class: total_increasing

An alternative is to define the Template Sensor using the “modern” style because then it will support the state_class option directly.

template:
  - sensor:
      - name: Energy Total Heating
        state: "{{ (states('sensor.gosund_002_energy_total')|float + states('sensor.gosund_003_energy_total')|float + states('sensor.gosund_004_energy_total')|float + states('sensor.gosund_005_energy_total')|float + states('sensor.gosund_006_energy_total')|float)}}"
        unit_of_measurement: kWh  
        device_class: energy
        state_class: total_increasing
7 Likes

Thx a lot!
I was checking the state_class parameter, but configuration check rejected it, and I didnt know about legacy and customize options.
It works now :slight_smile:

1 Like

I have one little problem with this sensor. My WiFi lost connection few times, whit part of the measuring sockets, so the result of state was lower, then before, and in efect i got data jump in statistics. Looks like “total_increasing” in “state_class” dont block this kind of bad measurements. Is there a posibility to add a contition, that if the result i lower then last time, sensor template would keep the old value? (when WiFI gonna come backu, sum gonna be bigger and i can live with that).

For the two sensors, what value do they report that represents a bad measurement?

Ok, so the value from the sensors in statistics database looks fine:

But the data for the template sensor is corrupt:

Maybe I should sum statistics, not current values, if it is possible.

Love penny drop explanations