Two point calibration

Hello :slight_smile:
We are trying to program a two point calibration for our flow sensor to control a water pump. On our Home Assistant there is an ESP8266. The pump can already be controlled via our Home Assistant and the volume flow can be regulated by us via it. So we have come that far.

The main part of the task is to install a two-point calibration. This should always be done in the morning with one switch. This switch causes the pump to run for 60 seconds and measure the average value of the flow sensor, which is then stored. For the calibration we also need manually measured values which should be entered in the code (Raw_30, Raw_70).
Our problem is that we have to save the ranges for the two point calibration in global variables to adjust the flow sensor afterwards, this is not working.

Here are the code sections:

# Global variables 
globals:
  - id: RawRange
    type: float
    restore_value: true
    initial_value: "0"
  - id: ReferenceRange
    type: float
    restore_value: true
    initial_value: "0"

switch:
  - platform: gpio
    pin: GPIO14
    id: calibration
    name: "Calibration"
    on_turn_on:
      # Start the calibration process
      - lambda: |-
          static float sum30 = 0.0;
          static float sum70 = 0.0;
          static int num_measurements = 0;
          static float Raw_30 = 0.0;
          static float Raw_70 = 0.0;

          while (num_measurements < 60) {
            sum30 += id(flow_sensor).state;
            sum70 += id(flow_sensor).state;
            num_measurements++;
            delay(1000); // Delay 1 second between measurements
          }

          float averaged_flow_30 = sum30 / 60;
          float averaged_flow_70 = sum70 / 60;
          
//input numbers 
          Raw_30 = 29;
          Raw_70 = 70;

          RawRange = Raw_70 - Raw_30;
          ReferenceRange = averaged_flow_70 - averaged_flow_30;

      # Delay for 60 seconds to allow pump to run
      - delay: 60s

      # Deactivate the calibration switch
      - switch.turn_off: calibration_switch

- platform: gpio
    pin: GPIO2
    id: kalibrierung_korrektur_switch
    name: "Calibriation"
    on_turn_on:
   
      #CorrectedValue = (((RawValue – RawLow) * ReferenceRange) / RawRange) + ReferenceLow
      - lambda: |-
            float corrected_flow = ((id(flow_sensor).state - Raw_30) * ReferenceRange) / Raw Range) + ReferenceLow;
            id(flow_sensor).publish_state(corrected_flow);
          

      # Delay for 1 second (optional)
      - delay: 1s
'''

These are the error messages: 

/config/esphome/esppwm1.yaml: In lambda function:
/config/esphome/esppwm1.yaml:92:7: error: 'Raw_30' was not declared in this scope
   92 |           Raw_30 = 29;
      |       ^   ~~
/config/esphome/esppwm1.yaml:93:7: error: 'Raw_70' was not declared in this scope
   93 |           Raw_70 = 70;
      |       ^   ~~
/config/esphome/esppwm1.yaml:96:41: error: cannot convert 'float' to 'esphome::globals::RestoringGlobalsComponent<float>*' in assignment
   96 |           ReferenceRange = averaged_flow_70 - averaged_flow_30;
      |                        ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
      |                                         |
      |                                         float
/config/esphome/esppwm1.yaml: In lambda function:
/config/esphome/esppwm1.yaml:112:53: error: 'Raw_30' was not declared in this scope
  112 |             float corrected_flow = ((id(flow_sensor).state - Raw_30) * ReferenceRange) / Raw Range) + ReferenceLow;
      |                                                     ^~~~~~
/config/esphome/esppwm1.yaml:112:81: error: 'Raw' was not declared in this scope
  112 |             float corrected_flow = ((id(flow_sensor).state - Raw_30) * ReferenceRange) / Raw Range) + ReferenceLow;

Can you please help us? 
Kind regards