Skip to content

Instantly share code, notes, and snippets.

@langestefan
Last active March 17, 2024 19:38
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save langestefan/acbf7e42b15b302e9aacc08663039e57 to your computer and use it in GitHub Desktop.
Save langestefan/acbf7e42b15b302e9aacc08663039e57 to your computer and use it in GitHub Desktop.
HA Energy Dashboard FAQ

Frequently Asked Questions for the Home Assistant Energy Dashboard

Yes, this beautiful thing:

image

Home Assistant introduces the Energy Dashboard here.

1. I'm replacing my [solar inverter, energy meter, ...], how can I retain my energy dashboard data?

Answer

Home Assistant stores energy dashboard related data in long term statistics, frequently referred to as LTS. In order to retain your energy dashboard data you must ensure the LTS are transferred from your old entity ID to the new one. To do so, follow these steps:

  1. Backup your Home Assistant, in case something goes wrong. It is very easy to mess this up and the result could be unrecoverable data loss.
  2. Remove the old entity or remove the integration this entity is associated with.
  3. Reboot HA OS, or reload your docker container if you run core.
  4. Verify that the old entity ID has been removed in dev-tools->states.
  5. Change the entity ID of the new entity to be identical to the old one (the one you just removed): (sensor.my_new_energy_device -> sensor.my_old_energy_device)
  6. Check that statistics have carried over. One way to do this is by checking that the energy dashboard still shows your old graphs.
  7. (optional, only from HA version >= 2023.4) Change the new entity ID to whatever you like, the statistics should carry over. This feature was added in Home Assistant 2023.4 so this does not work on older versions.

NOTE: The new entity ID can't have statistics associated with it already, or the data transfer will not work.

  1. (optional, only if you completed step 7) Update your Energy Dashboard config to the new entity IDs.

2. My entity is not listed when I try to add it to the energy dashboard. How can I make it visible?

Answer

First of all, make sure you are adding an energy [kWh or Wh] entity and not power [kW or W]. In case you have a device which provides instantaneous power only, read question #3 for instructions on how to calculate energy from instantaneous power data.

NOTE: Entities are only displayed in the drop down menu if they have generated valid statistics. If you added the integration that provides the energy sensor very recently (< 1 hour) it may take a while for Home Assistant to calculate valid statistics.

For an entity to be visible in the dropdown menu it must have the following attributes, which you can verify in dev-tools->states.

  • state_class: total_increasing or state_class: total
  • unit_of_measurement: kWh or unit_of_measurement: Wh
  • device_class: energy

If all of these attributes are set correctly check dev-tools->statistics to see if there is any issue that needs your attention. Simply pressing FIX ISSUE is enough in many cases.

  • If your entity is provided by an integration please create an issue on their GitHub page and refer to this FAQ.

  • If your entity is provided by a template sensor, you can specify the correct device_class, unit_of_measurement and state_class attributes directly in the template sensor YAML config. See the template sensor docs on how to do this. An example config of a correct energy sensor could be:

template:
  - sensor:
      - name: 'My Energy Sensor'
        device_class: energy
        unit_of_measurement: kWh
        state_class: total_increasing
        state: {{ state_attr('sensor.my_device', 'power_kw') | float }}
        availability: "{{ states('sensor.my_device') | is_number }}"  

See also question #9.

NOTE: This option is not available in the legacy sensor configuration format, so if you are still using this you will have to move over your YAML config to the more modern format to make these attributes available.

3. I have a power entity, but the energy dashboard only accepts energy entities. How can I use it?

Answer

Power in W is the rate at which energy in Wh is consumed. For example, if a 1kW load is on continuously for 1 hour it will have consumed 1 kWh. Thus to compute energy from power we need to integrate it over time. Luckily for us HA provides a convenient integration for this: Integration - Riemann sum integral, named after the famous German mathematician who first formulated it.

If you have a sensor that provides you with power readings in Watts (uses W as unit_of_measurement, device_class of power), then you can use the integration sensor to track how much energy is being spent. Take the next manual YAML configuration as an example:

sensor:
 - platform: integration
   source: sensor.current_power
   name: energy_spent
   unit_prefix: k
   round: 2

This configuration will provide you with sensor.energy_spent which will have your energy in kWh, as a device_class of energy.

image

See also this explanation for a more detailed discussion on power and energy.

4. My energy dashboard shows a weird spike of x kWh. How can I correct it?

Answer

How to correct the spikes

If you have a spike in your data, you can correct this in dev-tools->statistics.

Find the sensor there by searching for its entity ID and then press the icon on the right most side of the page.

image

You then can either:

  • Find the faulty value, and set it to 0.
  • Add a correction value in the same hour, which is the opposite of your faulty value.

NOTE: The states in this view are reported at an interval of 15 minutes, so it might take some effort to find the faulty value.

Background

To understand where this weird spike comes from, you need to understand how changes to the state of the sensor are interpreted by Home Assistant.

If the state_class of the sensor is total_increasing, a lower state (even from e.g. 1000 to 999) will be interpreted as a reset of the sensor. So in the example above, HA will think the sensor has been reset to 0 and then increased to 999 again. So the value used for the long term statistics will then be 1999 (1000 + 999). That means if for some reason your sensor will be 0 temporarily, and then go back the the correct state, the value will be doubled. Therefor it is really important to avoid decreases of the sensor value, which can be avoided by using a proper availability template in case you are using template sensors.

On the other hand, if the state_class is total a decrease of the sensor value will cause a decrease of the value in the long term statistics. So in case such a sensor drops to 0, you will see a negative spike, unless the last_reset attribute is set to the date and time of the reset, in that case it will be interpreted the same as for the total_increasing sensor, and new values after the reset will be added to the existing value.

5. My energy dashboard is not visible, and I can't configure it. How can I fix this?

Answer

Most likely you are not using default_config: in your configuration.yaml file. To make the energy dashboard visible add the following line to your config: energy:

6. I want feature x in the energy dashboard or I want to customize item y to my liking, how can I achieve this?

Answer

Unfortunately the frontend of the energy dashboard is currently not configurable. Customization will likely become possible in the future as functionalitity is constantly added to Home Assistant and the energy dashboard is very popular. In the meantime you can either:

7. I want finer time range control in the energy dashboard, is there a way to do this?

Answer

There is a popular feature request for this and the HA core PR for it is still open. In the meantime you can use the energy cards with a custom dashboard and combine it with the awesome energy-selector-period-plus community integration, available in HACS.

8. How can I access the calculated cost and compensation data calculated by the energy dashboard?

Answer

The energy dashboard automatically calculates the cost for grid consumption entities and the compensation for return to grid entities by writing the result to internally created entities. These monetary entities are named automatically according to the following format:

  • '<entity_id>_compensation' for return to grid entities.
  • '<entity_id>_cost' for grid consumption entities.

Where <entity_id> is the entity ID of the input sensor. For example, if your grid consumption entity has entity ID sensor.energy_consumption_tarif_2 then the cost entity will have the entity ID sensor.energy_consumption_tarif_2_cost. You can find these entities by navigating to dev-tools->states and searching for *_compensation or *_cost.

NOTE: Any modification to these entities outside the energy dashboard will likely break things, so be careful.

9. I want to add a device to the energy dashboard but the power consumption is only visible as a sensor attribute. How can I make it available?

Answer

We need to make the sensor attribute available as an entity using a template sensor.

For example, consider a sensor sensor.my_device which has an attribute called kwh_lifetime which displays the lifetime energy consumption:

  template:
     sensor:
        - name: "lifetime energy consumption"
          unit_of_measurement: "kWh"
          device_class: "energy"
          state_class: "total_increasing" 
          availability: "{{ states('sensor.my_device') | is_number }}"  
          state: "{{ state_attr('sensor.my_device', 'kwh_lifetime') | float }}"

This template will create an entity sensor.lifetime_energy_consumption with the state corresponding to the sensor attribute kwh-lifetime of the sensor sensor.my_device. The template sensor has the following components:

  • name: This sets the name of the sensor, in this case, "lifetime energy consumption".
  • unit_of_measurement: Specifies the unit of measurement for the sensor, which is "kWh" (kilowatt-hours) in this case. This is necessary to make the sensor visible in the energy dashboard.
  • device_class: Indicates the device class of the sensor. In this case, it's set to "energy" to signify that the sensor represents energy consumption. This is necessary to make the sensor visible in the energy dashboard.
  • state_class: This is set to "total_increasing". The total_increasing state class is used for sensors that continually increase over time, such as an accumulating energy consumption value. See this blog post for more details.
  • availability: This is a condition for the availability of the sensor. It checks whether the state of the input sensor (sensor.my_device) is a number, using the is_number filter. This is necessary to prevent undesired behaviour when the source sensor goes unavailable.
  • state: This is the actual state of the sensor, representing the lifetime energy consumption. It retrieves the value from the attribute 'kwh_lifetime' of the sensor named 'sensor.my_device'.

Consider another example where a sensor sensor.my_device has an attribute power_kw which displays the current power consumption:

  template:
     sensor:
        - name: "current power consumption"
          unit_of_measurement: "kW"
          device_class: "power"
          state_class: "measurement" 
          availability: "{{ states('sensor.my_device') | is_number }}"  
          state: "{{ state_attr('sensor.my_device', 'power_kw') | float }}"

NOTE: If we want to use this sensor in the energy dashboard we need to integrate it first, since the time integral of power is energy. To learn how to do this check question #3.

10. I have a power entity which goes negative when returning energy to the grid. How do I split this into two entities, one for energy production and one for energy consumption?

Answer

We first handle the split by creating two entities which are both non-negative, one for production and one for consumption:

$$P_{\textrm{consumption}} = \left\lbrace \begin{array}{ll} P_{\textrm{in}} & P_{\textrm{in}} > 0 \newline 0 & P_{\textrm{in}} \leq 0 \end{array} \right.$$ $$P_{\textrm{production}} = \left\lbrace \begin{array}{ll} -P_{\textrm{in}} & P_{\textrm{in}} < 0 \newline 0 & P_{\textrm{in}} \geq 0 \end{array} \right.$$
template:
   sensor:
      - name: "Grid Power Consumption"
        unit_of_measurement: "W"
        device_class: "power"
        state_class: "measurement" 
        availability: "{{ states('sensor.my_device') | is_number }}"  
        state: >
          {{ max([states('sensor.my_device') | float, 0]) }}
      - name: "Grid Power Production"
        unit_of_measurement: "W"
        device_class: "power"
        state_class: "measurement" 
        availability: "{{ states('sensor.my_device') | is_number }}"  
        state: >
          {{ max([states('sensor.my_device') | float * -1, 0]) }}  

We then individually integrate these sensors to create kWh sensors which tracks the total energy consumption and production:

sensor:
 - platform: integration
   source: sensor.grid_power_consumption
   name: Grid Energy Consumption
   unit_prefix: k
   unit_time: h
   round: 2
 - platform: integration
   source: sensor.grid_power_production
   name: Grid Energy Production
   unit_prefix: k
   unit_time: h
   round: 2  

11. How can I display historical energy cost or consumption in a timeseries chart?

Answer

Historical energy consumption and costs are stored on a per-entity basis in appropriately named long-term statistics. These hourly aggregated statistics are stored separately from the normal entity state and are never deleted. For utility entities such as electricity and gas only the growth is stored.

To display energy entity statistics in a chart we need 3 components: an entity with valid statistics, a chart that can display statistics and correct chart settings. For displaying long term energy costs calculated by the energy dashboard we also need to know the cost entity ID, how to get this is explained in question #8.

Below are some examples to get you started.

1. Displaying long-term energy consumption in a statistics graph card

Using the built-in statistics graph card we can display the daily energy consumption of a TV for the past 31 days:

image
Code
chart_type: bar
period: day
type: statistics-graph
title: TV Energy Consumption
entities:
  - entity: sensor.samsung_tv_daily_energy_usage
stat_types:
  - change
days_to_show: 31
hide_legend: true

2. Using more advanced display options with ApexCharts

With ApexCharts we have access to a lot more graphical features:

image
Code
type: custom:apexcharts-card
span:
  end: day
experimental:
  color_threshold: true
graph_span: 7d
locale: en
apex_config:
  chart:
    type: area
  stroke:
    show: true
    width: 1
    curve: smooth
  legend:
    show: true
  xaxis:
    labels:
      format: ddd
  yaxis:
    min: 0
    forceNiceScale: true
header:
  title: Production solarpanels
  show: true
  show_states: true
  colorize_states: true
series:
  - entity: sensor.solaredge_energy_this_day
    type: column
    show:
      datalabels: true
      name_in_header: false
    statistics:
      type: sum
      period: hour
    group_by:
      func: diff
      duration: 24h
    color_threshold:
      - value: 0
        color: '#000000'
      - value: 2
        color: '#CC0000'
      - value: 3
        color: '#FFA500'
      - value: 4
        color: '#006400'
      - value: 5
        color: '#82D305'

3. Combining several data sources in one chart

We can combine energy consumption and ambient temperature to see how our heating system performs on cold days:

image
Code
type: custom:apexcharts-card
span:
  end: day
header:
  title: Airco Energy Consumption
  show: true
  colorize_states: true
graph_span: 7d
locale: en
apex_config:
  chart:
    type: area
  xaxis:
    labels:
      format: ddd
yaxis:
  - id: first
    decimals: 0
    apex_config:
      tickAmount: 4
      forceNiceScale: true
  - id: second
    opposite: true
    decimals: 0
    apex_config:
      tickAmount: 4
      forceNiceScale: true
series:
  - entity: sensor.airco_energy
    name: Airco Energie
    yaxis_id: first
    type: column
    show:
      datalabels: true
    statistics:
      type: sum
      period: hour
    group_by:
      func: diff
      duration: 24h
  - entity: sensor.bresser_weather_station_temperature
    name: Weerstation
    yaxis_id: second
    type: line
    show:
      datalabels: true
    group_by:
      func: min
      duration: 24h

4. Displaying cost

Using the energy cost entity (calculated by the energy dashboard) we can display costs in our charts:

image
Code
type: custom:apexcharts-card
graph_span: 7d
locale: en
span:
  end: day
apex_config:
  chart:
    type: area
  stroke:
    show: true
    width: 1
    curve: smooth
  legend:
    show: true
  xaxis:
    labels:
      format: ddd
  yaxis:
    min: 0
    forceNiceScale: true
header:
  title: Energy price
  show: true
  show_states: true
  colorize_states: true
series:
  - entity: sensor.energy_consumption_tarif_1_cost
    type: column
    show:
      datalabels: true
    statistics:
      type: sum
      period: hour
    group_by:
      func: diff
      duration: 24h
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment