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

1 Like

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.

2 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