Device_class / state_class clash

I have a couple of entities which track energy costs (in GBP/kWh) from my gas and electricity smart meters. Currently they only change when my tariff changes, but it’s not impossible I will end up on time-of-use tariffs shortly, leading to them changing multiple times per day.

Currently, these valuese are not held in statistics, because they have a device_class=“monetary” and when I set state_class=“measurement”, HA complains and tells me I can’t do this.

I’d like these values stored in statistics, so need state_class=“measurement” set. How do I do this and keep everything working? Will the Energy dashboard complain if I remove the device_class attribute?

You don’t need measurement, you need statistics. Post the error message please. You likely need to use a different state class.

Here’s the definition of one of the entities, with the state_class declaration commented out:

    - name: "Smart Meter Electricity: Import Unit Rate"
      unique_id: "smart_meter_electricity_import_unit_rate"
      state_topic: "glow/<redacted>/electricitymeter"
      device_class: "monetary"
      unit_of_measurement: "GBP/kWh"
#      state_class: "measurement"
      value_template: "{{ value_json['electricitymeter']['energy']['import']['price']['unitrate'] }}"
      icon: "mdi:cash"

If I uncomment the state_class declaration, I get this error:
Entity sensor.smart_meter_electricity_import_unit_rate (<class ‘homeassistant.components.mqtt.sensor.MqttSensor’>) is using state class ‘measurement’ which is impossible considering device class (‘monetary’) it is using; expected None or one of ‘total’; Please update your configuration if your entity is manually configured, otherwise create a bug report at GitHub · Where software is built

Since adding the state_class entry and removing it again (because of the above error), HA is now complaining and raising a repair…

So I need to set state_class to get long-term statistics, but then I get the error in italics above.

Use total, the error message says this.

You need to remove the monetary device_class, it’s the wrong choice for a price ($/unit) sensor.

Monetary is only for values which reflect a total balance, it is not generically for ‘anything to do with money’.

1 Like

OK - thanks. For the monetary entities (both GBP/kWh for consumption and GBP for daily fixed charge) I’ve removed device_class=monetary and reinstated state_class=measurement and had no errors on restart. So, good news.