How to enable power consumption statistics

In HA 2021.8 the power consumption page has been introduced and I want to use it.

I gather power consumption information from several smart sockets. And I have the template sensor which represents overall power consumption. But I can’t initialize new power consumption card because it’s said that there is no statistics.

On the pages Expected data source not listed - Home Assistant and https://developers.home-assistant.io/docs/core/entity/sensor#long-term-statistics I have read that I need a statistics sensor. I have tried to specify state_class for my sensor but it is not working:
Invalid config for [sensor.template]: [state_class] is an invalid option for [sensor.template]. Check: sensor.template->sensors->power_all->state_class. (See ?, line ?).

The sensor:

- platform: template
  sensors:
    power_all:
      unit_of_measurement: "Вт"
      value_template: "{{ ( (states('sensor.load_04cf8cdf3c867ea8') | float) + (states('sensor.load_00158d00039c0362') | float) + (states('sensor.load_bw') | float) + (states('sensor.load_00158d00035f627f') | float) + (states('sensor.load_00158d0003531cff') | float) + (states('sensor.load_00158d00039fa6af') | float) ) | round(1) }}"
      state_class: 'measurement'

I read that only the new template sensors support this.

What are the new template sensors?

https://www.home-assistant.io/integrations/template/#state-based-template-sensors

Have found it:

(from 123)

The documentation doesn’t show the state_class option for legacy Template Sensor. My understanding is that the energy integration includes entities containing that option (and set to measurement). Without that option, the entity is excluded.

Easy way to find out is to convert one or more of your Template Sensors from legacy to the new format and add state_class: measurement.

https://community.home-assistant.io/t/2021-8-0-feel-the-energy/326853/83

1 Like

It is not working anyway.
I’ve created this new sensor:

template:
  - sensor:
      - name: "power_all_new"
        unit_of_measurement: "Вт"
        state_class: measurement
        state: >
          {% set room = states('sensor.load_04cf8cdf3c867ea8')      | float %}
          {% set fridge = states('sensor.load_00158d00039c0362')    | float %}
         
          {{ (room + fridge) | round(1) }}

Also I’ve customized it like this (see Energy: Not showing the expected entities for consumption - #12 by francisp):

sensor.power_all_new:
  last_reset: '1970-01-01T00:00:00+00:00'
  device_class: energy
  state_class: measurement

But this new sensor power_all_new still is not showing on the new energy consumption page.

New sensor indeed has the measurement option:

Maybe you habe to wait:

After setting up a new device, it can take up to 2 hours for new data to arrive in your energy dashboard.

It seems that the sensor with unit kWh is needed. Not the ordinary Watts! It works now.

2 Likes

Ah, thanx for this info!

Of course, a sensor that shows Watts measures power, but you need an energy sensor (power over time).

My energy integration stopped updating the graph? No errors anywhere I can see. Any suggestions on how to fix or what to check?

Thanks all for your info in this post, it’s been helping me to progress this. Unfortunately I’ve not be able to get it working and I’ve tried both old and new template sensors.

I’m using a Shelly EM which works great and sends its data using MQTT. Here’s a simple card with the data items:

Here is the sensor defintion (shame the new method cant use friendly_name):

template:
  - sensor:
      - name: "house_energy_kwh"
        # friendly_name_template: "House Energy (Total)"
        unit_of_measurement: "kWh"
        state_class: measurement
        device_class: energy
        state: >
          {% set phase1 = states('sensor.shellyem3_8caab561f4aa_channel_a_energy') | float %}
          {% set phase2 = states('sensor.shellyem3_8caab561f4aa_channel_b_energy') | float %}
          {% set phase3 = states('sensor.shellyem3_8caab561f4aa_channel_c_energy') | float %}
          {{ float(phase1) + float(phase2) + float(phase3) }}

Here is the active sensor definition:

For some reason the new “Energy” configuration UI will not allow me select the “sensor.house_energy_kwh” entity as the “Grid Consumption” sensor:

Ive got the point where after 4 hours of trial and error i’m giving up for a while in the hope that HA will recognise the entity in the “Energy” UI once its populated with some more data…

Any and all ideas are welcome. Thx

1 Like

The ‘some’ reason is : there is no last_reset attribute.

template:
  - sensor:
      - name: "house_energy_kwh"
        # friendly_name_template: "House Energy (Total)"
        unit_of_measurement: "kWh"
        state_class: measurement
        device_class: energy
        state: >
          {% set phase1 = states('sensor.shellyem3_8caab561f4aa_channel_a_energy') | float %}
          {% set phase2 = states('sensor.shellyem3_8caab561f4aa_channel_b_energy') | float %}
          {% set phase3 = states('sensor.shellyem3_8caab561f4aa_channel_c_energy') | float %}
          {{ float(phase1) + float(phase2) + float(phase3) }}
        attributes:         
          last_reset: '1970-01-01T00:00:00+00:00'

2 Likes

Thanks @fancisp, that worked a treat and now I just need to wait for the values to populate. Thanks again and I hope this helps others :smiley:

1 Like

I have a problem too.
The “cost” is not recalculated to me.
But in the interface, the data is displayed.
My localization is “RU”
What am I doing wrong. Thanks

sensor:  
	- platform: integration
		source: sensor.charger_volt_power
		name: energy_spent
		round: 2

My settings:

I’m fighting with this, but no success, maybe someone can help?

Got you! I have changed W to Wh, however, nothing show up again, do I need to restert HA?
image
image

UPDATE:
Ok so what you need to do in order for the thing to work: make sure you have Energy kWh measurement in your system, I have it in configuration file.
If you have Power, which is instant measurements, you can change it to kWh, which mend that if you use instant data 1 hr and devide it to 1000 you will have kWh:

      - name: Pgrid 
        address: 25214
        scan_interval: 10
        slave: 4
        input_type: holding
        scale: 0.001
        offset: 0
        unit_of_measurement:  "kWh" 
        precision: 1
        device_class: energy

Then you should go to Configuration tab → Customization and find your device there and add state class: measurement:

Hit SAVE, check again your configuration files and restart HA!

And you still get nothing )))

Below two devices, one is MQTT and it works, other MODBUS and it doesn’t!

unit_of_measurement: kWh
friendly_name: Pgrid
device_class: energy
state_class: measurement

state_class: measurement
unit_of_measurement: kWh
friendly_name: Washing_Machine ENERGY Total
device_class: energy

You still need a last_reset

Where to add this?

You can’t do that.Use the integration integration

And use the left method.

1 Like