The sensor must have the appropriate attributes. Check your entity attributes in Developer Tools > States to confirm the following:
device_class must be energy for electricity grid, solar, or battery categories. It must be gas for gas, or water for water.
state_class must be total or total_increasing.
The sensor must have an appropriate unit_of_measurement. See the help text for each category to see which units are accepted. Units containing an exponent must match superscript characters exactly as defined, e.g. m³ is accepted, m3 is not.If any of the attributes are not correct, please open an issue against the integration that provides your sensor, or if you are developing custom template sensors, make sure the templates have the correct settings.