Help with sonoff pow-r2

Hello everyone,

I have a sonoff pow r2 flash with the latest esphome firmware.
The problem I am facing is this. I am trying to store the today, yesterday and total energy values to the flash memory so that they get restored after power failure. This is working but when I update the firmware to a later version (wirelessly) yesterday value gets restored properly after the update but today and total values are erased. Why?

Any ideas?

Here is my yaml file.

substitutions:
  device_name: sonoff-pow-r2-01
  device_ip: 192.168.10.94

packages:
  wifi: !include common/wifi.yaml
  web_server: !include common/web_server.yaml
  ota: !include common/ota.yaml
  api: !include common/api.yaml
  text_sensor: !include common/text_sensor.yaml
  sensor: !include common/sensor.yaml

esphome:
  name: ${device_name}
  on_boot:
    priority: -100
    then:
      lambda: |-
        id(yesterday_energy).publish_state(id(energy_yesterday));  // Copy restored global variable energy_yesterday to yesterday_energy sensor

esp8266:
  board: esp01_1m
  # Restores writen sensor values from flash
  restore_from_flash: True

preferences:
  # Saves sensor values to flash every... There must also be a "restore_from_flash : True" in "esp8266:" for it to work 
  flash_write_interval: 60min

# Enable logging
logger:
  baud_rate: 0

#wifi:
  #use_address: x.x.x.x  # In case we need to change the ip address (this is the old address). When done comment out these lines

# Enable Home Assistant API
api:
  encryption:
    key: "--------------------------------------------------------"

globals:
  - id: energy_yesterday
    type: float
    restore_value: yes
    initial_value: '0'

time:
  - platform: homeassistant
    id: homeassistant_time
    on_time:
      # Just before every midnight
      - seconds: 59
        minutes: 59
        hours: 23
        then:
          # Copy last total_daily_energy sensor value of the day, to global variable energy_yesterday
          # Publish to yesterday_energy sensor the value of total_daily_energy sensor
          lambda: |-
            id(energy_yesterday) = id(daily_energy).state;
            id(yesterday_energy).publish_state(id(daily_energy).state);
       

captive_portal:
    
#######################################
# Device specific Config Begins Below #
#######################################

binary_sensor:
  - platform: gpio
    id: button_bsensor
    pin:
      number: GPIO0
      mode: INPUT_PULLUP
      inverted: True
    on_press:
      - switch.toggle: fakebutton

button:
  - platform: factory_reset
    name: ${device_name}_Restart with Factory Default Settings

switch:
  - platform: template
    name: ${device_name}_switch
    optimistic: true
    id: fakebutton
    lambda: 'return id(relay).state;'
    turn_on_action:
    - switch.turn_on: relay
    - light.turn_on: led
    turn_off_action:
    - switch.turn_off: relay
    - light.turn_off: led
  - platform: gpio
    id: relay
    pin: GPIO12
    #restore_mode: RESTORE_DEFAULT_OFF
    restore_mode: ALWAYS_ON

output:
  - platform: esp8266_pwm
    id: pow_blue_led
    pin:
      number: GPIO13
      inverted: True

light:
  - platform: monochromatic
    output: pow_blue_led
    id: led

uart:
  rx_pin: RX
  baud_rate: 4800

sensor:

# Power & Energy Sensors start here
  - platform: cse7766
    update_interval: 2s
    current:
      name: ${device_name}_current
      device_class: "current"
      state_class: measurement
      unit_of_measurement: A
      id: current
      filters:
        - or:            
          - throttle: 90s    
          - delta: 0.1   
    voltage:
      name: ${device_name}_voltage
      device_class: "voltage"
      state_class: measurement
      unit_of_measurement: V
      id: voltage
      filters:
        - calibrate_linear:
          # Map 0.0 (from sensor) to 0.0 (true value)
          - 0.0 -> 0.0
          - 237.7 -> 236.2
        - or:                
          - throttle: 90s     
          - delta: 2     
    power:
      name: ${device_name}_power
      device_class: "power"
      state_class: measurement
      unit_of_measurement: W
      #accuracy_decimals: 0
      id: power
      filters:
        #- calibrate_linear:
          # Map 0.0 (from sensor) to 0.0 (true value)
          #- 0.0 -> 0.0
          #- 9.7 -> 10.9
          #- 945.2 -> 938.9 
        - or:                 
          - throttle: 90s    
          - delta: 5    
    energy: 
      name: ${device_name}_energy
      internal: True          # Don't expose this sensor 
      device_class: "energy"
      state_class: total_increasing
      unit_of_measurement: kWh
      accuracy_decimals: 3
      filters:
        - multiply: 0.001

  - platform: total_daily_energy
    name: ${device_name}_total_daily_energy
    id: daily_energy
    device_class: "energy"
    #state_class: total_increasing
    power_id: power
    method: right
    unit_of_measurement: kWh
    accuracy_decimals: 3
    filters:
      - multiply: 0.001

  - platform: integration
    name: ${device_name}_total_energy
    id: total_energy
    sensor: power
    integration_method: right
    time_unit: h
    unit_of_measurement: kWh
    device_class: "energy"
    state_class: total_increasing
    accuracy_decimals: 3
    filters:
      - multiply: 0.001
    
  - platform: template
    name: ${device_name}_power_factor
    id: power_factor
    internal: True          # Internal components will not be exposed to Home Assistant
    lambda: return id(power).state / id(voltage).state / id(current).state;
    filters:
      - filter_out: nan
  
  - platform: template
    name: ${device_name}_yesterday_energy
    id: yesterday_energy
    device_class: "energy"
    unit_of_measurement: kWh
    accuracy_decimals: 3
    filters:
      - filter_out: nan