Control NeoPixel with double_click

Hello together,

I am trying my hand at Home assistant for the first time. As my first project I want to create a night lamp for my little daughter.

Everything works fine expecting one thing. I want to integrate the double_click function to switch the NeoPixel between predefined colors. But I don’t know how. Have someone a good idea, what is the easiest (and maybe best) way?

I also already red the ESPHome documentation, but to be honest I still don’t know how to… :grimacing:

I have tried the lambda function to increase a global var, but then I am not sure how to check the value and choose the color.

Thanks in advance
Edi

A double click on what? Something on a screen? Or a button connected to the esp?

As a inspiration :wink:
I would solve it like this (untested)

light:
  - platform: neopixel
    id: neopixel
    type: GRB
    variant: WS2811
    pin: GPIO23
    num_leds: 60
    name: "NeoPixel on Light"
globals:
   - id: globalcounter
     type: int
     restore_value: no
     initial_value: '1'
binary_sensor:
  - platform: gpio
    pin: D2
    name: "Button on Lamp"
    on_click:
      min_length: 50ms
      max_length: 350ms
      then:
        - light.toggle: neopixel
    on_double_click:
      min_length: 50ms
      max_length: 350ms
      then:
      - globals.set:
        id: globalcounter
        value: lambda: |-
          if (id(globalcounter) > 5) {
            return id(globalcounter) += 1;
          } else {
            return 1;
          }
          ESP_LOGD(TAG, "Global Counter is: %d", id(globalcounter));
       - if:
          condition:
            lambda: 'return id(globalcounter) == 1;'
          then:
            - light.addressable_set:
               id: neopixel
               red: 100%
               green: 0%
               blue: 0%
       - if:
          condition:
            lambda: 'return id(globalcounter) == 2;'
          then:
            - light.addressable_set:
               id: neopixel
               red: 0%
               green: 100%
               blue: 0%
[etc.. for 3, 4, 5]
    

Thank you leinich!
Your example don’t work, but was a good base :wink:

My solution looks like this and works fine:

light:
  - platform: ...
  bla bla bla

globals:
  - id: color_global_int
    type: int
    restore_value: no
    initial_value: '1'

binary_sensor:
  - platform: gpio
    pin: GPIO0
    name: "Button"
    # Single-click
    on_click:
      min_length: 50ms
      max_length: 350ms
      then:
        - light.toggle: neo
    # Long-click
    on_multi_click:
    - timing:  
      - ON for 0.5s to 1s
      - OFF for at least 0.2s
      then:
        - lambda: |-
            if (id(color_global_int) < 5){
              id(color_global_int) += 1;
            } else {
              id(color_global_int) = 1;
            }
        - if:
            condition:
              lambda: 'return id(color_global_int) == 1;'
            then:
              - light.turn_on: # pink
                  id: neo
                  brightness: 40%
                  red: 45%
                  green: 0%
                  blue: 35%
        - if:
            and so on...

What I don’t understand is the click and double-click function. Every time when I double clicked the lamp goes off. But wenn I turned it on again the color was already changed. The lamp didn’t stay on… Do you know why?
This is why I’m using multi_click. And it works fine.

Again many thanks

Best regards
Edi

Hi, did you ever get this working nicely? I don’t have a neopixel but a RGBW strip and hoping to achieve something similar…