What should an accumulating energy template sensor actually return?

I created this template sensor to get an estimate of my car charging energy.
Looking at past data, the charging basically instantly goes to 11kW so can just use that as a base.

Basically, every 10 minutes while charging, return 10 minutes worth of kWh.
When charging stops, get the minutes from last 10 minute block if >0, and return that as kWh.

Looking at a blog post about total_increasing when it was added, I got the impression HA would know how to accumulate the data itself, but the data looks like this:

It’s not accumulating, but rather goes up and then back down to 0.

- trigger:
    # charging ended
    # -> current minutes % 10 / 60 * 11
    - trigger: state
      id: "status"
      entity_id: sensor.garo_status
      from: "CHARGING"
    # charging ongoing
    - trigger: time_pattern
      id: "cron"
      minutes: "/10"
  sensor:
    - name: Garo estimated energy
      unique_id: garo_estimated_energy
      state: >
        {% if trigger.id == 'status' %}
          {% set current_minutes = now().timestamp() | timestamp_custom('%M') | int %}
          {{ (current_minutes % 10) / 60 * 11 | round(2, default=0) }}
        {% elif trigger.id == 'cron' and is_state('sensor.garo_status', 'CHARGING') %}
          {{ 10 / 60 * 11 | round(2, default=0) }}
        {% else %}
          {{ 0 }}
        {% endif %}
      unit_of_measurement: kWh
      device_class: energy
      state_class: total_increasing
      icon: mdi:lightning-bolt

So if I need to calculate accumulation myself, how do I get a previous value?

Also, if a template sensor runs at for example 00:00:00, will the accumulated energy get assigned to 00-01, or previous day 23-00? Is there some way to achieve the latter?

Forget the template sensor and use this:

Not quite sure how that would replace my template sensor?

I don’t have a source for how much energy ONLY the car charger is using, that’s why I’m trying to estimate it.

Do you have a power sensor for the car charging?

No.

I can’t isolate the energy or power of car charging, which is why I’m trying to do this as a stopgap until I can upgrade to a charger with built in metering.

I just could not find info on what should this type of sensor actually return.

Powercalc can create a virtual power sensor which you can then feed to the integral helper to get energy.

I get that you’re trying to get me to “do the right thing”, but my solution is quite enough for me until I get a new charger.

I just need to know what a total_increasing sensor should return to get a running total of its values.

I made the assumption based on New sensor state class: total_increasing | Home Assistant Developer Docs that HA would internally keep the total tally, but at least with my current template (or other attributes), it does not.

Instead of having one simple out of the box supported template sensor, i don’t want to install extra stuff to accomplish the same, even if it would be “more correct”.

Nope. The state class total_increasing is only used to tell the energy dashboard what to do if the value decreases (it is treated as a reset). It has nothing to do with making the sensor accumulate.

Ok makes sense.

So I just have to start returning the sum myself somehow, but remember seeing some examples of that.

Actually, one more related question

I checked the data the sensor as in the first post generated for energy dashboard, and it’s missing one “tick”.

Exported data from the graph, and it shows:

00:00:00 → 0 , this is when charging started
00:10:00 → 1.83… , first 10 minute tick of 11kW
00:20:21 → 0.0 , looks like this is the first if branch due to the decimal

So why is there no 00:20:00 event from the second trigger, that would’ve added another 1.83kWh?

Can template sensor triggers get merged?, due to not getting a cron trigger in the template, the sensor lost 1.83kWh of energy.

Edit:

Or does this mean the value stayed the “same” at 00:20:00, and changed to 0.0 at 00:20:21?

That means the energy dashboard doesn’t understand this as is, and at least during charging i should return a sum that includes previous values, unless there’s some other setting i can change that indicates the sensor just ticks values, that can be the same as previous

Did not find a solution to the original problem, if the sensor reports in kWh twice with the same value, it doesn’t get summed up as it seems HA thinks there was no change.

Ended up adding a template sensor that outputs 11000 W during charging and 0W otherwise and adding an integral helper on that to get the actual energy estimate.