Trying to cycle through colors on a Hue bulb each time a button is pressed

I’m trying to set up an automation that advances the color of a Hue bulb through a predefined list of colors each time and cannot get my approach to work, so I’m wondering if there is another way to go about it. The method I’m attempting is:

  • Check the rgb_color attribute of the light in question
  • Compare the value received to the predefined list
  • If it’s in the list then advance the list index by one or loop back to 0 if the end has been reached
  • If it’s not in the list, then go to the first color of the list

The problem is that the value returned by the rgb_color attribute is never exactly what I set it to, so the automation always tries to change the color to the first list entry. I’ll post a different topic to try and figure out that issue; my question in this topic is just if there is a different method to accomplish my end goal.

Here’s my code:

- alias: Double Press
  description: ""
  trigger:
    - device_id: 5e63c76894a53aeeeeee4c2e7ea8d834
      domain: zha
      platform: device
      type: remote_button_double_press
      subtype: remote_button_double_press
  condition: []
  variables:
    color_cycle:
      - [0, 255, 0]
      - [123, 123, 123]
      - [255, 167, 90]
      - [255, 0, 0]
      - [0, 255, 0]
      - [0, 0, 255]
  action:
    - service: light.turn_on
      data_template:
        entity_id: light.bedside_table
        rgb_color: >
          {% set current = state_attr('light.bedside_table', 'rgb_color')|list %}
          {% set idx = color_cycle.index(current) + 1 if current in color_cycle else 0 %}
          {% set color = color_cycle[idx] %}
          [{{color[0]-}}, {{color[1]-}}, {{color[2]}}]
  mode: single

I’m running HassIO 2022.11.4

1 Like

Why not maintain a variable for that tells you what idx you last set it to, and when the button is pressed move idx to idx+1?

Set a random color on a light
He has a cycle version as well, otherwise I would think using scenes as a better approach.
(Note: That trying to get the current colour state of more than one entity @ A time is problematic).
But you could add A input_select helper & have the script, change that; so you have A pre-set variable.

That would be a great solution, I didn’t realize it was possible. The hass-variables repo seems to provide this functionality, though.

[edit] Or this functionality is available built in via input_number

Here’s what I’ve come up with so far:

- alias: Double Press
  description: ""
  trigger:
    - device_id: 5e63c76894a53aeeeeee4c2e7ea8d834
      domain: zha
      platform: device
      type: remote_button_double_press
      subtype: remote_button_double_press
  condition: []
  variables:
    colour: [255, 0, 0]
    color_cycle:
      - [255, 0, 0]
      - [0, 255, 0]
      - [0, 0, 255]
  action:
    - service: light.turn_on
      data_template:
        entity_id: light.bedside_table
        rgb_color: >
          {% set idx = states('input_number.bedroom_color_index') | int + 1 %}
          {% set idx = idx % color_cycle | length %}
          {% set color = color_cycle[idx] %}
          [{{color[0]-}}, {{color[1]-}}, {{color[2]-}}]
    - service: input_number.set_value
      entity_id: input_number.bedroom_color_index
      data:
        value: >
          {% set idx = states('input_number.bedroom_color_index') | int + 1 %}
          {% set idx = idx % color_cycle | length %}
          {{ idx }}
  mode: single

I still need to modify it to keep the current brightness level and only change the color, but otherwise it seems to do the trick. Thanks for pointing me in the right direction!

1 Like

Very interesting thread!
Anyone knows how to cycle through color presets defined in Lovelace instead?
Thanks