Integration - Riemann sum integral for solar panels showing 'Unexpected device class'

Hi! New to the forum and HA overall.

I want to show my solar panel production in the energy tab.

I created a new sensor with the Riemann sum integral GUI under Settings > Devices & services > Helper Tab > + Create Helper > Integration - Riemann sum integral sensor. I followed this guide.

Added the integration with these settings:

And I can view the kwH as I want to if I view the sensor directly as it shows the current kwH and a timeline.

but when I add it to the Energy Tab I get the following message:
'Unexpected Device Class
The following entities do not have the expected device class:

  • sensor.solcellor_nuvarande_produktion’

sensor.solceller_nuvarande_produktion is the name of my newly created helper.

Did I make a mistake somewhere or if there is another way of creating a device_class energy.

Sorry I couldn’t post more images since I’m a new user.

You also need to do the last part of the guide, under energy.

No they don’t. That is equivalent of what they have done in the UI.

@Qvint go to developer tools → states. Paste the attributes of both the source sensor and Rieman Sum sensor here.

Thank you for the response!

They are already included in the sensor list:

Are the not configured correctly?

It’s missing the device_class, did you restart home assistant after adding the sensor, try that first.

image

That worked, thank you!

@Qvint Sorry if this is a stupid question but where did you add the device class please? I have the same issue and can see it is missing but not were to add it. Thanks

Hi @pete101 I came here wondering the same - and read the last part of the linked Riemann integrataion under Energy: It will work if you add device_class: power to you power sensor which you use as input to the integration helper (Riemann sum). It will then automatically get energy as device class it seems.

For completeness, these are two of my power sensors I have made, which I use as input to the Riemann sum:

  - platform: template
    sensors:
      hottub_pump2_power_consumption:
        friendly_name: "Hottub pump 2 power consumption"
        device_class: power
        unit_of_measurement: "kW"
        value_template: >
          {% set kilowatts_when_on = 3.000 %}
          {{ is_state_attr('fan.hottub_pump_2', 'preset_mode', 'HI')  * kilowatts_when_on }}                                                                                                                                                
  - platform: template
    sensors:
      hottub_blower_power_consumption:
        friendly_name: "Hottub blower power consumption"
        device_class: power
        unit_of_measurement: "kW"
        value_template: >
          {% set kilowatts_when_on = 0.700 %}
          {{ is_state('switch.hottub_blower', 'on') * kilowatts_when_on }}                                                                                                                                                                  

Note that I needed to trigger a change of state before the unit came into play correctly. Naturally I also restarted HA after changing the yaml config.

@mgee Thanks for the info. I had tried a device class of energy but not power on the source sensor. However that also didn’t work as the sensor doesn’t seem to accept a device class at all. I am using a command line sensor rather than a template so that maybe my issue.

Config:

- sensor:
      command: 'curl -u "[email protected]:*********" -X POST -H "Content-Type: application/json" -d "{\"id\":xxxxx}" https://mihome4u.co.uk/api/v1/subdevices/show'
      name: House Monitor
      unique_id: house_monitor
      device_class: power
      unit_of_measurement: W 
      value_template: '{{ value_json.data.last_data_instant }}'

And this is all that shows in dev tools

unit_of_measurement: W
friendly_name: House Monitor

I think the problem you have might be unrelated to the initial post here - I think it could be the command_line platform that is the problem - it has recently changed in HA. This is how I use it in version 2023.7.1:

command_line:
  - sensor:
      name: RPi CPU Temp
      command: "cat /sys/class/thermal/thermal_zone0/temp"
      unit_of_measurement: "°C"
      value_template: '{{ value | multiply(0.001) | round(1) }}'

I have no device_class there, but I think it can be added. I think it is best to create a new post if this doesn’t work for you, feel free to @ mention me there and I will try to help out.

1 Like

Have the same issue and a reboot / restart does not correct this
The following entities do not have the expected device class:
image

image

so was able to add the “device_class: power” using customize section in config.yaml
image

However it still complains about it not having this setting
Even after a restart.

QUICK Update:
Got this corrected. I changed POWER to ENERGY and this seems to satisfy the error (no more error)
image
image

Hello dear HA enthusiasts,
Like you in this thread, I’m running into this
image

So, I followed the advice in this thread to go to
Developer tools → States
and then one by one add the device_class: energy for each one that is missing
image

So I add ad line 6 the device_class: energy line and click on SET STATE button
Then it seems ok.
Then when I restart HA, all is gone again and I can start doing it again.

What am I doing wrong?
Why is it nog keeping the the added line?

3 Likes

I am having this exact issue. It was working just fine for the last couple months until recently it wanted the device class for one sensor… now it wants it for all my sensors. It’s broken this whole “Energy Dashboard” feature for me completely.

Edit 1:
It will remember the time stamp for the last change. even move the line into the correct spot, but it is not persistent after a restart.

Edit 2:
I redid every entity I had “Integration - Riemann sum integral sensor” I had to calculate the expected kWh reading that “Energy Dashboard” was expecting, and it worked… until I restarted, and it broke everything and put it back the way it was… but then would correct itself after a couple minutes… seems to be working now for most of my outlets from one brand. the outlets from another brand seem to not work, even though they show in Watts, but the “Integration - Riemann sum integral sensor” is unable to convert them kWh, and just shows unknown, even though it is reading out Watts.

Ran into the same issue. As it’s not possible to set the device_class in the helper and setting states manually is not retained after a restart, I did it in configuration.yaml, like this:

homeassistant:
  packages: !include_dir_named packages
  customize:
    sensor.kwh_koelkast:
        device_class: energy
    sensor.kwh_oven:
        device_class: energy

doing this won’t persist state over restarts.

Whats very bizarre here is that the first 2 Reimann helpers I added correctly had the device_class: energy.

The third one doesn’t

When I tried to use the customized config as above, all 3 now have lost the device class!

You need to ensure the source sensor has the correct device class (power) and state class (measurement). The Riemann Sum will then generate it’s own classes correctly.

Hi Tom, I appreciate your help,

I compared all 3 devices -they are identical wifi-plugs. All 3 source sensors that I am using in the Reimann Sum have the device_class power. The first 2 devices Reimann Sum generate the device_class: energy, the 3rd one doesn’t.

After tinkering with setting the cusomize.yml to add the device_class: energy to the 3rd device’s Reimann Sum helper, it still doesn’t pick it up.

Now randomly the 2nd device no longer has device_class: energy.

I’ve attached an image of the diff of the data files that are generated when clicking “Download Diagnostics” on the device screen.

The left is the plug that is not working, the right is the plug that is working. The only diff between them, (besides the dates and names) is the device_class.

I can’t see any reason that this plug would not pick up the device_class based on the information in this thread.

Has anyone got some pointers for how to dig deeper into home assistant to uncover whats happening here?

EDIT:

Ok so reading the code for the helper, it seems the device_class is only updated when the source state updates. This device_class state on the helper doesn’t seem to persist on server restarts, so it has to wait for a new change in the source state.

You can trigger the helper state to update by using the developer console to update the source state.

What is strange though is that while the other 2 devices appear to update the helper state within a few mins, this 3rd one didn’t, ironically this one has much higher usage.

EDIT 2:

Ok so I figured out what’s causing me grief. After realising that the helper won’t be tagged with “device_class: energy” until the source sensor receives an update, I went hunting down for issues related to updates and my device.

I’m using a Kogan Smart Plug device with the Tuya Smart Life app. I found that this is a common problem using Tuya Cloud Tuya smart plug energy monitoring values not updating · Issue #61224 · home-assistant/core · GitHub

I’ve switched to using tuya local and updates are now fairly quick.

I cant seem to add this
I am losing the entity every time there is an update any idea how I get it to keep it