Select/activate Hue scenes in new v2 Api setup

For latest version of package jump to Select/activate Hue scenes in new v2 Api setup - #27 by Mariusthvdb

---------------------

Anytime soon now we will see the light (…) of the new v2 Hue API @MarcelVeldt has been/is working miracles on, and I have been happy to test the last couple days.

Some amazing goodies are in store (for me personally the switches for the motion sensors that are now in core and no longer need complex rest/command_line sensors) and core active scenes for all our hue_groups. Also we will be able to differentiate between Hue groups and zones. all very cool stuff. Be sure to check it all out when it arrives!

Since I posted Hue scenes mimicked in Ha (and on Buttons, was Tiles) some very long time ago, and the set_hue_scene will also go, I decided to figure out a new way of doing all of that in the frontend. As much as possible in the frontend, with as little fiddling in the back-end as possible.

Think I made a config that is ready for sharing so here goes:

First, I create an input_select.hue_group_v2, in an automation at startup, which is auto-populated with the available scene for that specific selected group.

automation:

  - alias: Auto populate Hue group v2 input select
    id: Auto populate Hue group v2 input select
    trigger:
      platform: event
      event_type: delayed_homeassistant_start # a specially made event, to first give HA some time 
    action:
      service: input_select.set_options
      target:
        entity_id: input_select.hue_group_v2
      data:
        options: >
          {{expand(device_entities(device_id('Philips Hue 1')))
            |selectattr('attributes.is_hue_group','eq',true)
            |map(attribute='name')|list}}

#          {{expand(device_entities('yourbridgeidgoeshere'))
#            |selectattr('attributes.is_hue_group','eq',true)
#            |map(attribute='name')|list}}


input_select:

  hue_group_v2:
    name: Select Hue group
    options:
      - select

with that, the input_select is created, and can be used in the frontend:

type: entities
title: Set Inside 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;
    }
entities:
  - input_select.hue_group_v2
  - type: custom:fold-entity-row
    head:
     type: section
     label: Available Hue scenes
    padding: 0
    entities:
      - type: custom:auto-entities
        card:
          type: entities
          card_mod:
            style: |
              ha-card {
                box-shadow: none;
                margin: 0px -16px 0px -16px;
              }
        filter:
          template: |
            {% set select = states('input_select.hue_group_v2')|lower|replace(' ','_') %}
            {%- for s in states.scene
              if select == s.object_id.split('_')[:-1]|join('_') %}
              {{s.entity_id}}
            {%- endfor %}
  #      options:
  #        type: custom:template-entity-row
  #        name: >
  #          {{state_attr(config.entity,'name')}}
  - type: custom:template-entity-row
    entity: >
      light.{{states('input_select.hue_group_v2')|lower|replace(' ','_')}}
    secondary: >
      {% set object = states('input_select.hue_group_v2')|lower|replace(' ','_')%}
      {% 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)}}
    toggle: true
#  - type: divider
  - entity: automation.auto_populate_hue_group_v2_input_select
    secondary_info: last-triggered

result:

select group:

check available scenes and activate:

added a toggle to easily turn off gain:

Of course, none of this, and I can’t stress that enough, none of this, would have been possible without the amazing custom cards @ThomasLoven wrote, so next to Marcel, a huge thank you for Thomas is in order!

Hope this helps anyone looking for this functionality. Please see if you can build on this.
cheers.

2 Likes

though above is working very nicely, I do see an error during startup:

WARNING (MainThread) [homeassistant.components.input_select] Current option: select no longer valid (possible options: Alarm, Attic, Buffet, Corridor, Dining table, Dorm,  etc etc)

this is a bit of a catch 22 now, because when I take out the

    options:
      - select

in the input_select config, it complains options can not be empty, and if I add it, but override it with the automation to the actual groups I need, I get this…

catch 22 so much, the mere reload wont release the instance anymore and the circle keeps spinning…

What to do? Dont believe we can use an intermediary entity with that same template, because that would be a string, and not a list.

@123 forgive the tag, but since you already ‘liked’ I d hope you could have a look with me here too?

No good deed goes unpunished … :thinking:

I think I need to be using the beta to test what you created here … and I wasn’t planning to install the beta. Do you mind waiting until the release of the December version?

ofc, though I dont think it really is about the actual template here. this Using templates for input_select.set_options (populating options within an input selector) - #57 by 123 would probably do the same?

the point is we need to set some option in the input_select config, but then that gets overridden by the template a bit later, making the config checker complain?

I’ll have to take a look at it later (need to step away for awhile).

sure, glad if you would, thanks

I can’t replicate the warning message you received.

Tested with 2021.11.1.

input_select:
  stub:
    name: Stub
    options:
    - 'stub'

This automation successfully sets options for input_select.stub on startup without generating a warning message.

## Test setting an input_select's options on startup.
- alias: Set Stub Options
  id: set_stub_options
  trigger:
  - platform: homeassistant
    event: start
  action:
  - service: input_select.set_options
    target:
      entity_id: input_select.stub
    data:
      options: "{{ expand('group.battery_level') | map(attribute='name') | list }}"


I’m not familiar with this event type: delayed_homeassistant_start.

In my automation, I replaced:

  - platform: homeassistant
    event: start

with:

  - platform: event
    event_type: delayed_homeassistant_start

and the automation failed to run after a restart.

the event is a custom event:

  - alias: Run at startup
    id: Run at startup
    trigger:
      platform: homeassistant
      event: start
    action:
      - service: script.notify_startup
      - delay:
          seconds: >
            {{states('input_number.ha_delayed_startup')|int}}
      - event: delayed_homeassistant_start

after which you can do:

  - alias: Run at delayed startup
    id: Run at delayed startup
    trigger:
      platform: event
      event_type: delayed_homeassistant_start
    action:
      - service: script.notify_delayed_startup
      - service: input_boolean.turn_off
        entity_id: input_boolean.just_started
      - service: script.run_after_delayed_startup

for the options, I added a bit of a hack:

input_select:

  hue_group_v2:
    name: Select Hue group
    options:
      - Alarm

Alarm being one of my Hue groups, so that will always be there and wont cause any startup template issues.

Amazing you don see the warning, even though the ‘stub’ isnt in you options list… Now I need to take a step back.

thanks a bunch for being so thorough, truly appreciated!

did take out the relative_time out of the secondary: template:

   secondary: >
      {% set object = states('input_select.hue_group_v2')|lower|replace(' ','_')%}
      {% 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)}}

because that too gives startup warning I can not mitigate. And there’s no way I can think of a default value to add.

I left this experiment with input_select on my test system. While restarting the system for other reasons, this appeared:

So it can happen but why I don’t know.

apparently thats what happens when the Template is evaluated. you probably wont see that if you use the Family Battery Level as option in the input_select config.

that’s better:

homeassistant:

  customize_glob:

    scene.*concentreren:
      entity_picture: /local/hue_scenes/concentreren.png

    scene.*arctische_dageraad:
      entity_picture: /local/hue_scenes/arctische_dageraad.png

    scene.*energie:
      entity_picture: /local/hue_scenes/energie.png

    scene.*gedimd:
      entity_picture: /local/hue_scenes/gedimd.png

    scene.*helder:
      entity_picture: /local/hue_scenes/helder.png

    scene.*herfstgoud:
      entity_picture: /local/hue_scenes/herfstgoud.png

    scene.*lentebloesem:
      entity_picture: /local/hue_scenes/lentebloesem.png

    scene.*lezen:
      entity_picture: /local/hue_scenes/lezen.png

    scene.*nachtlampje:
      entity_picture: /local/hue_scenes/nachtlampje.png

    scene.*ontspannen:
      entity_picture: /local/hue_scenes/ontspannen.png

    scene.*savanne_zonsondergang:
      entity_picture: /local/hue_scenes/savanne_zonsondergang.png

    scene.*tropische_schemering:
      entity_picture: /local/hue_scenes/tropische_schemering.png

gedimd
herfstgoud
lezen
arctic
concentrate
energize
clear
spring
nightlight


savanna
tropical

with @petro 's help:

for some amazing templating power in the auto-entities card:

        filter:
          template: |
            {% set select = states('input_select.hue_group_v2')|lower|replace(' ','_') %}
            {% set ns = namespace(ret=[]) %}
            {%- for s in states.scene
              if select == s.object_id.split('_')[:-1]|join('_') or
                 select == s.object_id.split('_')[:-2]|join('_') %}
              {% set ns.ret = ns.ret + [{"entity":s.entity_id,
                  "name": s.object_id.split(select +'_')[1]|capitalize|replace('_',' ') }] %}
            {%- endfor %}
            {{ ns.ret }}

taken me some time, but finally made the template not throw startup warnings:

    secondary: >
      {% set x = ['unknown','unavailable'] %}
      {% if states('input_select.hue_group_v2') not in x %}
      {% set object = states('input_select.hue_group_v2')|lower|replace(' ','_')%}
      {% 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 %}

be sure to refresh the frontend with the new template a couple of times, because if you dont, the warnings/errors will persist over restarts.

I’m probably missing something incredibly obvious … but how do I get access to the new V2 API stuff? I assume it’s in some form of beta testing?

I was literally about to start trying to figure out how to write a bunch of code to do things that it sounds like the new the integration will give me for free, which would obviously be amazing :slight_smile:.

It’s in the 2021.12 beta release. At this point, you may as well wait for 2021.12.0 which is due to be released today (December 11).

So is there some more documentation about this new way of using scenes in 2021.12? The Hue integration page does not contain much extra info. I have a similar sort of setup where I have a input_select with all the scene names and a Node RED flow that loops through it when holding down a switch. I’m wondering how to change this now that we have individual entities for all scenes.

not sure what more than the above you would need? simply click one of the scenes and it activates. You can also call them directly in script/automations.

I have a strange effect, when I activate a Hue scene an automation or the developer tools section (Home Assistant 2021.12.3).

If activated like this:

service: scene.turn_on
target:
  entity_id: scene.livingroom_relax

… the scene is properly activated, but after a few minutes it starts cycling through all the colors in the scene. That is like pressing the (newly introduced?) “play” symbol on the scene in the Hue App.

So how can I activate a Hue scene without it starting to “play”?

Just tested in development tools with the same configuration and everything works fine, stable without color transitions.Test 30 minutes

I just noticed that some scene’s in the Hue App don’t have the “play” button once you activate them (most of the standard/default scenes, but strangely enough not all of them).
So these will not cycle through the colors, when activated from Home Assistant.

You may also not notice the cycling if the colors of the scene are similar or the dynamic cycle speed is set to a low value.

Could you try one that does have the “play” button and has many colors and a high cycling speed set?
(you can set this by changing the scene details, after pressing the “pencil” button)