Yes, this beautiful thing:
Home Assistant introduces the Energy Dashboard here.
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:
- 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.
- Remove the old entity or remove the integration this entity is associated with.
- Reboot HA OS, or reload your docker container if you run core.
- Verify that the old entity ID has been removed in dev-tools->states.
- 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
) - Check that statistics have carried over. One way to do this is by checking that the energy dashboard still shows your old graphs.
- (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.
- (optional, only if you completed step 7) Update your Energy Dashboard config to the new entity IDs.
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
orstate_class: total
unit_of_measurement: kWh
orunit_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
andstate_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.
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
.
See also this explanation for a more detailed discussion on power and energy.
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.
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.
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:
- Add the desired functionality yourself, if you're a developer with some spare time. Getting started is very simple.
- Make a feature request here if you're not.
- Add the energy cards to your own custom dashboard. See also question #7.
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.
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 theis_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:
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
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:
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:
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:
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:
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
How do I hide it from the sidebar? I have no interest in this feature
I can exclude it by editing the sidebar locally, but I want a permanent solution to disable the sidebar entity altogether