How to pull daily slices of cumulative data with ApexCharts

I’ve got a series of sensors on my PV inverter (MQTT inputs, basically) that output a cumulative value for energy in kWh (i.e. PV energy generated, power from the grid, battery power out, etc.). HA sees these entities and is able to plot historical data when I view the entity:

What I’d like to do is use ApexCharts to build a 14 day graph of stacked bar charts that show where my power came from for the day (PV, battery, grid). What I can’t figure out how to do is to have each day be represented by the data for that sensor only for that specific day. Essentially for any given day, the graphed value should be - . As it stands, each stacked bar in my graph just represents the cumulative value for that sensor at the end of day, so it just keeps getting bigger and bigger. I’ve looked into the data_generator option, which seems like what I need to do here, but I don’t know how to figure out where those values in the historical graph are coming from. Is this exposed anywhere in the UI?

For purposes of discussion, here’s a screenshot and my YAML (so far) for the graph:

  - type: custom:apexcharts-card
    apex_config:
      chart:
        stacked: true
    graph_span: 14d
    span:
      end: day
    show:
      last_updated: true
    header:
      show: true
      show_states: true
      colorize_states: true
      title: Daily load servicing
    series:
      - entity: sensor.deye_sunsynk_sol_ark_pv_energy_2
        name: PV
        type: column
        unit: ' kWh'
        group_by:
          func: max
          duration: 1d
      - entity: sensor.deye_sunsynk_sol_ark_battery_energy_out_2
        name: Battery
        type: column
        unit: ' kWh'
        group_by:
          func: max
          duration: 1d
      - entity: sensor.deye_sunsynk_sol_ark_grid_energy_in_2
        name: Grid
        type: column
        unit: ' kWh'
        group_by:
          func: max
          duration: 1d

Assuming your sensor has already data for more than one day:
Here is an example from me and it works.
I use daily sensors that reset each day. Maybe that is the issue, or using max I stead of last as function?

And here my yaml of top graph

type: custom:apexcharts-card
apex_config:
  legend:
    show: false
  chart:
    stacked: true
  dataLabels:
    background:
      borderWidth: 0
      opacity: 0
      foreColor: '#03A9F4'
  plotOptions:
    bar:
      borderRadius: 5
      dataLabels:
        position: center
graph_span: 7d
span:
  end: day
show:
  last_updated: true
header:
  show: true
  show_states: true
  colorize_states: true
  title: Stromherkunft
series:
  - entity: sensor.home_selfuse_energy_daily
    color: orangered
    show:
      datalabels: true
      header_color_threshold: false
      extremas: false
    type: column
    name: von PV
    group_by:
      func: last
      duration: 1d
  - entity: sensor.grid_consumption_energy_daily
    color: pink
    show:
      extremas: false
      header_color_threshold: true
      datalabels: true
    type: column
    name: aus dem Netz
    group_by:
      func: last
      duration: 1d
  - entity: sensor.home_consumption_energy_daily
    color: '#03A9F4'
    show:
      header_color_threshold: true
      datalabels: true
    type: area
    name: Stromverbrauch
    group_by:
      func: last
      duration: 1d
    opacity: 0
    stroke_width: 0

I like your thinking, but switching the function type doesn’t seem to give me a time slice.

As I understand it your sensors reset each day, whereas mine are just a cumulative value from the beginning of time. I’d need to write a function that is able to take a 24-hour slice of that stream and subtract <day_start_value> from <day_end_value> to get the total for the day. I’m assuming it’s possible given that there is a historical chart in HA for the entity values, just not sure how to go about accessing those and/or wrapping that up into a chart.

You should probably just use a different sensor that outputs your “realtime” kWh usage, not the cumulative lifetime one, and use “add” instead of “last” to get a total for each 24h period. I don’t think the cumulative sensor is the way to go here as it’s more complex to break out each day.

Sadly, that’s the only sensor I have. HOWEVER, I just read about the Utility Meter template which will give me the time slices into the entities of type total_increasing I’m looking for. So far it seems to be working.