Utility meter and total cost counter, how to deal with tariff's unit rate changes?

I have a utility meter entity that tracks the kWh comsumed in the peak and off peaks periods.
I then have a template entity that calculate the yearly cost

sensor:
  - platform: template
    sensors: 
      yearly_cost:
       value_template: "{{ (((0.3935 * states('sensor.yearly_import_energy_day')|float))+(0.075 * states('sensor.yearly_import_energy_night')|float))|round(2)}}"
       unit_of_measurement: '£'

If I manually change the value (0.3935), the entire “yearly cost” metric will be screwed up, how can I deal with this?

Manually calculate the total until the date the tariff changed. Put that as a constant in your calculation.

Is there a way to avoid having to do that every time the tariff changes?

Store the tariff in a helper. Take your daily consumption (take the previous period attribute from a utility meter with a daily cycle). Do this by triggering an automation on a specific time and take this previous value together with the tariff helper to calculate the last day’s total. Make a second helper, an input number, to store the running total. Add your new (calculated) value to the helper’s value. Wrap this helper in a template sensor and give it a state class for long-term tracking.

Now, if you change the tariff today, with tomorrow’s run it will calculate the total for today using the new tariff.

How can I apply the result of an operation (unit rate * daily consumption) to an input number?
The only services I see are ‘increment’ and ‘set’ (which needs a defined value)

Set can take a template, so you can reference the current value, for the equivalent of i = i + 1.

In fact, you don’t even need that helper I just realised now. You only need the sensor I referred to as the wrapper sensor. In it’s value template you can reference the current value too.

Ok, but this way my sensor will only update every 24 hours, right? Right now it’s continuously updating (almost every second) but I can’t change the tariff without affecting its value

P.S. I don’t think input number set can take a template

Read my comment again. Make a utility meter using your current sensor. That will give you a daily value for the previous day.

Do you really want to see the cost at a level more granular than daily? If so, make a utility meter with an hourly cycle.

Why do you say that? I don’t know about the UI, but in YAML you can.