Changes to number component in home-assistant do not reflect in ESPHome

Hi there,
I have a number component in my ESPHome config. In home-assistant, I see a slider. However, when I move the slider in home-assistant, it is not reflected in ESPHome number component.

Here’s the code:

servo:
  - id: servo1
    output: pwm_output
    auto_detach_time: 0.1s
    transition_length: 5s

output:
  - platform: esp8266_pwm
    id: pwm_output
    pin: GPIO0
    frequency: 50 Hz


number:
  - platform: template
    id: servo_position
    name: "Servo position"
    min_value: 1
    max_value: 5
    step: 1
    set_action:
      - if:
          condition:
            lambda: |-
              return id(servo_position).state == 1;
          then:
            - logger.log: "Condition 1 triggered: Moving servo to -100%"
            - servo.write:
                id: servo1
                level: -100.0%
      - if:
          condition:
            lambda: |-
              return id(servo_position).state == 2;
          then:
            - logger.log: "Condition 2 triggered: Moving servo to -50%"
            - servo.write:
                id: servo1
                level: -50.0%
      - if:
          condition:
            lambda: |-
              return id(servo_position).state == 3;
          then:
            - logger.log: "Condition 3 triggered: Moving servo to 0%"
            - servo.write:
                id: servo1
                level: 0.0%
      - if:
          condition:
            lambda: |-
              return id(servo_position).state == 4;
          then:
            - logger.log: "Condition 4 triggered: Moving servo to 50%"
            - servo.write:
                id: servo1
                level: 50.0%
      - if:
          condition:
            lambda: |-
              return id(servo_position).state == 5;
          then:
            - logger.log: "Condition 5 triggered: Moving servo to 100%"
            - servo.write:
                id: servo1
                level: 100.0%

here’s what happens on home-assistant (the slider is set on 4, but as you can see the number is 1):

I haven’t used this before, so I’m not 100% sure. But the docs say the new value is available in the x variable. So I would imagine you should not look at the state, but use x to determine the new state, as well as pick what you want to do when the new state is x.

1 Like

Oh man thank you thank you thank you! I’ve been hitting my head on this for 2 hours!

Could you please post your working yaml?

Here you go!

number:
  - platform: template
    id: servo_position
    name: "Servo position"
    min_value: 1
    max_value: 5
    step: 1
    set_action:
      - if:
          condition:
            lambda: |-
              return x == 1;
          then:
            - logger.log: "Condition 1 triggered: Moving servo to -100%"
            - servo.write:
                id: servo1
                level: -100.0%
      - if:
          condition:
            lambda: |-
              return x == 2;
          then:
            - logger.log: "Condition 2 triggered: Moving servo to -50%"
            - servo.write:
                id: servo1
                level: -50.0%
      - if:
          condition:
            lambda: |-
              return x == 3;
          then:
            - logger.log: "Condition 3 triggered: Moving servo to 0%"
            - servo.write:
                id: servo1
                level: 0.0%
      - if:
          condition:
            lambda: |-
              return x == 4;
          then:
            - logger.log: "Condition 4 triggered: Moving servo to 50%"
            - servo.write:
                id: servo1
                level: 50.0%
      - if:
          condition:
            lambda: |-
              return x == 5;
          then:
            - logger.log: "Condition 5 triggered: Moving servo to 100%"
            - servo.write:
                id: servo1
                level: 100.0%

1 Like