How to configure a Sensor so that is useable in in the energy Dashboard

I’m not an expert, but to clarify info from that link:

Your power sensor needs to have unit_of_measurement as W, and device_class of power. If you are configuring your KNX sensors via YAML, do so using the instructions here.
i think you will need to set state_class as measurement.

Once you have this sensor ready, then create a Reiman Sum Integral sensor via the helper section. The power sensor you created above should be available to select as the source, and it will automatically create a sensor with the appropriate unit and device_class based on the source. In this case kW/hr with device_class of energy. This will then be available in your Energy dashboard.

Thanks for coming back so fast :wink:

Unfortunately with KNX, device_class cannot be set. What worked though, is setting the state_class to “total_increasing”, which made the error regarding “last_reset” go away. Now the sensor seems to be accepted at least.

The unit for measurement for KNX sensors is already predefined by the type and can’t be defined with unit_of_measurement.

For Gas, there is a different problem, as I got the impression that the sensor would only be accepted when it is of device_class “gas”, which is (as stated above) not supported by the KNX funktion in HA.
So with kWh and m3 as units of measure being defined by the type, I’d guess the energy monitor is taking this for a electricity value and does not recognize it as gas (which is metered in m3 or kWh as well)

Is there a way to create kind of an generic alias object that can be fed by the KNX objekt but is presented to HA as a different entity?

CU
Frank

You can use customize: in yaml to modify any entities attributes.
Or wait till the next release. Add ability to set device class on knx sensor by elupus · Pull Request #81278 · home-assistant/core · GitHub
Some Knx type definitions already set device_class by default - wherever the DPT fits.

Cool, so I then need to wait. As kWh for electricity are the same DPT as kWh for gas… the DPT defines only the unit of measurement, not any type of usage. Hence I guess kWh are currently recognized as electricity.

In the meantime I am going to give that “customize:” tag a try

Thanks !
Cu
Frank

Ok, got it working. In case somebody else uses KNX sensors and can’t figure this out:

knx:    
  sensor:
    - name: "Haus Zählerstand Bezug"
      state_address: "13/4/31"
      type: active_energy_kwh
      state_class: total_increasing
      
    - name: "Haus Zählerstand Einspeisung"
      state_address: "13/4/32"
      type: active_energy_kwh
      state_class: total_increasing
      
    - name: "Gas Zählerstand m3"
      state_address: "13/5/0"
      type: volume
      state_class: total_increasing


homeassistant:
  customize:
    sensor.gas_zahlerstand_m3:
      device_class: gas
      unit_of_measurement: "m³"


Now you can use the Entities in the configuration for the energy dashboard. After some minutes you’ll see the dashboard populate.

Have a great weekend
Cu
Frank

ps: unfortunately, for GAS you can only use “m³” (be sure to not use “m3” instead) but not kWh, as we pay for gas in €/kWh and the calculation factor between m³ and kWh is slightly different in every area.

Thank you for the information because KNX associated with Home assistant is not usual and it is difficult to find information.
The gas consumption display works and I tried to reproduce the same for the water consumption but I encounter a problem.

knx_sensor.yaml

#Compteur gaz 
  - name: compteur_gaz
    state_address: "9/7/1"
    type: "volume"
    sync_state: every 30
    state_class: total_increasing

#Compteur eau (litres)
  - name: compteur_eau
    state_address: "9/7/12"
    type: "volume_liquid_litre"
    sync_state: every 30
    state_class: total_increasing

customize.yaml

sensor.compteur_gaz:
  device_class: gas
  unit_of_measurement: "m³"

sensor.compteur_eau:
  device_class: water
  unit_of_measurement: "L"

the device class “water” is not accepted and I have this error message

Value is not accepted. Valid values: “apparent_power”, “aqi”, “awning”, “battery”, “battery_charging”, “blind”, “carbon_dioxide”, “carbon_monoxide”, “cold”, “connectivity”, “current”, “curtain”, “damper”, “date”, “door”, “duration”, “energy”, “frequency”, “garage”, “garage_door”, “gas”, “gate”, “heat”, “humidity”, “illuminance”, “light”, “lock”, “moisture”, “monetary”, “motion”, “moving”, “nitrogen_dioxide”, “nitrogen_monoxide”, “nitrous_oxide”, “occupancy”, “opening”, “outlet”, “ozone”, “plug”, “pm1”, “pm10”, “pm25”, “power”, “power_factor”, “presence”, “pressure”, “problem”, “receiver”, “running”, “safety”, “shade”, “shutter”, “signal_strength”, “smoke”, “sound”, “speaker”, “sulphur_dioxide”, “switch”, “tamper”, “temperature”, “timestamp”, “tv”, “update”, “vibration”, “volatile_organic_compounds”, “voltage”, “window”.

The device class “water” is well mentioned in the documentation sensor (devices)
The problem is the same with the device class “volume”

I have similar issue. I have sonoff device that gives output as power W, i tried reimann through UI but … couldnt get it working, not shown anywhere in energy dashboard :frowning:

It’s not surprising for the sonos which exposes a power (in W) while the energy dashboard expects an energy (in Wh).

i know, this is why i tried riemann sum integral. I just dont know how to set it up, do i need to make it via configuration.yaml ?

And, im a complete noob on these things :slight_smile:

There’s no need to do anything in yaml anymore. You can do everything from the UI…

Create a new helper using the Riemann (make sure to give it a new name and select your sensor) than wait a bit and you should have the sensor available in the energy dashboard.

The new energy sensor can also be visible in the Developer tools > States… Take a screenshot from there with the attributes and share it here if you still having issues.

1 Like

Yes, thank you. I was just too hesitant, needed to wait for it to get something to show :slight_smile:

In my case with a value already in m3, so the Riemann conversion does not seem to be the solution?

Dear,
I created a Sensor (Arduino) which give a value in M3.
I need to convert to energy (Wh) by simply make a conversion (multiplication by +/- 10).
It is something similar to Riemann but no “integration” but easier a multiplication.
Is it possible to create a HELPER which do a multiplication and change the unit to Wh ?
If not, I need to do it in my Arduino and expose energy directly. But I suppose other device on the market expose volume and need to be converted to energy.
Thank a lot

You can create a simple template sensor.

Something like this:

template:
  - sensor:
    - name: Your new sensor name
      unique_id: your_new_sensor_unique_id
      unit_of_measurement: "Wh"
      device_class: energy
      state_class: measurement
      state: >-
        {% if is_number(states('sensor.your_existing_entity_id')) %}
          {{ states('sensor.your_existing_entity_id') * 10 }}
        {% else %}
          {{ states('sensor.your_existing_entity_id') }} # this will catch any 'unknown' states from the source sensor and replicate that to the new sensor
        {% endif %}
1 Like

Would the input sensor need to be in W or KW?

Hi,

I also have an issue like this. I use Tasmota and get entities for Power and “smartmeterreader_energy_total_in” and “smartmeterreader_energy_total_in_out” from my power meter in the Tasmota entities. All so fine, but I can not select the energies in the Energy Dashboard without the “trick” by creating a template sensor (see solution here: Energy consumption with Tasmota power monitoring plug).

But this does not satisfy me :wink:

I also tried to change the device_class, unit_of_measurement and state_class with the customize.yaml, which brings the needed effect of the entity to show up in the Energy Dashboard, but it doesn’t. So to vizualize it for this post here I have the following configuration:

customize.yaml:

#sensor.bitshake_smartmeterreader_energy_total_in:
#  device_class: energy
#  unit_of_measurement: 'kWh'
#  state_class: total_increasing
sensor.bitshake_smartmeterreader_energy_total_out:
  device_class: energy
  unit_of_measurement: 'kWh'
  state_class: total_increasing

And the entities look as follows (after complete restart of Home Assistant):

Although the “…_energy_total_out” has the correct attributes, it does not show up in the Energy Dashboard. I also tried it with state_class: total, but it was the same.

Can someone help here out please?

Thank you!

Ok, it even doen’t work with the solution I linked, so I have no solution yet.

Following problem with the solution which I linked. I added the following sensor templates in the configuration.yaml:

template:
  - sensor:
      - name: "Stromzaehler_Energie_Total_Bezug"
        unit_of_measurement: kWh
        device_class: energy
        state_class: total_increasing
        state: "{{ states('sensor.bitshake_smartmeterreader_energy_total_in')|float}}"
  - sensor:
      - name: "Stromzaehler_Energie_Total_Einspeisung"
        unit_of_measurement: kWh
        device_class: energy
        state_class: total_increasing
        state: "{{ states('sensor.bitshake_smartmeterreader_energy_total_out')|float}}"

And then I really can choose “Stromzaehler_Energie_Total_Bezug” and “Stromzaehler_Energie_Total_Einspeisung” in the Energy Dashboard, but get the following error:

Does anybody knows how to fix this?

Not it works, also without the template Sensor. Problem was, that I set in the customize.yaml

unit_of_measurement: 'kWh'

and it must be

unit_of_measurement: kWh

without the ticks. Furthermore I needed to remove the old statistics via Development Panel->Statistics. There it was shown that the entities have a problem, which can be fixed.

Now I have not template sensors in the configuration.yaml anymore and the following entity changes in the customize.yaml:

#Tasmota
sensor.bitshake_smartmeterreader_power_power_curr:
  device_class: power
  unit_of_measurement: W
  state_class: measurement
sensor.bitshake_smartmeterreader_energy_total_in:
  device_class: energy
  unit_of_measurement: kWh
  state_class: total
sensor.bitshake_smartmeterreader_energy_total_out:
  device_class: energy
  unit_of_measurement: kWh
  state_class: total

Now the energy entities can be selected in the energy dashboard!

1 Like

I was also having trouble with a template sensor through a riemann helper and adding the helper to the dashboard. As posted above it seems important that the template sensor has state_class: measurement.

Example:

- sensor:
    - name: "Current Power"
      unit_of_measurement: 'W'
      device_class: "power"
      state_class: measurement
      state: >
        {% if is_state('switch.some', 'on') %}
          400
        {% else %}
          0
        {% endif %}

After this I created a new Riemann integration using the Helper UI and was able to add the helper to the Energy Dashboard as an Individual device. Hope this helps.

I’m some how not able to figure out on why I can’t select this sensor in the HA dashboard:

    - name: "Growatt Solar PV Energy Today"
      state_topic: "energy/solar"
      value_template: '{{ ((value_json["PV1EnergyToday"] + value_json["PV2EnergyToday"]) | round(2)) }}'
      device_class: energy
      state_class: total_increasing
      unit_of_measurement: "kWh"

The individual ones from the strings can be selected without issues:

    - name: "Growatt Solar PV1 Energy Today"
      state_topic: "energy/solar"
      value_template: '{{ value_json["PV1EnergyToday"] }}'
      device_class: energy
      state_class: total_increasing
      unit_of_measurement: "kWh"

    - name: "Growatt Solar PV2 Energy Today"
      state_topic: "energy/solar"
      value_template: '{{ value_json["PV2EnergyToday"] }}'
      device_class: energy
      state_class: total_increasing
      unit_of_measurement: "kWh"

As far as I see there’s no difference really between them, all have the energy device_class, a correct unit and state_class