Documentation for types of selectattr filtering?

Hello good people,

I am new to HA and I want to have a select list that gets updated dynamically and it seems the only way to do that is with a template. However, I am struggling a bit with some of the jinja syntax and I am unsure where to find the documentation to help clear it up.

Essentially, I am trying to create a template that creates a select list of all the Phillips Hue scenes that get automatically created in a given room so that I can then use something like a bubble card select list to choose them.

As an example, one of the rooms is Sunroom and the Hue integration creates scenes like scene.sunroom_artic_aurora (name: Arctic aurora) or scene.sunroom_bright (name: Bright)

Because Hue uses the same name like “Bright” for scenes in multiple rooms, I needed to be able to limit the list to a given room but not show the name of the room in the list as it is redundant. I came up with this for listing the options and it works how I want it but was put together from other examples people posted:

options: {{ states.scene | selectattr('entity_id','search','sunroom')| map(attribute='name') | list | replace("Sunroom ","") }}

Now, the trouble comes in when I am trying to set up the target entity ID. My main problem is that I don’t understand what the different filters specifically do for selectattr.

I’ve seen examples that say:

selectattr('entity_id','search',option)

or others that say:

selectattr('name', 'eq' , option)

and others but where is the documentation that says what “search” or “eq” actually does? I looked on the jinja doc page and the templating page and saw some references to regex filtering but nothing that explicitly gave the list of those options, their syntax, and what exactly they do. Am I blind?

I thought maybe I had cracked it with the following:

entity_id: "{{ states.scene | selectattr('entity_id','search','sunroom') | selectattr('name', 'eq' , option) | map(attribute='entity_id') | list | first}}"

but it throws the following error
“Failed to perform the action select/select_option. not a valid value for dictionary value @ data[‘entity_id’]”
and I can’t find any way in the template editor in dev tools to test me selecting something to see what the value of “option” is or the then-resulting entity_id would be.

Beyond that, if I need to concatenate the name of the room with the name attribute to come up with an entity ID, can I even do that?

Any help anyone could provide would be most appreciated.

1 Like

search → the searched for word can be anywhere in the object you are searching.

eq → equals. i.e. it must match the object value exactly. You can also use ==

If option is the string you are looking for then it must be quoted here otherwise it will be interpreted as a variable:

| selectattr('name', 'eq' , 'option')

It’s in Jinja2’s documentation.

selectattr is one of many Built-in Filters. It applies one of many Built-in Tests. eq is one of those Built-in Tests.

search is a test that is unique to Home Assistant (in other words, it’s not one of Jinja2’s Built-in Tests). search and the similar match can be used as tests or functions and are documented in Home Assistant’s Templating documentation (in the Regular Expressions section).

I recommend you review this chapter of Home Assistant’s documentation. It provides an introduction to Home Assistant’s implementation of Jinja2.

Templating

1 Like

In your options template you are modifying the entity names…

… so they will never match exactly in the selectattr()…

You cannot use eq as your test for string values unless the values are going to be exactly the same.

Thanks for the clarification on search and eq. With regard to your option comment, I was under the impression from the documentation here: Template - Home Assistant and from the examples I came across that that option IS a variable produced by the select template that contains whatever option was selected. Do I still need to quote in that case? If I quote it won’t it just be the string “option” in that case?

Thank you! So the answer is that I am partially blind because I was reading that exact page but the way that section is written made no sense to me without understanding the meaning of a test. That page could VERY MUCH use some examples in that section. Thank you.

Options is an attribute of the select / input_select entity type otherwise known as a domain (light is a domain, switch is a domain, input_select is a domain, select is a domain, etc…). This has nothing to do with the “select” function in a jinja template. They are just named the same. A select entity is a drop down list of options from which you can choose one, like this:

That is what the documentation you linked to is about. How to configure a template select entity.

If you wanted to access the options list of one select entity you would do it like this:

{{ state_attr('select.tv_source', 'options') }}

Search all your select entities with an options list that contained the word “sunroom” you would do something like this:

{{ states.select | selectattr('attributes.options','search','sunroom') }}

Thanks for the response. I realize that I am modifying the names but they look strange without modification because everything in the list appears with the room prefix as “sunroom bright”, “sunroom aurora”, “sunroom dimmed”, etc so my plan was to add the “sunroom” back into the name for the select_option filter. Now I just need to figure out how :rofl:

I was referring to the option variable as referenced here in the doc:


My understanding is that when creating a select entity you specify the options and then you specify what happens when one is selected with select_option and if you want to reference what was selected you would use the option variable like in this example I found:

template:
  - select:
    - name: Select Scene for Lounge
      unique_id: hue_selector
      state: ""
      select_option: 
        - service: hue.activate_scene
          data:
            transition: 2
          target:
            entity_id: "{{ states.scene | selectattr('name', 'eq' , option) | map(attribute='entity_id') | list | first}}"
      options: "{{states.scene | map(attribute='name') | list }}"

So in a case like that I would not quote the option variable, correct?

You are querying the scene domain:

That domain (entity type) does not have an options list attribute only input selects and select entities have that attribute. Scenes do not. So…

No there will be no option variable for a scene.

Yes it would be unquoted if you were accessing the input_select or select domain.

FYI here are the available attributes for a scene: Scenes - Home Assistant

Tom, I’m confused about the point you are trying to make…

OP is setting up a Template select and wants the template to select for the name of the scene being equal to the value of the variable option within the select_option action sequence.


If the Area name is in a static position you can concatenate in the selectattr():

template:
  - select:
      - name: Select Scene for Lounge
        unique_id: hue_selector
        state: ""
        options: "{{ states.scene | map(attribute='name') | select('match', 'Lounge') | list }}"
        select_option: 
          - service: hue.activate_scene
            data:
              transition: 2
            target:
              entity_id: |
                {{ states.scene | selectattr('name', 'eq' , 'Lounge '~option) 
                | map(attribute='entity_id') | first }}

Cheers @Didgeridrew ! This worked like a charm. For anyone who is curious this is what ended up working for me.

template:
  - select:
    - name: Sunroom Hue Scenes
      unique_id: sunroom_hue_scenes
      state: ""
      options: {{ states.scene | selectattr('entity_id','search','sunroom')| map(attribute='name') | list | replace("Sunroom ","") }}
      select_option: 
        - service: hue.activate_scene
          data:
            transition: 2
          target:
            entity_id:  |
	    {{ 
	       states.scene
	      | selectattr('name', 'eq' , 'Sunroom '~option) 
	      | map(attribute='entity_id') 
	      | first
        }}

I am curious though, is there a way to set the “state” of the select entity to be the selected option so that can be displayed on the button or in some other place?

Yes, provide a template for the state configuration variable. Two ways to define the state template are covered in the following cookbook article:

Community Cookbook - Template Select - Area-based Scene Selector

1 Like

Yeah I missed the big picture and was concentrating on the scene domain not having an option variable.

entity_id: "{{ states.scene | selectattr('name', 'eq' , option)

The component itself does though.

@Didgeridrew Forgive me for being new to YAML and HA and also being a bit dense but I am having a difficult time discerning how to utilize the examples you linked to. I get that you can input a template into the “state” of the select entity you are creating. The “Helper-Free” example looks like it is grabbing all the entities for the area and then sorting them based on state but I am not sure what “reverse=true” does and then it is displaying the first one in the list.

The hue-generated scenes seem to have a default state of “unknown” and then when the hue.activate_scene action runs it looks like it sets the time of the run as the state:

So I am guessing that maybe I could look for the most recent time run but I am not sure how to find/sort that and if the ones still listed as “unknown” would mess up the sort. Any thoughts?

Although now that I think about it, doing it that way would always show the state as the most recent one that was run even if that scene hadn’t recently been running. I am not sure what would be more accurate. :confused:

The reverse arg in the sort() filter sorts the date strings so the newest is first; without reverse they would be sorted oldest → newest. There isn’t a last filter, so reversing the sort allows us to get the desired value with first.

You can remove the “unknowns” etc by adding a reject filter after the map filter:

| reject('in', ['unknown','unavailable'])

Yep, it will always return the most recently run scene. Since scene’s are stateless in HA neither is more accurate than the other… it just depends on how you want your select to behave.

If use a Helper it is possible to use automations or more complex templating to reset the state after a given amount of time or on restart, etc.

That makes sense but is that working in the example because the “state” of a scene is always just a date of the last time it was run unless it is unknown? Also, are you using the preformatted tag to highlight the terms in these messages?

Yes

Yes… you can test it for yourself in the Template tool by mapping to the state instead of the name:

… or remove the map filter completely and you can see the whole objects.

1 Like