ESPHOME Help change lambda output to number

I have this code in esphome yaml that returns the desire value in string rather than number. Kindly help change the code to output number that I can assign unit of measurement to. Thanks in advance.

          - text_sensor.template.publish:
              id: pv1_voltage
              state: !lambda |-
                  int32_t nIndex = 0, nCur = 0;
                  std::string::size_type curPos = 0, charPos = 0;
                  while((charPos = x.find(' ', curPos)) != std::string::npos && nIndex > nCur++) curPos = ++charPos;
                  return x.substr(curPos, charPos-curPos);

ESPHome has a parse_number helper you can use.

<float> y = parse_number<float>(x);

Text sensors don’t accept floating point numbers