Price tracking energy usage dynamic contract

I may have something that fits you well.

I use a triggered template sensor

I have a sensor sensor.kamstrup_tpi which reports the kWh and it never resets. It just increases at a certain rate.

And I have a sensor sensor.electricity_price_median which is the current price of electricity.

This sensor then calculates the spend every time the meter reports a new value

- trigger:
    - platform: state
      entity_id: sensor.kamstrup_tpi
  sensor:
    - name: "Electricity Spend"
      unique_id: electricity_spend
      #device_class: monetary
      unit_of_measurement: "DKK"
      state: >
        {% set price = float(states('sensor.electricity_price_median'),0) %}
        {% set meter = float(states('sensor.kamstrup_tpi'),0) %}
        {% if meter > 0 and this.attributes.last is defined %}
           {% set delta = meter - float(this.attributes.last,0) %}
        {% else %}
           {% set delta = 0 %}
        {% endif %}
        {{ '%0.6f'|format( price * delta  ) }}
      attributes:
        last: >
          {% if this.attributes.last is defined %}
            {% set lastlast = float(this.attributes.last,0) %}
          {% else %}
            {% set lastlast = 0 %}
          {% endif %}
          {% set meter = float(states('sensor.kamstrup_tpi'),0) %}
          {% if meter > 0 %}
            {{ meter }}
          {% else %}
            {{ lastlast }}
          {% endif %}

I abuse the attribute part of the template sensor to hold the electricity meter reading from the previous reading. This way I can calculate the incremental consumption. And then I multiply by the price

This is part of the way. You want to accumulate by month.

You then create a Utility Meter Helper. You do that in the UI. Settings → Devices and Services → Helpers tab → Create Helper button. You define the period to be monthly and you use the triggered template sensor as input. And turn on Delta Values in the dialog

And then you have a monthly price.

Hope this helps