Discrepancy between energy statistics

It seems my statistics, which should be dependant on one-another are drifting apart and I can’t figure out why.

I’m trying to keep track of how much energy is used for the day, the month and last month, this is my approach:

I have a Shelly 3em pro that reports total active energy. This unit was installed on Aug 11

sensor.ec6_total_active_energy
state_class: total_increasing
device_class: energy
unit_of_measurement: kWh

Checking this entitys history yesterday, it reported that the first value was recorded on Aug 14 at 04:12 with a state of 54,88kWh
Checking today (Aug 25) the first reported value is on Aug 15 at 04:12 with a value of 93,81kWh.

EDIT: Checking the state today (Aug 28th) the first reported values is on Aug 18 at 04:12 with a value of 123,61 kWh

I use Node red to create a new sensor.per_day with the following attributes:

state_class: measurement
device_class: energy
unit_of_measurement: kWh

The sensor is calculated using a flow that sets a node red variable at 00:01 o’clock each day with the current state of sensor.ec6_total_active_energy I then use a function node to calculate the difference and feed that to the above sensor

// Get the Home Assistant global object
const homeAssistant = global.get('homeassistant');

// Check if the Home Assistant global object is defined
if (homeAssistant) {
    // Get the current state of the sensor
    const currentUsedState = homeAssistant.homeAssistant.states['sensor.ec6_total_active_energy'].state;

    // Get the state of the sensor at the start of the day from the global variable
    const startOfDayUsedState = global.get('startOfDayUsedec6');

    // Calculate the difference between the current state and the state at the start of the day
    const diff = currentUsedState - startOfDayUsedState;

    // Round the diff value to 2 decimal places
    const roundeddiff = Math.round(diff * 100) / 100;

    // Set the payload to the rounded avoided CO2 value
    msg.payload = roundeddiff;
} else {
    // The Home Assistant global object is not defined
    node.error('Home Assistant global object not found');
}

return msg;

I then use about the same approach in node red to calculate the energy usage for the month but ofcourse adjusted to get the state of sensor.ec6_total_active_energy on the first day of the month at 00:01

EDIT: This sensor is also being downsampled (if that’s what’s happening) and historical states are being purged after 10 days it seems.

Lastly I use apexcharts-card to display the total for the past 4 months using this config

type: custom:apexcharts-card
all_series_config:
  stroke_width: 1
  show:
    offset_in_name: false
    legend_value: false
    extremas: false
    name_in_header: false
graph_span: 4month
span:
  end: month
apex_config:
  legend:
    show: false
  dataLabels:
    style:
      fontSize: 9px
    background:
      enabled: true
      padding: 2
      borderRadius: 1
yaxis:
  - id: hours
    decimals: 0
    apex_config:
      tickAmount: 4
      title:
        rotate: -90
        text: kWh
        style:
          fontWeight: 400
          fontSize: 11
    show: true
    opposite: false
header:
  show: true
  title: Energy used (4 months)
  show_states: false
  colorize_states: true
series:
  - entity: sensor.per_day
    name: Total for the month
    yaxis_id: kwh
    color: orange
    type: column
    opacity: 1
    statistics:
      type: max
      period: day
    group_by:
      func: sum
      duration: 1month
  - entity: sensor.per_day
    name: kWh line
    stroke_width: 2
    yaxis_id: kwh
    color: green
    type: line
    opacity: 1
    show:
      datalabels: true
    statistics:
      type: max
      period: day
    group_by:
      func: sum
      duration: 1month

As you can see, the apexchart is showing a bigger total for the month than the main sensor feeding all stats (sensor.ec6_total_active_energy)

“Energy used (Total)” is: sensor.ec6_total_active_energy
“Today (counting)” is sensor.per_day
“Month” is: sensor.per_month

image

Apexchart:
image

sensor.per_day example for Aug 22 - Aug 25

What could be causing this?