Sending template sensor value as float

I have a MLX90393 sensor that provides X and Y magnetic field values, as so:

sensor:
  - platform: mlx90393
    address: 0x0c
    id: mlx
    x_axis:
      name: "MLX Field Strength X"
      id: mlx_x
      accuracy_decimals: 5
    y_axis:
      name: "MLX Field Strength Y"
      id: mlx_y
      accuracy_decimals: 5
    update_interval: 1s

I wrote a template sensor to calculate a compass heading from these values, as so:

- platform: template
    name: "MLX Heading"
    update_interval: 1s
    accuracy_decimals: 2
    filters:
      - offset: 360
    lambda: !lambda |-
      float x = id(mlx_x).state;
      float y = id(mlx_y).state;
      float h = atan2(x, y)/0.0174532925;
      return h;

I want mlx_heading to appear in HA as a number, but it displays as a string. I can’t set the digits to display in lovelace and history shows the values on a bar, not a graph. I also tried using a number template, as so:

number:
  - platform: template
    name: "MLX Heading 2"
    min_value: -360
    max_value: 360
    step: 0.1
    update_interval: 1s
    set_action: 
      then:
    lambda: !lambda |-
      float x = id(mlx_x).state;
      float y = id(mlx_y).state;
      float h = atan2(x, y)/0.0174532925;
      return h;

but this gives me values different from mlx_heading and I still can’t set the digits in HA. How can I send my heading to HA as a number, either with template or number template. All help is appreciated.

States are always strings.

To get the sensor’s value to show as a numerical graph instead of nominal values, assign a unit_of_measurement in its configuration.

Thank you so much. If I was writing the template in HA, I would probably have put in a unit of measurement (I used degrees), but it didn’t occur to me to do so in ESPHome as I thought the float would take care of that. Thanks again for the prompt correction.