Last_reset not working in Energy statistics

Hi, there, I really like the Idea of the Energy view and history and directly added the energy counters via mqtt, but it seems like the time of last reset is somehow wrong and I don’t know what I’m doing wrong.
Somehow it is counted as negative at the time of the reset: (the exact value, it generated the day before)

Which is correct regarding the actual energy value because it is reset at midnight, but I thought this is compensated by the last_reset attribute?
The last_reset value in the attributes looks correct to me:

state_class: measurement
last_reset: 2021-08-20T00:00:00
unit_of_measurement: Wh
friendly_name: e3dc_hist_sol_total
device_class: energy

and the sensor-entry in the config looks like this: (for all hist_* entries in the json below)

  - platform: mqtt
    name: e3dc_hist_sol_total
    state_topic: e3dc/power
    unit_of_measurement: "Wh"
    device_class: energy
    state_class: measurement
    value_template: "{{ value_json.hist_sol_total | int }}"
    last_reset_topic: e3dc/power
    last_reset_value_template: "{{ value_json.last_reset | int | timestamp_local | as_datetime }}"

the value, coming over mqtt looks like this:

    "battery": -879,
    "house": 863,
    "solar": 0,
    "solar2": 0,
    "grid": -16,
    "stateOfCharge": 89,
    "hist_bat_in": 14405.25,
    "hist_bat_out": 11060,
    "hist_sol_1": 57337,
    "hist_sol_2": 31594,
    "hist_sol_total": 88931,
    "hist_grid_in": 40120,
    "hist_grid_out": 227.5,
    "hist_house": 42380,
    "last_reset": 1629410400

And the script which creates the data for mqtt switches the last_reset with the same message, when the hist_* values change to 0.
Did I understand something wrong about the last_reset attribute? Or is it shown with a different timezone in the developers entity list? So maybe I need to move it to a different timezone-offset?

Thanks for all ideas in advance :slight_smile:

Hm, I found this post:

And it says that the last_reset string needs to have the “+00:00” at the end, so, I’ll try it… I hope that the timezone taken into account because I’m actually UTC+2, so I changed the last_reset_value_template to:

last_reset_value_template: "{{ (as_datetime((value_json.last_reset | int | timestamp_utc)|string+'Z')).isoformat() }}"

and with that change, the last_reset time looks now like:

last_reset: 2021-08-20T22:00:00+00:00

if this doesn’t work, then maybe I need to have 2021-08-21T00:00:00+02:00 instead, but that will be tricky to get that depending on the current daylight-saving-state.

I’ll report tomorrow if it worked.

Nope. last_reset has to be UTC, so +2 won’t work correctly.

1 Like

Unfortunately it did not work :frowning:
Any other ideas?

I would suggest, if possible, to use a lifetime sensor, so that it never resets.

I fixed this by using sensor.yearly_usage.