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)}}"
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.