New values added to the sensor with every restart of HA

Hi there,

I am using following in my confirguration.yaml to get accumulated rain values for the last three days:

sensor:
  - platform: statistics
    name: "Total Rain Last3d"
    entity_id: sensor.daily_rain_yesterday
    state_characteristic: sum
    sampling_size: 3

template:
  - trigger:
      - platform: time
        at: "23:59:59"
    sensor:
      - name: "Daily Rain Yesterday"
        unique_id: "daily_rain_yesterday"
        unit_of_measurement: "mm"
        device_class: precipitation
        state: >
          {{ states('sensor.gw1100a_v2_2_3_daily_rain_rate') | float(0) }}

Basically, a second for midnight daily rain rate is persisted in the “Daily Rain Yesterday” and this triggers the update of “Total Rain last 3d”.

This works perfectly as long as I do not restart the HA, which now and then I must. If I do I assume that HA restores the value of “Daily Rain Yesterday” which then causes that the same value is added to the “Total Rain last 3d” again and mess up the values.

How can I prevent this? Any idea?

thx,
zavjah

Hello @Adamkiewicz,

Thank you very much for such a quick response. With your adaptation do I still need the template sensor for Daily Rain yesterday, eg. for set the uniqueID, unit of meassurment and so on? Or should I replace my code 1:1 with yours?

thx,
zavjah

This service call doesn’t exist in Home Assistant:

sensor.set_entity

Did you use ChatGPT to produce your reply?

Hello @123,

thx you for your intervention. DO you have an idea how to circumvent this problem of mine?

thx,
zavjah

Hello @123 ,

I’m not sure if utility meter can really help. I’ve looked into Get sum for today, yesterday, last x hours from a sensor, too. It has the similar question where utility meter doesn’t help. As far I understand the utility meters track a sensor over a defined time resetting it to 0 once this time elapses. Than start tracking again.

My setup in the configuration.yaml works differently: the daily temperature is feeded into the statistics sensor that only can store three values. This means the the oldest is always kicked out at the sum of tree is the value of the sensor. This was i get the rolling sum of rain over the last tree days.

My only issue is that when I am forced to restart HA the value of the daily rain is restored and this triggers the adding of the value from previous day again. This is what I want to prevent. Basically, what i need is a manual trigger once a day and not automatic update every time daily rain sensor changes value.

Would the suggestion above work with the service input_number, something like here: Calculate cumulative total sensor from daily total sensor - #6 by matys ? Of course I would need to apply this to the statistics sensor and not the daily rain yesterday sensor as I want that the statics are only updated at midnight.

I hope this makes any sense and I am not talking rubbish here :grimacing:

Cheers,
Zavjah

The problem is resetting of values at the end of the cycle. Can that be deactivated? And a cycle of three days defined?

And… he’s gone.

Hello @123, no, after three days the oldest value is replaced with the value of the last day. It is rolling sun of last three days I am after.

Thx @123 , I’ll give it a try and report back.

Hello @123,

thank you very much for your invest and your ideas/help. I’ve looked into your suggestion and as far as I understand the concept here to the rain level resets every three day period and thus it is not rolling.

I’ve solved this by creating seven template sensors, one for each day I want to observe (rain_1_day_ago, rain_2_days_ago, …). Each sensor inherits the value of it’s predecessor short before it’s value is updated to the new one. Although everything else but elegant this solution persists the rain values over a period of 7 past days allowing me to use them in custom made calculations like min/max or sum. As all of them are time pattern triggered I do not have any problems with restarts any more.