How to use external statistics (sensor:energy) in energy dashboard

Hi all,

I would like to use external statistics in the energy dashboard (e.g. as energy consumption). But they are not shown in the list of entities to select.

According to https://www.home-assistant.io/docs/energy/faq/#troubleshooting-missing-entities, the state_class and the device_class must be set correctly, but AFAIK both cannot be set for external statistics.

A user of my integration found an interesting way how to solve this, see https://github.com/caco3/homeassistant-statistics/blob/docs/energy-dashboard-external-statistics/docs/user/energy-dashboard.md, but manually changing a configuration which is not allowed in the GUI does not look that good to me.

Here https://community.home-assistant.io/t/im-adding-old-data-using-async-add-external-statistics-to-energy-i-cant-figure-out-how-to-show-cost/863868 a user was able to configure an external statistics to the energy dashboard, but at least I do not know this is possible at all.

So, is there a recommended way to use external statistics in the energy dashboard?

State class and device class aren't relevant for external statsitics, and are not required.

It may need to have unit_class set to "energy".

Thx for the lightning-fast answer. Can I set the unit-class in the UI and/or Yaml configuration for an external statistic? Or do I have to set the unit-class in the integration creating the external statistics?

There is no UI or yaml configuration for external statistics, so I'm not sure what you mean.

They can only be created by integrations. Where is your statistic coming from?

1 Like

The statistics are coming from my import statistics integration. Currently I am setting the unit_class to None.

So I have to update my integration.

I did not find any docu how to determine the unit converter used for unit conversions

AI found STATISTIC_UNIT_TO_UNIT_CONVERTER :

def get_unit_class(unit: str | None) -> str | None:
    """
    Resolve the unit_class for a given unit of measurement.

    Uses Home Assistant's STATISTIC_UNIT_TO_UNIT_CONVERTER to find the converter
    for the unit, then reverse-maps it to the unit_class string via
    UNIT_CLASS_TO_UNIT_CONVERTER.

    Args:
    ----
        unit: The unit of measurement string (e.g., "kWh", "°C"), or None

    Returns:
    -------
        str | None: The unit_class string (e.g., "energy", "temperature"),
                    or None if the unit has no compatible converter

    """
    if unit is None:
        return None

    converter = STATISTIC_UNIT_TO_UNIT_CONVERTER.get(unit)
    if converter is None:
        return None

    return _CONVERTER_TO_UNIT_CLASS.get(converter)

Thx a lot!

Yeah the documentation is a bit sparse here. :confused:

  • I can see for energy sensors it needs 'energy'.
  • For power is 'power'
  • For gas it can be 'volume' or 'energy'
  • For gas flow it is 'volume_flow_rate'
  • For water it is 'volume'
  • For water flow it is 'volume_flow_rate'

I updated my integration, its working now. Thx again!

1 Like