ESPHome, change the stored value in flash memory for the energy sensor

Hi,
Please help me compile a lambda function in which the value for the total energy sensor will be changed from the entered number template and after pressing the button template.
Is there any way to change the value of the total energy sensor?

button:
  - platform: template
    name: "PV_Energy_total_Set"
    entity_category: config
    on_press:
      - lambda: |-
          id(pv_energy_total).state set id(value_to_write);

number:
  - platform: template
    name: "Value_to_write"
    id: value_to_write
    optimistic: true
    mode: box
    entity_category: config
    restore_value: false
    icon: mdi:numeric
    min_value: 0
    max_value: 5000
    step: 1

sensor:
  - platform: integration
    name: 'PV Energy_total'
    id: pv_energy_total
    sensor: pv_power
    time_unit: h
    restore: true
    unit_of_measurement: 'kWh'
    state_class: total_increasing
    device_class: energy
    accuracy_decimals: 3

Try this on number component:

    set_action:
      then:
        - lambda: |-
        id(pv_energy_total).publish_state(x);

Don’t need button.

This lambda works, but after changing the value it immediately returns the value calculated by the sensor. It does not remember the change.

If that sensor is updating automatically, it’s obvious it’s getting overwritten.
What you are trying to accomplish?

I want to enter the total production for this sensor which until now I had counted by the assistant in HA. I want to make HA not count these values ​​and esp itself writes to memory.

I still don’t follow. You want a sensor that outputs a value that you just manually input? What’s the purpose of this kind of sensor?

No, I just want to enter the value that another sensor has been counting so far, and then let this sensor continue counting energy from this entered value.

Ok, I finally understood what you are looking for.
So you basically want to reset the total counter, not to zero, but to your input number.
Unfortunately, I don’t know how to do it. For pulse counter there is syntax for it, so I expect there is for integration sensor as well. I did quick search, didn’t find anything.