Using an input select to apply scenes - How to?

Hi Everybody

I have a question for which I could not find a hint (please direct me if this was answered already).

I would like to use an input_select to apply scenes (I cannot use HUE scenes for that).
More specifically I’d like:

  • The input_select to be filled automatically with scenes (e.g., scene name starting with scene.living…)
  • Once I select a scene I’d like it to be applied automatically

Any idea? Or any link to existing documentation/exampes?
Thank you
Stefano

While what you have described is kind-of possible with an input select and an automation or two… the better option is a Template Select.

Community Cookbook - Template Select Entities

template:
  - select:
      - name: "Living Scenes"
        state: >
          {{ 
            states.scene
            | selectattr('object_id', 'match', 'living')
            | sort(attribute='state', reverse=true)
            | map(attribute='name')
            | list
            | first
          }}
        options: >
          {{
            states.scene
            | selectattr('object_id', 'match', 'living')
            | map(attribute='name')
            | list
            | sort
          }}
        select_option:
          - service: scene.turn_on
            target:
              entity_id: >
                {{
                  states.scene
                    | selectattr('name', 'eq', option)
                    | map(attribute='entity_id')
                    | first
                }}
1 Like

GREAT!!!
Thanks a lot

Hello

I tried the code you shared and it works, but there are a couple of points I’d like to clarify, if you can.

First of all, the only change I made to your code is the Area name, which in my case is “Studio” so I changed the 2 lines putting ‘studio’ instead of ‘living’.
I can see the scenes in an entity card I created

image

Strangely, there is already one scene selected, while it is not active.
When I click I get the “properties” popup

Here I can select from the populated list, although one scene is missing. More specifically, all the scenes named scene.studio* appear while another one called scene.000_studio_off does not. I understand the listbox is populated with scenes in a given area, isn’t it?

Selecting a scene (e.g., Studio Futuristic Hal) the scene is applied

But the entity card still shows the initial value (the scene that nobody selected)

Am I doing anything wrong?

In HA there is no concept of a scene being active the way you describe. A scene is active when it is in the process of applying itself, which takes mere seconds. Turning it on runs the actions in the scene, when all states are set then the scene turns off. It does not know if those devices are still in the state that the scene describes a moment later.

You have a selector to select the name of a scene, and an action that activates the scene if you pick a new name. The selector needs at least one of the values to be the current one. If there is no empty choice at the start, it just picks one (probably the first).

The problem with the current approach is you cannot pick the same scene twice in a row. So if you set a scene, someone or something else operates on the devices, the select is still pointing to the same scene. You have to pick another scene before you can pick the original one again.

Scenes should be more like a button, So to have a list of scenes you can activate, you need a list of buttons. This is more something like an auto entities card could do.

If you want to keep the select, you might want to add an empty line to the top of the list, and switch the select to the empty line once the scene was activated. The. you can always pick every scene in the list to activate it.

1 Like

To clarify a couple points in addition to Edwin’s answer…

The configuration I posted was to meet the criteria you gave i.e. “scene name starting with scene.living…”. To do that I used the test match which is specific to the start of the ID string. If you want it to find all the scenes that have “studio” anywhere in the ID string, replace match with search.

It is also possible to use the scene entities’ Areas directly instead of basing if off the entity ID, but these templates do not actually check the Area.

Edwin has already covered the concept of “active” scenes…

What is displayed is the entity’s state which is only kind of the selection. The select entity’s state is determined by the template, which will always return the scene that was most recently activated.

As Edwin stated it is possible to add another option like a blank or “No Scene Selected”, etc. And it is also possible to have the state reset to that value if no scene has been activated with a given amount of time.

template:
  - select:
      - name: "Studio Scenes"
        state: >
          {% set reset_minutes = 5 %}
          {% set recent = states.scene | selectattr('object_id', 'search', 'studio')
            | sort(attribute='state', reverse=true) | list | first %}
          {{ recent.name if recent.state|as_datetime > 
          now() - timedelta(minutes=reset_minutes) else 'No Scene Selected' }}
        options: >
          {{ ['No Scene Selected'] +
            states.scene | selectattr('object_id', 'search', 'studio')
            | map(attribute='name') | list | sort }}
        select_option:
          - service: scene.turn_on
            target:
              entity_id: >
                {{ states.scene | selectattr('name', 'eq', option)
                | map(attribute='entity_id') | first }}

Clear.

Big THANK YOU

Thank you for the clear explanation