How to do time triggered copy of values from one attributes to another of template sensor

Every day before midnight, I want to store todays rainfail in a variable rain_yesterday, after having moved rain-yesterday’s valye to rain_2_days_ago. I use a (triggered) template sensor, with the two days represented as attributes. One of the attributes (rain_yesterday) gets the data, but the copy from yesterday to day_before yesterday does not work. I also tried to create two sensors instead, with time triggers 30 secs apart. When copying a value from another sensor (ie not a template sensor), it works. What am I missing?

template:
  - trigger:
      - platform: time_pattern
        # This will update every night
        hours: 23
        minutes: 59
    sensor:
      # Keep track how much rain has accumulated, set state to last update time
      name: historical rainfall
      unique_id: historical_rainfall
      state: "{{ as_timestamp(now())| timestamp_custom('%c') }}"
      icon: mdi:weather-rainy
      attributes:
        rain_2_days_ago: "{{ state_attr('sensor.historical_rainfall', 'rain_yesterday') |int |default(99)}}"
        rain_yesterday: "{{ states('sensor.netatmo_rain_gauge_rain_today') |int |default(199)}}" 

I’m not sure how you would do that with a sensor but it’s trivial to do it using a custom component called “variables+history” in HACS.

after you install that custom component you just create the variable:

variable:
  rain_history:
    value: 'Not set'
    restore: true
    attributes:
      friendly_name: 'Rainfall History'

then use a time triggered automation to update the variable:

automation:
  - alias: 'Update Rainfall History'
    initial_state: 'on'
    trigger:
      - platform: time_pattern
        hours: 23
        minutes: 59
    action:
      - service: variable.set_variable
        data:
          variable: rain_history
          attributes:
            history_1: "{{ states('variable.rain_history') }}"
            history_2: "{{ state_attr('variable.rain_history','history_1') }}"
            history_3: "{{ state_attr('variable.rain_history','history_2') }}"
            history_4: "{{ state_attr('variable.rain_history','history_3') }}"
            history_5: "{{ state_attr('variable.rain_history','history_4') }}"
            history_6: "{{ state_attr('variable.rain_history','history_5') }}"
            history_7: "{{ state_attr('variable.rain_history','history_6') }}"
            history_8: "{{ state_attr('variable.rain_history','history_7') }}"
            history_9: "{{ state_attr('variable.rain_history','history_8') }}"
            history_10: "{{ state_attr('variable.rain_history','history_9') }}"
      - service: variable.set_variable
        data:
          variable: rain_history
          value: >
            {{ states('sensor.netatmo_rain_gauge_rain_today') | int |default(199) }}

and if you want the date in the history then add this to the template:

{{ as_timestamp(now()) | timestamp_custom('%m/%d') }}

such as:

{{ as_timestamp(now()) | timestamp_custom('%m/%d') }} - {{ states('sensor.netatmo_rain_gauge_rain_today') | int |default(199) }}
1 Like

If you are interested, it’s possible to use the Utility Meter integration to monitor a sensor’s value and maintain a long-term record of daily/weekly/monthly/yearly data. I just gave someone else an example of how to use it to record heating hours.

@finity and @123 thanks both for your dsuggestions! I ended up making the template sensor work but abandoned it when I reaslised that there is no persistence across reboots or even template config reloading because the sensor values are re-initialised. So I went for the variables+history add-on. Here is the final code for reference of others. Thanks again.

#setup for HACS integration "variables+history"
variable:
  rainfall:
    value: "Initial value"
    restore: true
    attributes:
      friendly_name: "rainfall history past 10 days"
      icon: mdi:weather-rainy


alias: 'Rain: collect historical rainfall data'
description: using the variable+history HACS custom component
trigger:
  - platform: time_pattern
    hours: '23'
    minutes: '59'
condition: []
action:
  - service: variable.set_variable
    data:
      variable: rainfall
      attributes:
        today-1: '{{ states(''variable.rainfall'') }}'
        today-2: '{{ state_attr(''variable.rainfall'',''today-1'') }}'
        today-3: '{{ state_attr(''variable.rainfall'',''today-2'') }}'
        today-4: '{{ state_attr(''variable.rainfall'',''today-3'') }}'
        today-5: '{{ state_attr(''variable.rainfall'',''today-4'') }}'
        today-6: '{{ state_attr(''variable.rainfall'',''today-5'') }}'
        today-7: '{{ state_attr(''variable.rainfall'',''today-6'') }}'
        today-8: '{{ state_attr(''variable.rainfall'',''today-7'') }}'
        today_9: '{{ state_attr(''variable.rainfall'',''today-8'') }}'
        today-10: '{{ state_attr(''variable.rainfall'',''today-9'') }}'
  - service: variable.set_variable
    data:
      variable: rainfall
      value: |
        {{ states('sensor.netatmo_rain_gauge_rain_today') | float  }}
mode: single
1 Like

that’s the big reason I use it.

And it makes it easy to have custom attributes set by the same service.