Hue Scenes without Hue Bridge

In order to phase out the Hue Bridge, I was looking for a way to have nicely colored scenes in HA, similar to the Hue ones. I am only aware of this discussion, but it relies on the hue bridge.

My solution relies on three parts:

1. The script

It basically supports the following things:

  • Select a room or a couple of lights.
  • Select a scene by name.
  • The scene is associated with a few color, that are distributed randomly to the participating lights.
  • It’s possible to only apply to lights that are already on.
  • It’s possible to skip light groups.
  • We don’t want to accidentally turn on lights that cannot be colored.

Aaaand here it is (also posted as a GitHub Gist here):

alias: Light / Hue Scene
description: >-
  This script tries to replicate the Hue scenes. Colors are distributed randomly
  on participating lights. Each scene currently has five different colors
  represented by XY values. The script is only applied to lights that support XY
  color mode.
fields:
  target:
    name: Target
    required: true
    description: Select one or more areas or light entities.
    selector:
      target:
        entity:
          domain: light
  scene:
    name: Scene
    description: Which scene? Scenes are taken from the Hue app.
    required: true
    default: Savanna Sunset
    selector:
      select:
        options:
          - Savanna Sunset
          - Golden Pond
          - Horizon
          - Frosty Dawn
  onlyonlights:
    name: Only lights currently on?
    description: If enabled, the scene is only applied to the lights currently on.
    required: false
    default: false
    selector:
      boolean: null
  skipgroups:
    name: Skip groups
    description: If enabled, group light entities will be skipped.
    required: false
    default: true
    selector:
      boolean: null
sequence:
  - variables:
      colors: |-
        {% set scenes = {
          "Savanna Sunset": {
            "colors": [[0.644, 0.3348],[0.5246,
              0.3864],[0.4801, 0.4309],[0.5862, 0.3575],[0.4162, 0.4341]]
          },
          "Golden Pond": {
            "colors": [[0.5695, 0.3999],[0.482,
              0.4489],[0.496, 0.4424],[0.5584, 0.4083],[0.5063, 0.4474]]
          },
          "Horizon": {
            "colors": [[0.2779, 0.2188],[0.1811,
              0.1979],[0.5247, 0.3877],[0.592, 0.385],[0.1731, 0.1978]]
          },
          "Frosty Dawn": {
            "colors": [[0.4221, 0.386],[0.387,
              0.4328],[0.4013, 0.4172],[0.439, 0.3782],[0.4675, 0.3769]]
          }
        }%}
        {{scenes[scene].colors}}
      lights: |-
        {% set l=[]%}
        {% if target.area_id %}
          {% if target.area_id is iterable and not target.area_id is string %}
            {% for a in target.area_id %}
              {% set l = l + area_entities(a)|select('match', 'light.')|list %}              
            {% endfor %}
          {% else %}
            {% set l = l + area_entities(target.area_id)|select('match', 'light.')|list %}
          {% endif %}
        {% endif %}
        {% if target.entity_id %}
          {% if target.entity_id is iterable and not target.entity_id is string %}
            {% set l = l + (target.entity_id|list) %}
          {% else %}
            {% set l = l + [target.entity_id] %}
          {% endif %}
        {% endif %}

        {% if onlyonlights %}
        {% set l = l| select('is_state', 'on')%}
        {% endif %}

        {% if skipgroups %}
        {% set l|from_json %}
        [{%- for ll in l -%}
          {%- if not state_attr(ll, "entity_id")-%}
          "{{ ll }}"
          {%- if not loop.last-%},{%-endif-%}
          {%-endif-%}
        {%- endfor -%}]
        {% endset %}
        {% endif %}

        [{%- for ll in l %}
          {%- set colormodes = state_attr(ll, "supported_color_modes") -%}
          {%- if "xy" in colormodes -%}
          "{{ ll }}"
          {%- if not loop.last-%},{%-endif-%}
          {%- endif -%}
        {%- endfor -%}]
  - repeat:
      for_each: "{{ lights }}"
      sequence:
        - service: light.turn_on
          data:
            xy_color: "{{ colors|random}}"
          target:
            entity_id: "{{ repeat.item }}"
mode: single

2. The colors

The next question: How to get the colors? The Hue app shows them in small circles, but I thought there needs to be a better way to extract the colors. Since I still have the bridge running, I applied the scene to a number of lights, and extracted the color info via Home Assistant with this template script:

{%- set lightstates -%}
[{%- for l in ["light.example1", "light.example2"] -%}
"{{state_attr(l,"xy_color")|list }}"
{% if not loop.last %},{%endif%}
{%- endfor -%}]
{%- endset -%}

{% for xy in lightstates|from_json|unique -%}
{{xy}}
{% if not loop.last %},{%endif%}
{%- endfor -%}

I used the template tool in the developer section of HA to run this, and then copied the colors into the script.

3. The UI

My dashboards are heavily based on the entities and auto-entities cards, so I wanted to integrated these scenes into that, and came up with the above UI. It uses paper-buttons-row to show the buttons. A short tap activates the scene on all lights currently on, and a long tap activates it on all lights in the area.

      - type: custom:paper-buttons-row
        base_config:
          name: false
          styles:
            icon:
              height: 64px;
              width: 64px;
          tap_action:
            action: call-service
            service: script.light_hue_scene
            service_data:
              onlyonlights: true
              skipgroups: true
              target:
                area_id: wohnzimmer
          hold_action:
            action: call-service
            service: script.light_hue_scene
            service_data:
              onlyonlights: false
              skipgroups: true
              target:
                area_id: wohnzimmer
        buttons:
          - entity: script.light_hue_scene
            image: /local/hue/savanna-sunset.jpg
            tap_action:
              service_data:
                scene: Savanna Sunset
            hold_action:
              service_data:
                scene: Savanna Sunset
          - entity: script.light_hue_scene
            image: /local/hue/horizon.jpg
            tap_action:
              service_data:
                scene: Horizon
            hold_action:
              service_data:
                scene: Horizon
          - entity: script.light_hue_scene
            image: /local/hue/golden-pond.jpg
            tap_action:
              service_data:
                scene: Golden Pond
            hold_action:
              service_data:
                scene: Golden Pond

Final thing, because it took me far too long to notice: The Hue scene images are creative commons images, and are linked within the “About” section of the app. From these links (most of them to flickr) they can easily be installed in HA.

Enjoy!

10 Likes

Hey,

I use now your latest build. Is it correct that the “Shuffle Colors” not shuffle the bulbs with the scene colours? But only when you click again on the scene so the bulbs will change from color?

Not entirely sure what you mean. Technically, each scene has a number of associated colors. For each bulb, a random color is picked from this list. Since it‘s only six colors, it can happen that all bulbs pick the same color.

Clear. My question was not written ok.
Shuffle colors dienst make the scene dynamic.

To do that a automation will be needed :slight_smile:

Ah right, you mean the dynamic scenes in hue. Never used them before.
Right, if you repeatedly run the above script, you‘ll get a similar result (I think). But it‘s probably a good idea to change the transition timer, such that each bulb makes a smooth transition from one color to the next.

HI @nilsreiter , I really like that approach and have just tried it out.
However, for me it only works on Hue lamps and strips. All other RGB capable light do not show any change - this includes in particular WLED lightstrips, I would love to have included with the script. They are certainly capable to accept xy-colors.
Could you please check, if you could maybe adjust the script?

Many thanks in advance!
Stefan

Ok, it’s actually pretty simple. The script now checks whether xy or rgb is a supported color mode. Home assistant then handles the rest.

Updated version can be found here: GitHub - nilsreiter/home-assistant-scenes: Nice, Hue-inspired scenes for Home Assistant. Also, I added one “scene” called “Random” which assigns a random colour to each bulb.

3 Likes

Really nice! It works with all sort of lights now. Thanks a lot!
But please have a look into the script code. For me HA throughs an error if lights are turned off. It only works for already switched on lights. Running the service from the developer tools, it appears that the 2 toggles for “only for lights on” and “skip groups” have to be “initialized” (meaning toggled once) - only after that the service will be carried out without throughing an error.

Many thanks and best regards!

Hi, Saw your getting rid of your Hue Bridge. I use DIYHUE.

I have some of the LIFX color bulbs. The script was not working with those. I found that the bulbs report these attributes

min_color_temp_kelvin: 1500
max_color_temp_kelvin: 9000
min_mireds: 111
max_mireds: 666
effect_list: effect_colorloop, effect_pulse, effect_stop
supported_color_modes: color_temp, hs
color_mode: hs
brightness: 255
hs_color: 27.999, 64.706
rgb_color: 255, 166, 90
xy_color: 0.524, 0.385
friendly_name: Kitchen Countertop A19 1
supported_features: 36

I updated the script with the supported_color_modes that I have reported by the LIFX bulbs, adding hs in the if statement

        [{%- for ll in l %}
          {%- set colormodes = state_attr(ll, "supported_color_modes") -%}
          {%- if "xy" in colormodes or "rgb" in colormodes or "hs" in colormodes -%}
          "{{ ll }}"
          {%- if not loop.last-%},{%-endif-%}
          {%- endif -%}
        {%- endfor -%}]

and it is WORKING! Thank you so much for the updated script.

1 Like

I’m using the latest script and it is working great for me.

Is there a way to also set the brightness of the lights? I find that in the evening hours the lights are too bright when I apply the scene. Do I just follow up with an automation step to set the brightness percentage right after setting the scene?

It‘s pretty easy to add to the script.

Within the data dictionary, you can add brightness and specify a value between 0 and 255.

I.e., the snippet would like this:

- service: light.turn_on
  data:
     xy_color: "{{ colors|random}}"
     brightness: 100
  target:
     entity_id: "{{ repeat.item }}"

It‘s probably easier to specify a global variable for brightness, and I can add this into the script, but not until the weekend.

1 Like

Love the script and scenes.

The transition and interval how does it work

transition: 5
interval: 5

After the transition direct a new transition

or

After the transition 5 seconds wait then new transition

I don’t understand the question.

transition is the time that the bulb takes to go from one colour to another. 1s would be a quick fade, 5s would be a slow fade.

interval is how often the bulb should be changing from one colour to another. 60s you would get the same colour for 60s.

Thanks. But means that when transition is done the interval will start. So 5 seconds transition and 10 seconds interval a bulb have 15seconds a color? They are not sequence but after first transition and then interval.

That’s a great idea. Thanks for that. I’ll get to work setting this up straight away. One more question: Did you just happen to see the page on your Github that leads to the color palettes on the Hue subreddit?

I’ll also test the dynamic scenes right away, as they are the only reason for me to continue to operate the Hue Bridge (especially now when Phillips wants to operate the control via the cloud).

The script on GitHub - nilsreiter/home-assistant-scenes: Nice, Hue-inspired scenes for Home Assistant

a) now supports setting a brightness (percentage) value, which will then be applied to all affected lights

b) now has better color selection: It’s still randomised, but if you have more colours than lights, the same color is not used twice. If you have more lights than colors, colours are repeated.

1 Like

Help I Have A Error:
Failed to call service script/1699761739641. UndefinedError: dict object has no element Undefined

How To Fix It???