ESPHome: Controle dimmable light with esp32 and button

Hello,
I come to you because I can’t find how to program what I want to do.
My goal is to control 8 light circuits in a dimmable way using an esp32 connected to this card to make it simple. as well as 8 push buttons.

What I would like to program is that when I press my button: - for example <1 second it turns the light on or off

  • If I hold it down, it gradually increases the brightness.

I managed to program the two separately, but I can’t put these two functions together

Below is my example on one of the lamps.

Thanks

binary_sensor:

  - platform: gpio
    pin:
      number: GPIO32
#      allow_other_uses: true
      mode: INPUT_PULLUP
      inverted: True
    name: "Button 1"
    id: Button1
#    on_press:
#        - output.turn_on: dimmer1
    on_click: 
      then:
        - output.turn_on: dimmer1
                
    on_press:
      - while:
          condition:
            binary_sensor.is_on: Button1

          then:
            - light.dim_relative:
                id: Light_1
                relative_brightness: 5%
                transition_length: 0.1s
                brightness_limits:
                    max_brightness: 90%
            - delay: 0.1s

output:
  - platform: ac_dimmer
    id: dimmer1
    gate_pin: GPIO22
    init_with_half_cycle: true
    zero_cross_pin:
      number: GPIO23
      allow_other_uses: true
      mode:
        input: true
      inverted: yes
    min_power: 10% 
    max_power: 100%  

light:
  - platform: monochromatic
    output: dimmer1
    name: Light 1
    default_transition_length: 25ms
    gamma_correct: 0.0
    id: Light_1  

They give you an example for doing this exact thing in the documentation. This is why its important to actually read through it.

I don’t understand what example you are talking about, maybe my English is too bad or my understanding of esphome is bad. :neutral_face:

Well, its plainly titled, “Example: dimming a light with a button press” so, it shouldnt be very confusing, right?

Ho OK, But as said in my post I know how to do it, in fact I followed the documentation. What I can’t program is the two functions at the same time.
Namely, Single presses to turn on or off and long presses to vary the intensity of the light.
When I try to integrate the two I always have problems, either the ON/OFF no longer works, or the dimmer part no longer works correctly

For example my last attempt.
Below, the short press does not really work when turning on, it considers that it is an on_press. If I keep it pressed it varies well, and if I press it short when already on it turns off well.

    on_click:
      then:
        - light.toggle: Light_1
    on_press:
      - while:
          condition:
            binary_sensor.is_on: Button1
          then:
            - light.dim_relative:
                id: Light_1
                relative_brightness: 5%
                transition_length: 0.1s
                brightness_limits:
                    max_brightness: 90%
            - delay: 0.1s

Try using on_multi_click to specify what click timings correspond to different actions. This allows you to guarantee that the different click timings are mutually exclusive so the on_click and on_press actions don’t simultaneously fire.

Here the single click works well, but the long click does not, it increments by 5% per long click and not continuously


    on_multi_click:
    - timing:
        - ON for at most 0.7s

      then:
        - logger.log: "simple click"
        - light.toggle: Light_1
    - timing:
        - ON for at least 1s
      then:
        - logger.log: "LONG click"
        - light.dim_relative:
            id: Light_1
            relative_brightness: 5%
            transition_length: 0.1s
            brightness_limits:
                max_brightness: 90%
        - delay: 0.1s

Use while:, as you did here:

I’m sorry but I don’t see where you’re going.

 on_multi_click:
    - timing:
        - ON for at most 0.7s
      then:
        - logger.log: "simple click"
        - light.toggle: Light_1
    - timing:
        - ON for at least 1s
      then:
        - logger.log: "LONG click"
        - while:
            condition:
              binary_sensor.is_on: Button1
            then:
              - light.dim_relative:
                  id: Light_1
                  relative_brightness: 5%
                  transition_length: 0.1s
                  brightness_limits:
                      max_brightness: 90%
              - delay: 0.1s

Ho OK! Great! That’s all I needed! Thank you very much for your help! It seems to be OK :slightly_smiling_face:

1 Like

You can also find a lot of templates here:

Maybe your type is there already, if not, you can use it as guideline to write your own :wink:

I know I’ve already looked, but in these cases, the programming is a bit too complicated for me.
I was looking for something simple :slightly_smiling_face:
Thanks

It is customary to assign the solution tag to the post which actually provided a solution, so that others with the same issue might find it immediately.

In this case, it is obvious that Daniel’s post right above yours offers a complete solution, while your post with the current solution tag just says it worked.

Could you kindly assign a proper solution tag to the post just above yours?

1 Like