Strange total power consumption readings (state_class issue ?)

I have a strange problem with my total reading.
After a restart of home assistant there is a one time that the value is increased with the total amount of the readed sensor, and not the different from the last state.

where do i have to look for the problem.

I’m curently running version 2022 8.0
The total sensor is from a grott server (mqtt sensor value) this is becaus i’m not happy with the update time of the original growatt app and intergration, it is very often offline and then there are no readings. so i wen’t for the local setup with the above grott configuration.

below one could see what happens with the values after a restart.

and the values from adjust statistics

And this is my total sensor

- platform: mqtt
  state_topic: energy/growatt
  value_template: "{{ value_json['values']['pvenergytotal'] | int / 10 }}"
  unique_id: growatt_generated_energy_total
  device_class: energy
  unit_of_measurement: "kWh"
  name: Growatt - Opgewekte energie (Totaal)
  icon: mdi:solar-power
  state_class: total_increasing

when changing the high value to 0 it all seems fine again.
where or how can i solve this problem

It really depends what is being received on the topic when you are getting those negative spikes. Zero? None? Unavailable? Some actual small number?

This may work:

value_template: >
  {% if value_json['values']['pvenergytotal'] | is_number and value_json['values']['pvenergytotal']|int(0) > 5000 %}
    {{ value_json['values']['pvenergytotal'] | int / 10 }}
  {% else %}
  {% endif %}

Replace “5000” with a number that is smaller than your current total and larger than the negative spike value.

Is there a way to find out what is recieved on the topic after a restart.
or what HA is doing ?

wan’t to understand what is going wrong.
tryed your solution but it won’t work, stil getting the same mis reading.

Could it be that HA thinks it has to restart from 0 instead off the last know value, if so how can i solve that.

a small update, seems that i have found out a solution to my problem.
I changed the very first value that is been reading and changed it to its initial total output value.
and it looks like on restart it now goes right.

Maybee someone could point out how to enter an initial value, now my monthly readings are way of scale for the month Juli and that is not what i like.

an next update, things are stil not going well. somewhere it is going wrong.
after a restart the values arent right.

when i look in the database, the sum value was way of scale.
it looks like after a restart it somehow doubles the previus value

it looks like it has somthing to do with this state_class attribute.

i think after a reset, the value is lower then the previeus value so it count’s as a new meter value, but i don’t know how to solve this, if it is solvabel.

I thought what may have been happening is that after a restart the mqtt sensor state is not restored. And so your template retuned a null value. But my updated version of the template should have fixed that (maybe).

There is one way to guarantee that the mqtt sensor is restored correctly after a restart. And that is to ensure that the growatt device sends mqtt messages with the retained flag set. That way as soon as home assistant starts it can obtain the value from the broker.

Is there any way you can enable this for your device?

I found the setting in the latest beta version, no i’m going to test things out.
i will send a follow up in the next few days.

Restated a couple off times but always the same problem, it looks to me it is a bug within HA

First check that the message is actually being retained in the broker. Use MQTT Explorer (free) to look at the message topic and look for this icon:

found the problem, within the broker there was an old retained message from an early test setup.
I don’t use retain in mqtt. deleted the old values and now evertything is working fine.

Thanks for the help.