Sensors for Energy Cost Calucation with changing Energy Cost rates

Hi,

i try to create sensors that calculate my hourly, daily, weekly, and monthly energy cost based on other sensors that hold the current price per kWh. Unfortunately, my sensors doesn’t go up entirely. At the moment where the price for kWh goes up or down the history of my sensor changes for some past values.

Sensor that should hold the € spend for kWh

Here is the configuration for the template sensors i’m using:

- sensors:
    - name: "Days in Month"
      state: >
        {% if now().month in [1,3,5,7,8,10,12] %}
          31
        {% elif now().month in [4,6,9,11] %}
          30
        {% elif now().month == 2 and ((now().year-2000) % 4 > 0) %}
          28
        {% elif now().month == 2 and ((now().year-2000) % 4 == 0) %}
          29
        {% endif %}
      unit_of_measurement: "Days"
      unique_id: 89d683ff-62cf-429e-922c-fe89d76b1a41

    - name: "EVN Grundpreis pro Monat"
      unit_of_measurement: "EUR"
      state: "{{ (37.2/365) * (states('sensor.days_in_month') | round(2) )}}"
      state_class: measurement
      unique_id: 475cb4ae-9e29-4287-91f1-b9306d643d81
      icon: mdi:currency-eur

    - name: "EVN Grundpreis Netz pro Monat"
      unit_of_measurement: "EUR"
      state: "{{ (36/365) * (states('sensor.days_in_month') | round(2) )}}"
      state_class: measurement
      unique_id: 52904d41-6a8f-4537-b9ae-a62ebbb9e381
      icon: mdi:currency-eur

    - name: "EVN Messgerät pro Monat"
      unit_of_measurement: "EUR"
      state: "{{ (26.16/365) * (states('sensor.days_in_month') | round(2) )}}"
      state_class: measurement
      unique_id: 85cb3e2f-3b00-453a-8b6d-53f77e6d0cff
      icon: mdi:currency-eur

    - name: "aWATTar Summe kWh Preis"
      unit_of_measurement: "EUR/kWh"
      state: >
        {% set preiskwh = (states('sensor.epex_spot_data_net_price') | float(0) ) + ((states('input_text.evn_verbrauchspreis_netznutzung_je_kwh') | float(0)) * 1.2) + ((states('input_text.evn_verbrauchspreis_netzverlust') | float(0)) * 1.2) %}
        {{ (preiskwh / 100) | float(0) }}
      state_class: measurement
      unique_id: 4209c8f4-5ef3-4182-842a-5b8ebd78f115
      icon: mdi:currency-eur

    - name: "aWATTar Kosten Monthly HS"
      unit_of_measurement: "EUR"
      state: >
        {% set grundpreisnetz = states('sensor.evn_grundpreis_netz_pro_monat') | float(0) %}
        {% set grundpreis     = states('sensor.evn_grundpreis_pro_monat') | float(0) %}
        {% set messgeraet     = states('sensor.evn_messgerat_pro_monat') | float(0) %}
        {% set preiskwh       = states('sensor.awattar_summe_kwh_preis') | float(0) %}

        {{ ((states('sensor.evn_bezug_monthly_kwh') | float(0) ) * preiskwh) + ((grundpreisnetz * 1.2) + (grundpreis * 1.2 ) + (messgeraet * 1.2)) }}
      state_class: total
      unique_id: 6d3c0b43-83ac-4689-9c09-9755c3d9ee67
      icon: mdi:currency-eur
      last_reset: "{{ state_attr('sensor.evn_bezug_monthly_kwh', 'last_reset') }}"

I’ve tried already using as state_class total and total_increasing. If also have tried to set last_reset based on the utility meter that holds the energy usage, so the two sensors are corretly aligned.

I would like the costs only to go up until the utility meter resets. It seems that the change in € / kWh has an impact on the historical data that was already collected. The price per / kWh is changed most of the time every hour.

Anyone an idea why this behaves that way or how to implement my scenario correctly?

Regards
Stefan