Statistics error on one template sensor

I have the following sensors:

  - name: Iammeter_1_modbus
    type: tcp
    host: 192.168.20.12
    port: 502
    sensors:
      - name: Iammeter_1_modbus_ImportEnergy
        unique_id: 2452716747601
        slave: 1
        address: 4
        input_type: holding
        unit_of_measurement: kWh
        device_class: energy
        state_class: total_increasing
        count: 2
        scale: 0.0003125
        offset: 0
        precision: 3
        data_type: uint32
      - name: Iammeter_1_modbus_ExportGrid
        unique_id: 2452716747603
        slave: 1
        address: 6
        input_type: holding
        unit_of_measurement: kWh
        device_class: energy
        state_class: total_increasing
        count: 2
        scale: 0.0003125
        offset: 0
        precision: 3
        data_type: uint32

These are alternate directions on an electricity CT clamp, one recording inbound, the other outbound energy.

Statistics is complaining about one of them - ImportEnergy:
image

It is total_increasing, and it is defined identically to the ExportGrid one which is fine. The value from the template is there, and is increasing in display (14,825 kWh in the image), so it is “reading” ok - but not retaining statistics.

The Iammeter modbus definition is as recommended.

If I “permanently remove the long term statistics of sensor.iammeter_1_modbus_importenergy from database” will that resolve it?

I have a templated (filtered) entity based on this and it’s fine:

      - name: "Grid consumption energy"
        unit_of_measurement: 'kWh'
        device_class: energy
        state_class: total_increasing
        availability: >-
          {{ states('sensor.iammeter_1_modbus_importenergy') not in ['unavailable', 'unknown', 'none', 0] }}
        state: >-
          {{ states('sensor.iammeter_1_modbus_importenergy') | float }}

image

When you look at the import sensor in Developer Tools → States, what does it list the state_class as?

Interesting

ScreenShot_20230708135935

state_class: total_increasing

Where the heck did that come from?

Is there perhaps a hidden character at the end of your state_class yaml for that sensor?

Found it.

There was a yaml customisation which had the strange additional text.

Nothing to see here… lol. Thank you Tom.