Select/activate Hue scenes in new v2 Api setup

Yes you’re right. If a scene that contains playback is used, playback starts immediately after the scene is activated.
Default HUE scenes have this behavior if they have multiple colors. If I create my own scene, it works as expected because it doesn’t have playback.

This behavior doesn’t happen with the, now being depreciated, old way of calling Hue scenes using the hue.hue_activate_scene method.

In fact, I now see in the developer tools it has an optional V2-API boolean parameter “dynamic” that you can give to activate the dynamic behavior.

IMO it appears that this parameter is missing in the current scene.turn_on command, or a default behavior where this is not activated.

Yes it works, but you get a warning in the log.

Use of service_call 'hue_activate_scene' is deprecated and will be removed in a future release. Please use scene entities instead

I assume that in the new V2 way of calling the scene it will be fixed because now I could consider it a mistake. But if that’s not the expected behavior?

1 Like

Good news: I wanted to open an issue on GitHub about this, but just found out it is already being worked on, with default behavior being not to activate the dynamic scenes:
Philips Hue v2 When using scene entities dynamic is always set to true · Issue #61554 · home-assistant/core · GitHub

Conclusion: for the short term you can still use the hue_activate_scene (with warnings) while for the permanent fix in scene.turn_on this will be fixed in the next major release next year.

Thank you for the information.

changed my scene selector template to:

        filter:
          template: |
            {% set hue_group = states('input_select.hue_group_v2')|lower|replace(' ','_') %}
            {% set ns = namespace(scenes=[]) %}
            {%- for s in states.scene
              if hue_group == s.attributes.group_name|lower|replace(' ','_')%}
                {% set ns.scenes = ns.scenes + [{'entity':s.entity_id,
                  'name': s.object_id.split(hue_group +'_')[1]|capitalize|replace('_',' ') }] %}
            {%- endfor %}
            {{ns.scenes}}

which makes use of the attribute group_name and by using that simply evaluates whether the selected hue_group is identical to the group_name…

no more convoluted template to compare eg group Dining with group Dining table, and select appropriate scenes (and list their names, without the group prefix)

should have thought of that before tbh…

taking that 1 step further, and using the new |slugify filter allows to do:

        filter:
          template: |
            {% set hue_group = states('input_select.hue_group_v2') %}
{{hue_group}}
            {% set ns = namespace(scenes=[]) %}
            {%- for s in states.scene
              if hue_group == s.attributes.group_name%}
                {% set ns.scenes = ns.scenes + [{'entity':s.entity_id,
                  'name': s.object_id.split(hue_group|slugify +'_')[1]|capitalize|replace('_',' ') }] %}
            {%- endfor %}
            {{ns.scenes}}

here’s some additional pictures for the season:

gouden_ster
gouden_vijver
onder_de_boom
stille_nacht

added some niceties to the cards (scrolling, anchors) and initial support for Dynamic scenes (activate/deacitvate/speed/brightness) which shows on the condition Dynamic scene are available for the selected group. Before activating Dynamic scene, record state of lights on the fly (works with the template!), and restate those when stopping the dynamic scene.

so full config working now is:

type: entities
title: Set Hue scenes
show_header_toggle: false
card_mod:
  style: |
    .card-header {
      background-color: var(--background-color-off);
      color: var(--text-color-off);
      padding-top: 0px;
      padding-bottom: 0px;
      margin: 0px 0px 16px 0px;
    }
    ha-card {
      margin: 0px 0px 16px 0px; /* create space before next card in view*/
    }
entities:
  - input_select.hue_group
  - type: custom:fold-entity-row
    card_mod: &scroll
      style: |
        #measure {
          max-height: 300px;
          overflow-x: hidden;
          overflow-y: scroll;
        }
    head:
     type: section
     label: Regular scenes
     card_mod: &label
       style: |
         .label {
           margin-left: 0px;
         }
    <<: &config
      padding: 0
      no_animation: true
    entities:
      - type: custom:auto-entities
        card: &mod
          type: entities
          card_mod:
            style: |
              ha-card {
                box-shadow: none;
                margin: 0px -16px;
              }
        show_empty: false
        filter:
          template: |
            {% set hue_group = states('input_select.hue_group') %}
            {% set ns = namespace(scenes=[]) %}
            {%- for s in states.scene
              if hue_group == s.attributes.group_name and
                  s.attributes.is_dynamic != true %}
                {% set ns.scenes = ns.scenes + [{'entity':s.entity_id,
                  'name': s.object_id.split(hue_group|slugify +'_')[1]|capitalize|replace('_',' ') }] %}
            {%- endfor %}
            {{ns.scenes}}

  - type: conditional
    conditions:
      - entity: binary_sensor.selected_hue_group_has_active_scenes_available
        state: 'on'
    row:
      type: custom:fold-entity-row
      #card_mod: *scroll
      head:
       type: section
       label: Dynamic scenes
       card_mod: *label
      <<: *config
      entities:
        - type: custom:auto-entities
          card:
            type: entities
            card_mod:
              style: |
                ha-card {
                  box-shadow: none;
                  margin: 0px -16px;
                }
                .card-content {
                   max-height: 250px;
                   overflow-y: scroll;
                }
          filter:
  # list with scenes, without the group name, to have better frontend presentation
            template: |
              {% set hue_group = states('input_select.hue_group') %}
              {% set ns = namespace(scenes=[]) %}
              {%- for s in states.scene
                if hue_group == s.attributes.group_name and
                    s.attributes.is_dynamic == true %}
                  {% set ns.scenes = ns.scenes + [{'entity':s.entity_id,
                    'name': s.object_id.split(hue_group|slugify +'_')[1]|capitalize|replace('_',' ') }] %}
              {%- endfor %}
              {{ns.scenes}}

  # list with scenes and their full name, including the group name
  #            {% set hue_group = states('input_select.hue_group') %}
  #            {% set ns = namespace(scenes=[]) %}
  #            {%- for s in states.scene
  #              if hue_group == s.attributes.group_name and
  #                  s.attributes.is_dynamic == true %}
  #                {% set ns.scenes = ns.scenes + [s.entity_id] %}
  #            {%- endfor %}
  #            {{ns.scenes}}


  # Initial support for (de)activating dynamic scenes
  # Hope to change to button toggle
  # Maybe only show when Available and active

        - input_select.hue_dynamic_scene
        - input_number.hue_dynamic_scene_speed
        - input_number.hue_dynamic_scene_brightness
        - type: conditional
          conditions:
            - entity: binary_sensor.selected_hue_group_active_scene
              state: 'off'
          row:
            entity: script.activate_hue_dynamic_scene
            icon: mdi:play
            action_name: play
        - type: conditional
          conditions:
            - entity: binary_sensor.selected_hue_group_active_scene
              state: 'on'
          row:
            entity: script.deactivate_hue_dynamic_scene
            icon: mdi:stop
            action_name: stop

        - type: conditional
          conditions:
            - entity: binary_sensor.hue_groups_dynamic_scening
              state: 'on'
          row:
            type: custom:fold-entity-row
            head:
             type: section
             label: Currently scening group
             card_mod: *label
            <<: *config
            entities:
              - type: custom:auto-entities
                card: *mod
                show_empty: false
                filter:
                  include:
                    - attributes:
                        dynamics: true

#          {{states.light
#            |selectattr('attributes.dynamics','eq',true)
#            |map(attribute='name')|list}}

        - type: conditional
          conditions:
            - entity: binary_sensor.hue_groups_dynamic_scening
              state: 'on'
          row:
            type: custom:fold-entity-row
            head:
             type: section
             label: Currently scening lights
             card_mod: *label
            <<: *config
            entities:
              - type: custom:auto-entities
                card: *mod
                show_empty: false
                filter:
                  include:
                    - attributes:
                        dynamics: dynamic_palette

  - type: custom:template-entity-row
    entity: >
      light.{{states('input_select.hue_group')|slugify}}
    secondary: >
      {% set x = ['unknown','unavailable'] %}
      {% if states('input_select.hue_group') not in x %}
      {% set object = states('input_select.hue_group')|slugify %}
      {% set group = 'light.' + object %}
        {% set bri = state_attr(group,'brightness') %}
        {% set rgb = state_attr(group,'rgb_color') %}
        {% if states(group) == 'on' %} Bri: {{bri}}, Rgb: {{rgb}}
        {% else %} Off
        {% endif %} since {{relative_time(states[group].last_changed) if (states[group]
          and states[group].last_changed not in x) else 'Initializing....'}}
      {% else %} Not yet set
      {% endif %}

    toggle: true

  - type: custom:fold-entity-row
    head:
     type: section
     label: Automations
     card_mod: *label
    <<: *config
    entities:
      - entity: automation.auto_populate_hue_group_input_select
        secondary_info: last-triggered
      - entity: automation.auto_populate_hue_dynamic_scene_input_select
        secondary_info: last-triggered

necessary back-end code:

##########################################################################################
# Inputs
##########################################################################################

input_select:

# need at least 1 option for the config check, which will be overwritten during startup
  hue_group:
    name: Select Hue group
    options:
      - Alarm
#    initial: Alarm

  hue_dynamic_scene:
    name: Hue Dynamic scene
    options:
      - 'Alarm - Tyrell'

input_number:

  hue_dynamic_scene_speed:
    name: Hue scene speed
    min: 0
    max: 100
    step: 10

  hue_dynamic_scene_brightness:
    name: Hue scene brightness
    min: 0
    max: 255
    step: 10

##########################################################################################
# Templates
##########################################################################################

template:

  binary_sensor:

    - unique_id: selected_hue_group_active_scene
      name: Selected Hue group active scene
      state: >
        {% set select = states('input_select.hue_group')|slugify %}
        {% set group ='light.' + select %}
        {{is_state_attr(group,'dynamics',true)}}

    - unique_id: selected_hue_group_has_active_scenes_available
      name: Selected Hue group has active scenes available
      state: >
          {% set hue_group = states('input_select.hue_group') %}
          {{states.scene
            |selectattr('attributes.group_name','eq',hue_group)
            |selectattr('attributes.is_dynamic','eq',true)|list|length != 0}}

    - unique_id: hue_groups_dynamic_scening
      name: Hue groups dynamic scening
      state: >
        {{states.light|selectattr('attributes.dynamics','eq',true)|list|length != 0}}

#        {% set select = states('input_select.hue_group') %}
#        {% set group = states.light
#          |selectattr('attributes.friendly_name','eq',select)
#          |map(attribute='entity_id')|join %}
#        {{is_state_attr(group,'dynamics',true)}}

##########################################################################################
# Scripts
##########################################################################################

script:

  activate_hue_dynamic_scene:
    alias: Activate Hue dynamic scene
    mode: restart
    icon: mdi:power
    sequence:
      - condition: >
          {{is_state('binary_sensor.selected_hue_group_active_scene','off')}}
# first record current state of lights in the selected group
      - service: scene.create
        data:
          scene_id: before_dynamic_scene
          snapshot_entities:
            - >
              {% set select = states('input_select.hue_group')|slugify %}
              {% set group ='light.' + select %}
              {{group}}
      - service: hue.activate_scene
        data:
          dynamic: true
          speed: >
            {{states('input_number.hue_dynamic_scene_speed')|int}}
          brightness: >
            {{states('input_number.hue_dynamic_scene_brightness')|int}}
        target:
          entity_id: >
            {% set select = states('input_select.hue_dynamic_scene')|slugify %}
            {{'scene.' + select}}

#            {% set select = states('input_select.hue_dynamic_scene')%}
#            {{states.scene
#             |selectattr('attributes.friendly_name','eq',select)
#             |map(attribute='entity_id')|join}}

  deactivate_hue_dynamic_scene:
    alias: Deactivate Hue dynamic scene
    mode: restart
    icon: mdi:power-off
    sequence:
      - condition: >
          {{is_state('binary_sensor.selected_hue_group_active_scene','on')}}
      - service: hue.activate_scene
        data:
          dynamic: false
        target:
          entity_id: >
            {% set select = states('input_select.hue_dynamic_scene')|slugify %}
            {{'scene.' + select}}
# reset the lights to the state before the dynamic scene activation
      - service: scene.turn_on
        target:
          entity_id: scene.before_dynamic_scene
#      - service: light.turn_on
#        data:
#          profile: relax
#        target:
#          entity_id: >
#            {{'light.' + states('input_select.hue_group')|slugify}}

#            {% set select = states('input_select.hue_dynamic_scene')%}
#            {{states.scene
#             |selectattr('attributes.friendly_name','eq',select)
#             |map(attribute='entity_id')|join}}

##########################################################################################
# Automations
##########################################################################################

automation:

  - alias: Auto populate Hue group input select
    id: Auto populate Hue group input select
    mode: restart
    trigger:
      platform: event
      event_type: delayed_homeassistant_start
    action:
      service: input_select.set_options
      target:
        entity_id: input_select.hue_group
      data:
        options: >
# expand(integration_entities('Philips Hue 1'))
          {{states.light
            |selectattr('attributes.is_hue_group','eq',true)
            |map(attribute='name')|list}}

  - alias: Auto populate Hue dynamic scene input select
    id: Auto populate Hue dynamic scene input select
    mode: restart
    trigger:
      - platform: event
        event_type: delayed_homeassistant_start
      - platform: state
        entity_id: input_select.hue_group
    action:
      service: input_select.set_options
      target:
        entity_id: input_select.hue_dynamic_scene
      data:
        options: >
          {% set hue_group = states('input_select.hue_group') %}
          {{states.scene
            |selectattr('attributes.group_name','eq',hue_group)
            |selectattr('attributes.is_dynamic','eq',true)
            |map(attribute='name')|list}}

Please confirm which card you currently use for this yaml?
Thank you, your work is excellent sir.

not sure I follow, this is the whole card?

The new slugify filter is such a blessing for umlauts people!

Turns code monster like


      options: |
            {% set phrase =  ['…','Aus'] %}
            {% set x = states('input_select.hue_gruppen2')|lower |replace(' ', '_') |replace('ä','a') |replace('ü','u') %}
            {{ phrase + state_attr('light.' ~ x, 'hue_scenes') |list |sort }

into


      options: |
            {% set phrase =  ['…','Aus'] %}
            {% set x = states('input_select.hue_gruppen2')|slugify %}
            {{ phrase + state_attr('light.' ~ x, 'hue_scenes') |list |sort }}

Thank you for inspiration!

1 Like

nice use of the hue_scenes attributes. Now how to use that and pick only those that are dynamic… we have to resort to my more complex namespace construction unfortunately?
During the migration to v2 I asked to have a list of entity_id’s too, but that wasnt allowed (because the api doesnt return entity_id’s, only names. And the HA integration directly forwards only what’s in the api, no own constructions.
Would have been easy otherwise ;-(

Yes, I would also like to get to grips with the new possibilities.

Maybe something like


{% set x = states('input_select.hue_gruppen2') %}
{{ expand(device_entities(device_id('hue_bridge')))
            |selectattr('attributes.is_dynamic','eq', true)
            |selectattr('attributes.group_name','eq', x)
            |map(attribute='entity_id')
            |list
            |join('\n ') }}

(have to get in deeper)

changed that to

{% set x = states('input_select.hue_group') %}
{{ expand(integration_entities('Philips Hue 1'))
            |selectattr('attributes.is_dynamic','eq', true)
            |selectattr('attributes.group_name','eq', x)
            |map(attribute='entity_id')
            |list }}

and it shows the list of dynamic scenes, but that would be identical to expand(states.scene) which is a bit more efficient, since it disregards all lights :wink:

in fact we dont even need the expand(states.scene) there and can suffice with states.scene| which is what I use in the package above

Yeah, there are several roads that lead to Rome. I love the dynamic scenes, so for me there’s no need to differentiate, but it’s nice to know how :wink:
I don’t use auto entities card - the loop is necessary for it?

yes, because when using the filter template: one can not set options, other than in that template. So, I needed to create the list of names (I didnt want the group name to be listed) and made that somewhat more complex template in for loop namespace, resulting in:

and not show:


as is still in the input_select for the scenes…

So I am confused. I like that I can activate scenes in HA, but none of my scenes appear to activate as dynamic (meaning the color DOES NOT change).

What I would like to do is learn how to activate a scene so that it is dynamic from Node Red?

Is there any attribute I should look out for?

sorry, not using NodeRed, so I wouldn’t know how to help out

Ok. How do you make a service call to activate a dynamic scene in HA?

hue.activate_scene and use the dynamic and speed options.

1 Like

templates needs a small adjustment nowadays:

          {% set hue_group = states('input_select.hue_group') %}
          {{states.scene
            |selectattr('attributes.group_name','defined')
            |selectattr('attributes.group_name','eq',hue_group)
            |selectattr('attributes.is_dynamic','eq',true)
            |map(attribute='name')|list}}

in dev templates we otherwise get:

although the Frontend still works as expected