Pulse counter reset after power shutdown

My pulse counter is always reset to 0 during an (unplanned) power outage. When I update the ESP, setting the value davov works.

Is there a solution for this?

esphome:
  on_boot:
    then:
      - pulse_counter.set_total_pulses:
          id: power
          value: !lambda 'return id(global_total);'
  on_shutdown:
    then:
      - globals.set:
          id: global_total
          value: !lambda 'return id(energy).state * 1000;'

  services:
    - service: set_pulse_total
      variables:
        new_pulse_total: int
      then:
        - pulse_counter.set_total_pulses:
            id: power
            value: !lambda 'return new_pulse_total;'


sensor:
  - platform: pulse_counter
    internal_filter: 13us
    pin: D2 #D2
    name: 'Power'
    id: power
    unit_of_measurement: 'kW'
    state_class: measurement
    device_class: power
    filters:
      - multiply: 0.06 # (60 minutes per hour / 100 pulses per kWh)
    total:
      name: 'Energy'
      id: energy
      unit_of_measurement: 'kWh'
      state_class: total_increasing
      device_class: energy
      accuracy_decimals: 3
      filters:
        - multiply: 0.001

interval:
  - interval: 60s
    then:
      - globals.set:
          id: global_total
          value: !lambda 'return id(energy).state * 1000;'

globals:
  - id: global_total
    type: int
    restore_value: yes