Alternate Weekly/Monthly/Yearly sensor. Is it possible to add backdated sensor value?

I have several water and electricity meter devices and would like to collect daily/montly/yearly consumption stats for each of the sensor.

I tried utility_meter, but it has a big issue: on every incoming value (from the hardware) it produces 3 more values in the database (daily/monthly/yearly accumulators). As soon as I have pretty big number of meter devices, my database size will grow rapidly. It would be nice to store accumulated values only once per period somehow.

I am thinking about patching utility_meter or developing a brand new sensor component, but not really sure if it is possible in HA architecture.

Question 1: Is it possible to update a sensor value in the database, but not create another record for this? Just update a most recent one

I am also thinking about another aproach. Unlike utility_meter which operates with just one value (state) I will probably need to store several values. Taking into account that meter values are just monotonically increasing numbers, the algorithm will look like this:

  • On the first meter value that came since the beginning of the period - store it to a start_value variable (see the question below)
  • All other values came till the end of period are also stored to a variable (cur_value), but do not update main state of the sensor
  • on ‘end of period’ notification store cur_value-start_value difference to the sensor state, so that it is written to the database and could be shown on the graph.

Perhaps I could use previous state value, but not sure if this would work in the following scenario: some of the consumers may be switched off for several month and do not produce new values for several periods.

Question 2: I need to store current value. Is there a way to store it somehow so that it survives on HA restart, but at the same time does no clutter the database on every update?