Ability to set state_class on modbus sensor

The new energy features of 2021.8 allow you to set different sources of energy (solar).
I measure my solar panels with a Modbus kWh sensor, but that one doesnt show up.

It’s most likely because we cannot set the state_class to ‘measurement’ right now, so that’s the feature request, to add state_class as an option to the modbus sensors (similair to what has already been done to the mqtt sensor).

(The part about long term statistics)

(mqtt sensor has the state_class option)

+1 for this, I need it to track all the energy from my Victron modbus stuff

2 Likes

Also +1, I am using Modbus to read out Eastron SDM630 electricity meter. Does not work out of the box with new Energy stuff, so I am doing “overlay” counter with Utility Meter integration.

Until then you can just customize it to your existing sensors.

(It’s currently not stated in the docs, but should work)

I’ve created a PR fro state_class, but since I don’t use Modbus someone should battle-test it.

3 Likes

That doesn’t work for me.

I use modbus integration to get the energy values from my Alpha Ess smile T10 storage system.

1 Like

+1 from me also, using modbus to read SMA and Kostal inverters, and also their home energy meter via the inverters modbus.

What I actually tried and thought should work: using platform template sensors and add the state_class here, but also not supported:

- platform: template
  sensors:
    ac_power_combined:
      friendly_name: "AC power combined"
      icon_template: mdi:solar-power
      unit_of_measurement: W
      device_class: power
      state_class: measurement
      value_template: >-
          {% if states('sensor.ac_power_stp8')|int > 0 %}
          {{ states('sensor.ac_leistung_stp10')|int + states('sensor.ac_leistung_stp8')|int }}
          {% else %}
          unknown
          {% endif %}

And also read in How to enable power consumption statistics - #13 by francisp that

  attributes:
       last_reset: '1970-01-01T00:00:00+00:00'

is required to use a sensor in statistics, but also not supported in platform template sensor.

A bit off-topic, but would be especially great if HA’s energy integration would support automatic (Riemann sum) integration for power readings instead of energy readings out of the box.

+1 on this, i haven’t really looked into it but i think i’d have to use history/stats to then fake a sensor in kwh for the energy integration, all my readings are currently in W

+1 I’m using MODBUS to track my HYBRID INVERTER. I have all my energy consumption, energy generation there:

So what should I change “pgrid” to in order to get it to show up in drop/down menu:

Power won’t show, no matter what you change. You need energy. (Wh instead of W)

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

Since your power won’t be linear you should use a Riemann Integral to calculate energy. But I guess most inverters provide energy as well…

You will (currently) also need a last_reset attribute for the energy panel to accept it. This will change with next release (3 weeks)

Yes, I have used it, thanks!

Thanks, I was struggling that not even my MQTT consumption sensors were appearing in Energy panel, and this was the reason, I had to set last_reset like this:

  last_reset_topic: "tele/blitzwolf8/SENSOR"
  last_reset_value_template: "1970-01-01T00:00:00+00:00"

My main meter is MODBUS based, which still cannot be used, probably for the same reason: I can set up state_class in Customazation, but not last_reset, so for that only the template sensor or a yearly utility_meter is the solution now.

Did you try to add a Other attribute on the bottom with name last_value and value 1970-01-01T00:00:00+00:00 like so and still nothing?
image

1 Like

Thanks, it’ll probaly work! (just it wasn’t offered by customization as state_class was)

1 Like

could you share your modbus mapping?

sorry for late answer,
here are my modbus mapping

- name: "AlphaHub"
  type: rtuovertcp
  host: 192.168.1.50
  port: 502
  delay: 10
  timeout: 10
  sensors:
# Netz
      - name: Alpha Netz Einspeisung
        unit_of_measurement: kWh
        device_class: energy
        state_class: total_increasing
        slave: 85
        address: 16
        data_type: uint32
        scale: 0.01
        precision: 1

      - name: Alpha Netz Bezug
        unit_of_measurement: kWh
        device_class: energy
        state_class: total_increasing
        slave: 85
        address: 18
        data_type: uint32
        scale: 0.01
        precision: 1

      - name: Alpha Netz Leistung L1
        unit_of_measurement: W
        device_class: power
        state_class: measurement
        slave: 85
        address: 27
        data_type: int32
        scale: 1

      - name: Alpha Netz Leistung L2
        unit_of_measurement: W
        device_class: power
        state_class: measurement
        slave: 85
        address: 29
        data_type: int32
        scale: 1

      - name: Alpha Netz Leistung L3
        unit_of_measurement: W
        device_class: power
        state_class: measurement
        slave: 85
        address: 31
        data_type: int32
        scale: 1

      - name: Alpha Netz Leistung
        unit_of_measurement: W
        device_class: power
        state_class: measurement
        slave: 85
        address: 33
        data_type: int32
        scale: 1

      - name: Alpha Netz Spannung L1
        unit_of_measurement: V
        device_class: voltage
        state_class: measurement
        slave: 85
        address: 20
        scale: 1

      - name: Alpha Netz Spannung L2
        unit_of_measurement: V
        device_class: voltage
        state_class: measurement
        slave: 85
        address: 21
        scale: 1

      - name: Alpha Netz Spannung L3
        unit_of_measurement: V
        device_class: voltage
        state_class: measurement
        slave: 85
        address: 22
        scale: 1

      - name: Alpha Netz Frequenz
        unit_of_measurement: Hz
        device_class: frequency
        state_class: measurement
        slave: 85
        address: 26
        scale: 0.1

# Batterie
      - name: Alpha Batterie Spannung
        unit_of_measurement: V
        device_class: voltage
        state_class: measurement
        slave: 85
        address: 256
        scale: 0.1

      - name: Alpha Batterie Strom
        unit_of_measurement: A
        device_class: current
        state_class: measurement
        slave: 85
        address: 257
        scale: 0.1

      - name: Alpha Batterie Leistung
        unit_of_measurement: W
        device_class: power
        state_class: measurement
        slave: 85
        address: 294
        scale: 1

      - name: Alpha Batterie
        unit_of_measurement: '%'
        device_class: battery
        slave: 85
        address: 258
        scale: 0.1

      - name: Alpha Batterie Ladeenergie
        unit_of_measurement: kWh
        device_class: energy
        state_class: total_increasing
        scale: 0.1
        slave: 85
        address: 288
        data_type: uint32
        precision: 1

      - name: Alpha Batterie Entladeenergie
        unit_of_measurement: kWh
        device_class: energy
        state_class: total_increasing
        scale: 0.1
        slave: 85
        address: 290
        data_type: uint32
        precision: 1

# Wechselrichter
      - name: Alpha WR Leistung 
        unit_of_measurement: W
        device_class: power
        state_class: measurement
        slave: 85
        address: 1036
        data_type: int32
        scale: 1

      - name: Alpha WR Leistung L1
        unit_of_measurement: W
        device_class: power
        state_class: measurement
        slave: 85
        address: 1030
        data_type: int32
        scale: 1

      - name: Alpha WR Leistung L2
        unit_of_measurement: W
        device_class: power
        state_class: measurement
        slave: 85
        address: 1032
        data_type: int32
        scale: 1

      - name: Alpha WR Leistung L3
        unit_of_measurement: W
        device_class: power
        state_class: measurement
        slave: 85
        address: 1034
        data_type: int32
        scale: 1

      - name: Alpha WR Temperatur
        unit_of_measurement: '°C'
        device_class: temperature
        state_class: measurement
        slave: 85
        address: 1077
        scale: 0.1

# Photovoltaik
      - name: Alpha PV Ertrag
        unit_of_measurement: kWh
        device_class: energy
        state_class: total_increasing
        slave: 85
        address: 1086
        data_type: uint32
        scale: 0.1
        precision: 1

# PV 1 = Ost
      - name: Alpha PV Ost Leistung
        unit_of_measurement: W
        device_class: power
        state_class: measurement
        slave: 85
        address: 1055
        scale: 1
        data_type: int32

# PV 2 = West
      - name: Alpha PV West Leistung
        unit_of_measurement: W
        device_class: power
        state_class: measurement
        slave: 85
        address: 1059
        data_type: int32
        scale: 1

# Backup
      - name: Alpha Backup Leistung L1
        unit_of_measurement: W
        device_class: power
        state_class: measurement
        slave: 85
        address: 1044
        data_type: int32
        scale: 1

      - name: Alpha Backup Leistung L2
        unit_of_measurement: W
        device_class: power
        state_class: measurement
        slave: 85
        address: 1046
        data_type: int32
        scale: 1

      - name: Alpha Backup Leistung L3
        unit_of_measurement: W
        device_class: power
        state_class: measurement
        slave: 85
        address: 1048
        data_type: int32
        scale: 1

      - name: Alpha Backup Leistung
        unit_of_measurement: W
        device_class: power
        state_class: measurement
        slave: 85
        address: 1050
        data_type: int32
        scale: 1