Button Press: Change Bulb Color

Hi

I searched the forum but couldn’t find an answer.
I have some zigbee light bulbs, and an aqara zigbee button (I am using this blueprint ZHA - Aqara Wireless Mini Switch).
I would like to change the color of the bulb when doing a long press. Ideally, there would be a list of color and each long press would just go through the list and then back to 0.
I have no idea how to start doing that, I would appreciate help on this.

Thanks!

One of the issues I am running into is that the rgb status of the light will not be perfectly what I set (i.e. (255,0,0) will become (255,0,14)).
So I cannot set a condition. Is there a way to create a list and have a variable index attached to the entity that I could increment?

I’m wondering the same thing. I got a zwave remote, and set up a blueprint for it, but I’d like to be able to cycle (named?) colors with each press or have it rotate through the color wheel with a hold. How can this be set up?

1 Like

Btw I tried to set the color using the xy mode and since I got a few different brands that use either RGB/HS or XY, that gives back a non exact outcome as well.

I found a solution using input_select. I will post the code later.

1 Like

There we go. That’s the script to cycle through a list of colors set in an input_select.

The input_select entry:

bedroom_desk_color:
name: Bedroom Desk Bulbs Color
options:
- red
- orange
- yellow
- green
- blue
- purple
- pink
initial: red
icon: mdi:palette

And the script:

alias: "Color Cycle: Bedroom Desk Bulbs"
sequence:
  - service: light.turn_on
    data:
      color_name: "{{ states('input_select.bedroom_desk_color') }}"
    target:
      entity_id: light.bedroom_desk_bulbs
    enabled: true
  - service: input_select.select_next
    data: {}
    target:
      entity_id: input_select.bedroom_desk_color
mode: single
icon: mdi:palette

Now I am turning this in a blueprint that works with the Aqara Mini Switch. I am struggling to be able to set either color_name or color_temp. If I am trying to set only one, it works, but I am not sure how to switch between both using a template.

The blueprint snippet is:

  - conditions:
    - '{{ command == ''double'' }}'
    sequence:
      - service: input_select.select_next
        data: {}
        target:
          entity_id: '{{ light_mode }}'    
      - service: light.turn_on
        data:
          color_name:  >
            {% if is_state(light_mode, "color") %}
            {{ states(color_list) }}
            {% else %}
            {{ states(whitetemps_list) }}
            {% endif %}
        target: 
          entity_id: '{{ target_light }}'

I would like here to either set the color_name or color_temp depending on light_mode.
Is that even possible? I tried to shift color_name or color_temp inside the loop but it doesn’t work.

You could just use the hue value.

(current hue value + 15) % 360

I will still face the problem that I want to use color_name in some case and another mode otherwise, no?

Why do you need color name?
Hue is just a number between 0 and 359 that represents all colors.

Yeah I guess I could convert my colors to hue… however just for curiosity is it possible to achieve the conditional data type?
It’s overall easier as well to set a list of colors with names… using hue means i need to set the saturation and brightness as well

What conditional data?
Color names will only cover a few of the colors a bulb can display.
If you got it working with names then great keep it that way.
I just wanted to suggest hue since it’s a lot easier to work with

Yes I only need the simple colors in my list that’s why I chose this solution.
By conditional data type I mean that I need to set the data for the light.turn_on to be either color_name or color_temp.

I managed to get this behaviour using a python script:

entity_id = data.get("entity_id")
color_list = data.get("color_list")
light_mode = data.get("light_mode")
temperature_list = data.get("temperature_list")

current_mode  = hass.states.get(light_mode).state
if entity_id is not None and color_list is not None and light_mode is not None and temperature_list is not None:
    if current_mode == 'color':
        current_color = hass.states.get(color_list).state
        service_data = {"entity_id": entity_id, "color_name": current_color}
        hass.services.call("light", "turn_on", service_data, False)
        
    elif current_mode == 'white':
        current_temperature = int(hass.states.get(temperature_list).state)
        service_data = {"entity_id": entity_id, "color_temp": current_temperature}
        hass.services.call("light", "turn_on", service_data, False)

Along with this automation:

action:
- choose:
  - conditions:
    - '{{ command == ''single'' }}'
    sequence:
      - service: light.toggle
        data: {}
        target:
          entity_id: '{{ target_light }}'
  - conditions:
    - '{{ command == ''double'' }}'
    sequence:
      - service: input_select.select_next
        data: {}
        target:
          entity_id: '{{ light_mode }}'    
      - service: python_script.color_light_cycle
        target:
          entity_id: "light.bedroom_desk_bulbs"
        data:
          color_list: "input_select.bedroom_desk_color"
          light_mode: "input_select.bedroom_desk_mode"
          temperature_list: "input_select.bedroom_desk_white_temp"
  - conditions:
    - '{{ command == ''hold'' }}'
    sequence:
      - service: input_select.select_next
        data: {}
        target:
          entity_id: >
            {% if is_state(light_mode, "color") %}
            {{ color_list }}
            {% else %}
            {{ whitetemps_list }}
            {% endif %}     
      - service: python_script.color_light_cycle
        target:
          entity_id: "light.bedroom_desk_bulbs"
        data:
          color_list: "input_select.bedroom_desk_color"
          light_mode: "input_select.bedroom_desk_mode"
          temperature_list: "input_select.bedroom_desk_white_temp"
1 Like

As a massive HASS noob,… where do I copy paste all this in? Where can I put the pythin script in? For the automation, I would assume in the automation section, but where? Some guidance would be highly appreciated…

Appreciated! thanx for the quick reply (you have no idea how long I have searched for how to make these things work… knowing what you have to search for helpes a ton ;-))