Changing ESPHome slider value in frontend

Hi all together,

since the ESPHome and HA support new entities and components with the new update, I’m especially interested in ESPHome’s number component.

That’s my code:

globals:
    # This variable stores the current mode chosen by pressing the buttons (physical or digital)
  - id: totalClick
    type: int
    restore_value: no
    initial_value: "0"

sensor:
    # This sensor only displays the current mode chosen by pressing one of the buttons (digital or physical button)
  - platform: template
    id: buttonClickSensor
    name: "esp01s01_buttonClickSensor"
    update_interval: 0.5s
    accuracy_decimals: 0
    #unit_of_measurement: "clk"
    lambda: return id(totalClick);

number:
  - platform: template
    name: "esp01s01_mode_slider"
    id: "modeSlider"
    step: 1
    min_value: 0
    max_value: 3
    mode: slider
    set_action:
      then:
        - lambda:  id(totalClick) = x;

button:
    # This is the digital button which is the equivalent of the physical button
  - platform: template
    name: "esp01s01_publicButton"
    id: "publicButton"
    on_press:
      - lambda: |-
          id(totalClick)++;
          if( id(totalClick) > 3 ) id(totalClick) = 0;
      - switch.turn_on: ssrLogicMain

That’s the fronend view:
image

What is happening (working properly):

  • Clicking the publicButton changes the value of buttonClickSensor (each press increments the value by 1; if it is greater than 3, it’s getting reset to 0), marked in RED
  • Changing the mode_slider changes the value of buttonClickSensor, marked in GREEN

What is not happening yet:

  • Clicking the publicButton changes the value of mode_slider, YELLOW ARROW
  • Changing the mode_slider or clicking the publicButton shows the current value next to the mode_slider, YELLOW CIRCLE => if it would work it could replace the buttonClickSensor

Can someone tell me how to reach that with the “externally” changed mode_slider?

I tried various combinations including on_value... set.number: ...., but nothing worked.

1 Like

This morning I installed the new versions:

  • HA core: core-2021.12.2
  • ESPHome: 2021.12.1

The behavious is still the same after updating my ESP device.

Does anyone have an idea?

Hi,
I ran into the same problem as you, when i tried to use your example.
Adding “optimistic: true” in fixed the slider problem.
Hope it helps.

3 Likes

It is right solution. My friend.
Thank you!

Can you share where to put the optimistic:true statement in the yaml code?

Just dropping in to answer Michel’s question. The optimistic: true goes in the number definition:

number:
  - platform: template
    name: "esp01s01_mode_slider"
    id: "modeSlider"
    step: 1
    min_value: 0
    max_value: 3
    mode: slider

    # <em>
    optimistic: true
    # </em>

    set_action:
      then:
        - lambda:  id(totalClick) = x;

There is documentation on the Template Number documentation page.

2 Likes