Total_increasing sensor not working properly / issues on integration restart

So, my use case: my AC is updating it’s energy consumption in more or less hourly intervals. The new state represents the energy used since the last state update. So far, so simple.

My template sensor to convert this sensor to an ever increasing energy sensor:

- sensor:  
  - name: Klima Energy Test
    unit_of_measurement: kWh     
    device_class: energy
    state: "{{ states('sensor.test_heat_energy_consumption') }}"
    state_class: total_increasing

to my understanding of New sensor state class: total_increasing | Home Assistant Developer Docs the resulting sensor is expected to increase no matter what. Yet the sensor reads:

0.3|2022-09-21 19:56:35.261535
0.6|2022-09-21 19:56:35.262924
|2022-09-21 19:57:01.792876
0.3|2022-09-21 19:57:01.794291
0.6|2022-09-21 19:57:01.807656
|2022-09-21 20:04:10.462048
0.3|2022-09-21 20:04:10.463590

The empty values seem to result from an integration restart. I wonder where they come from.

Anyways: the main issue is that the sensor breakes his “only increasing” promise and resets to a lower value.

Any idea anyone what is happening here?

Thx

Edit: This all doesn’t make sense. Sensor Entity | Home Assistant Developer Docs gives a totally different explanation of the sensors - it seems from there that total_increasing is not correct here at all. It should be total/last_reset, but last_reset is no more in current HA versions.

It is noted that total_increasing is set to replace this combination, but above link also states that changes <10% will be ignored in total_increasing. Which is not functionally equivalent to the “older” method at all.

What a mess.

Edit 2: Reading further it seems from Home Assistant Statistics | Home Assistant that the state_class does not affect the state itself but the way the statistics module uses the saved states. Is this correct?

You don’t need to create a whole new sensor just to add a state class and device class.

Just use Customize.

Also if the integration used to create this energy sensor is not applying the correct state class open an issue to get it corrected.

Hi, thanks for the input. This is just a stripped down version of the sensor that sums several values in the full version. That‘s why it may look overly complicated. The problem is with the total_increasing property not the sensor type.

Well if you don’t give us the complete information it makes it difficult to help you.

You may need an availability template, hard to say without seeing the whole thing.

Well, I gave the minimal version that is able to reproduce the issue. That‘s what you do to ease troubleshooting. I do not ask for my „original“ problem to be solved but on input on the specific issue I presented in my post.

So I would kindly ask to look at what I showed and not on what I did not show.

Then I can’t help you. Good luck.

Just in case some poor soul in search of a solution finds their way into this mess. I got a working template:

The Problem:

I needed a sensor collecting several other sensor data that produce a new energy value roughly every hour that represents the energy used between the last update an the current (Daikin integration ie) like so:

0.2|2022-09-26 13:01:09.234542
0.3|2022-09-26 15:00:10.632920
0.2|2022-09-26 16:00:41.481914
0.3|2022-09-26 19:00:49.469248
0.3|2022-09-26 20:01:19.582871

The total value of this is 13 (kWh).

What didn’t work:

Using the total_increasing property on the accumulating sensor. This only counts positive differences in the data for the statistics, so the aggregated value between:

0.3|2022-09-26 19:00:49.469248
0.3|2022-09-26 20:01:19.582871

is 0. Which is obviously not correct in this case. BTW: my understanding was correct. The total_increasing is not more than a hint for the statistics module and not affecting the sensor itself.

Using a stance like

- name: Klima Energy Test
  ...
  state: "{{  states('sensor.klima_energy_test')  + states('sensor.test_heat_energy_consumption') }}"

also doesn’t work. HA complains a circular reference and that reflects in the collected data:

0.7|2022-09-26 19:00:49.484034
1.0|2022-09-26 19:00:49.485299
1.3|2022-09-26 20:01:19.584779
1.6|2022-09-26 20:01:19.586204

One can see that the value is increased twice per update cycle which is because the state updates whenever one of the referenced states updates. So the update to sensor.klima_energy_test triggers the update of sensor.klima_energy_test. This is the circular reference HA complains about.

Side note: I saw that very method in several posts while researching my problem. It does not produce correct results.

Solution:

- trigger:
  - platform: state
    entity_id:
      - 'sensor.test_heat_energy_consumption'
  sensor:
    name: Klima Energy Test
    unit_of_measurement: kWh
    device_class: energy
    state: "{{ (states('sensor.klima_energy_test') | float(0)) +(states('sensor.test_heat_energy_consumption') | float(0)) }}"
    state_class: total_increasing

The trigger template restricts updates of the state to updates of the underlying sensor without creating a circular reference. total_increasing assertion is still needed if one wants to use the sensor in modules like the energy integration.

Hope that helps someone.

1 Like

I think that i have the same issue. I have a small ESP reading data from my solar over RS485 and provide it as a REST server.

I’ve made a template sensor

  - platform: rest
    resource: http://[ipaddress]/TotalE
    name: "TLX Total Energy Production"
    method: GET
    value_template: "{{ value_json.value |round(3)}}"
    unit_of_measurement: kWh
    device_class: 'energy'
    state_class: total_increasing 

I’m not complete sure when/why i get a 0 reading stored - but looking at the sensors (I.e
http://ha:8123/history?entity_id=sensor.tlx_total_energy_production&start_date=2022-08-31T22%3A00%3A00.000Z&end_date=2022-09-30T21%3A00%3A00.000Z

Shows that it has happened two times this month. With the current reading around 65.000kwh that of course throws the Energy dashboard for solar production off with an increase of ~130.000kwh.

I understand that the total_increasing behaves like intended but i don’t understand why i get the sensor reading of 0. Might be bad network or restarts in home assistant. I could implement “memory” in the ESP so that it would never return anything if the value was 0, but not sure that is the problem. Think it the HA template sensor would then also just insert a zero. So might need some kind of filtering of zero values.

I also have sensor for daily production. That will reset each day. Would that be better for the energy dashboard ? If i then have a 0 value it would only make an error in that days production…