Using property .state of number returns previous value not curent

the idea is this. I created text_sensor and number (slider) with 4 states. and when o change slider i want to change text_sensor value between “off” , “low” , “mid”,“high” acording to slider state 0, 1, 2, 3.

Problem is this when i set slider from 0 to 1 it show “off” but it should show “low”.
and than when i set slider to 3 text_sensor will show “low” and so on.
it alvays show previous value not actualy set on slider.

Can any 1 help?

this is my code in esphome

text_sensor:
  - platform: template
    name: "Prędkość wentylatora"
    id: Predkosc
    update_interval: never

number:
  - platform: template
    name: "Speed"
    id: "speed_controller"
    step: 1
    min_value: 0
    max_value: 3
    mode: slider
    optimistic: true
    set_action:
      then:
        lambda:  !lambda |-
              if (id(speed_controller).state == 0) id(Predkosc).publish_state("Off");
              if (id(speed_controller).state == 1) id(Predkosc).publish_state("LOW");
              if (id(speed_controller).state == 2) id(Predkosc).publish_state("MID");
              if (id(speed_controller).state == 3) id(Predkosc).publish_state("HIGH");

Try this:

text_sensor:
  - platform: template
    name: "Prędkość wentylatora"
    id: Predkosc
    update_interval: never

number:
  - platform: template
    name: "Speed"
    id: "speed_controller"
    step: 1
    min_value: 0
    max_value: 3
    mode: slider
    optimistic: true
    on_value:
      then:
        lambda:  !lambda |-
              if (id(speed_controller).state == 0) id(Predkosc).publish_state("Off");
              if (id(speed_controller).state == 1) id(Predkosc).publish_state("LOW");
              if (id(speed_controller).state == 2) id(Predkosc).publish_state("MID");
              if (id(speed_controller).state == 3) id(Predkosc).publish_state("HIGH");