Automation adding the word Option and rendering action inoperable

Hi,

My first time posting here, been struggling for literal hours with this one.

I’m trying to set up a template select with dynamic name changes. Got all of that to work, only problem is getting the state to actually change. It seems perpetually stuck between either unknown or unavailable.

The code in my automation to test the state change


action:
  - service: select.select_option
    data:
      entity_id: select.yamaha_a8a_scene_selector_template
      option: >
        {% set options = state_attr('select.yamaha_a8a_scene_selector_template', 'options') %}
        {% for option in options %}
          {% if '7' in option %}
            {{ option }}
            {% break %}
          {% endif %}
        {% endfor %}

Produces the error: Error running action

Option 7 - PC Viewing - 0 7 is not valid for entity select.yamaha_a8a_scene_selector_template, valid options are: 1 - Movie Viewing - 0 1 , 2 - Full Stereo Listening - 0 2 , 3 - NET Radio Listening - 0 3 , 4 - Radio Listening - 0 4 , 5 - Analog Audio Listening - 0 5 , 6 - Game Playing - 0 6 , 7 - PC Viewing - 0 7 , 8 - AV Viewing - 0 8

However, pasting the exact same code in the developer tools template editor produces the desired result:

7 - PC Viewing - 0 7

Essentially, it seems like the code for some reason is being interpreted as having the word option appended to the dynamic name change, even though to my knowledge I don’t have any such word coming out of the template select, or the template sensor from which the template select derives its options and names for said options. But developer tools clearly shows the code being interpreted correctly.

The template select was created in the GUI, so I don’t know how to copy the code for that over to the forum.

Logs show the same error when trying to execute the automation, but nothing beyond what’s already shown.

Am I doing something wrong, did I miss something, or did I discover a bug?

Thank you very much in advance, really appreciate it!

Ah, I don’t know why it’s losing the formatting, please bear with me…

You may have to paste it into a text editor first to get the line feeds.


action:
  - service: select.select_option
    data:
      entity_id: select.yamaha_a8a_scene_selector_template
      option: >
        {% set options = state_attr('select.yamaha_a8a_scene_selector_template', 'options') %}
        {% for option in options %}
          {% if '7' in option %}
            {{ option }}
            {% break %}
          {% endif %}
        {% endfor %}

Finally!!! Sorry about that

Thanks, yes I was having trouble copying it from the editor into the browser with a bunch of 20% being added, figured it out now! I’ve editing the original post with the proper formatting.

You should share the state template for your entity… that is how the value for the state is derived.

Oh ok, maybe I’m missing something… is this different from the select template and the sensor template? I set up the template select entity using the gui, so I don’t know how to copy that here as there’s no option I can see to view the yaml code like there is for automations.

Here’s what I could copy from the template select, in case that’s helpful.

Template select
————————————
State Template (text field):

{{  states('select.yamaha_a8a_scene_selector_template') }}

Available Options (text field):

{% set options = states('sensor.yamaha_a8a_scene_options') %}
          {% if options %}
            {{ options.split(', ') }}
          {% else %}
            {% set default_options = ['Scene 1', 'Scene 2', 'Scene 3', 'Scene 4', 'Scene 5', 'Scene 6', 'Scene 7', 'Scene 8'] %}
            {{ default_options }}
          {% endif %}

Actions on select (in YAML format, setup through GUI):

choose:
  - conditions:
      - condition: template
        value_template: "{{ '1' in states('select.yamaha_a8a_scene_selector_template') }}"
    sequence:
      - action: rest_command.scene_change_1
        data: {}
        metadata: {}
  - conditions:
      - condition: template
        value_template: "{{ '2' in states('select.yamaha_a8a_scene_selector_template') }}"
    sequence:
      - action: rest_command.scene_change_2
        data: {}
        metadata: {}
  - conditions:
      - condition: template
        value_template: "{{ '3' in states('select.yamaha_a8a_scene_selector_template') }}"
    sequence:
      - action: rest_command.scene_change_3
        data: {}
        metadata: {}
  - conditions:
      - condition: template
        value_template: "{{ '4' in states('select.yamaha_a8a_scene_selector_template') }}"
    sequence:
      - action: rest_command.scene_change_4
        data: {}
        metadata: {}
  - conditions:
      - condition: template
        value_template: "{{ '5' in states('select.yamaha_a8a_scene_selector_template') }}"
    sequence:
      - action: rest_command.scene_change_5
        data: {}
        metadata: {}
  - conditions:
      - condition: template
        value_template: "{{ '6' in states('select.yamaha_a8a_scene_selector_template') }}"
    sequence:
      - action: rest_command.scene_change_6
        data: {}
        metadata: {}
  - conditions:
      - condition: template
        value_template: "{{ '7' in states('select.yamaha_a8a_scene_selector_template') }}"
    sequence:
      - action: rest_command.scene_change_7
        data: {}
        metadata: {}
  - conditions:
      - condition: template
        value_template: "{{ '8' in states('select.yamaha_a8a_scene_selector_template') }}"
    sequence:
      - action: rest_command.scene_change_8
        data: {}
        metadata: {}

Maybe the line break at the end is messing with your template? Try >- for the multiline text block.

Another thing: Are the select options ending in spaces? The list of options seems weird with a space before the comma. Maybe if you quote the output?

No, it is part of the Template Select configuration which you have now provided:

From the above, it seems that you are deriving the state of your Template Select from the state of a separate select entity… is that correct?

If that is the case, you need to add an action in the “Actions on select”/select_option where you set the value of that entity.

Also, you should change the conditions in the Choose action to reference the option variable, not the state of the other select entity.

{{ '1' in option }}

If you haven’t already, there is an article on Template Selects in the Community Cookbook that you may find helpful.

If I understand right, you’re trying to set up a Template Select.

Can you post the select_option handler? It’s required for a Template Select, and so far I’ve only seen the state and options handlers, I think.

Also, is the actual Template Select you’re trying to set up called select.yamaha_a8a_scene_selector_template? If so, something seems quite amiss here (or I’m not understanding something you’re doing) since you can’t define the state of the Template Select using a self-referencing states call. If that’s your template, how are you defining the options attribute?

It’s shown in Post #10 under “Actions on select (in YAML format, setup through GUI):”

Oh, thanks, I missed that!

Could we see the output of the following code in the Template Editor:

{% set options = states('sensor.yamaha_a8a_scene_options') %}
          {% if options %}
            {{ options.split(', ') }}
          {% else %}
            {% set default_options = ['Scene 1', 'Scene 2', 'Scene 3', 'Scene 4', 'Scene 5', 'Scene 6', 'Scene 7', 'Scene 8'] %}
            {{ default_options }}
          {% endif %}

There are a couple of things about that snippet that seem suspect to me but can’t check them out without seeing that.

This is the output for the code from Developer Tools Template


[
  "1 - Movie Viewing - 0 1\n\n  ",
  "2 - Full Stereo Listening - 0 2\n\n  ",
  "3 - NET Radio Listening - 0 3\n\n  ",
  "4 - Radio Listening - 0 4\n\n  ",
  "5 - Analog Audio Listening - 0 5\n\n  ",
  "6 - Game Playing - 0 6\n\n  ",
  "7 - PC Viewing - 0 7\n\n  ",
  "8 - AV Viewing - 0 8"
]

Not sure where the n/n is coming from, but here’s the code I was using for the template sensor, from which the template select derives its list of options:


sensor:
  #Yamaha A8A Extract Scene Names
  - platform: template
    sensors:
      yamaha_a8a_scene_options:
        value_template: >
          {% set scene_list = state_attr('sensor.yamaha_a8a_scene_list', 'scene_list') %}
          {% for scene in scene_list %}
            {% if loop.first %}{{ scene.num }} - {{ scene.text }} - {{ scene.tag }} {{ scene.default_tag }}{% else %}, {{ scene.num }} - {{ scene.text }} - {{ scene.tag }} {{ scene.default_tag }}{% endif %}
          {% endfor %}

And the output for that code:


sensor:
  #Yamaha A8A Extract Scene Names
  - platform: template
    sensors:
      yamaha_a8a_scene_options:
        value_template: >
          
          
            1 - Movie Viewing - 0 1
          
            , 2 - Full Stereo Listening - 0 2
          
            , 3 - NET Radio Listening - 0 3
          
            , 4 - Radio Listening - 0 4
          
            , 5 - Analog Audio Listening - 0 5
          
            , 6 - Game Playing - 0 6
          
            , 7 - PC Viewing - 0 7
          
            , 8 - AV Viewing - 0 8

Basically, the Names of the options are subject to change, so I’m trying to link it based on the numbers included, and want the thing to work whether the number is at the beginning of the name, at the end, or in the middle or before another digit.

The \n characters are newlines, and they are probably your problem.

Try this in your available options:

{% set options = states('sensor.yamaha_a8a_scene_options') %}
          {% if options %}
            {{ options.split(', ')|map('trim')|list }}
          {% else %}
            {% set default_options = ['Scene 1', 'Scene 2', 'Scene 3', 'Scene 4', 'Scene 5', 'Scene 6', 'Scene 7', 'Scene 8'] %}
            {{ default_options }}
          {% endif %}

The template state is referencing itself; I thought I’d want it to change state as soon as an option is. selected.

The line you posted,


{{ '1' in option }}

Is that supposed to replace the entity in this line?

option: >
        {% set options = state_attr('select.yamaha_a8a_scene_selector_template',

No, but let’s hold off on that for a minute…

OK that seems to take care of the extra (paragraph?) lines, thank you!

Don’t know if the spaces after the last digit could a problem though:


[
  "1 - Movie Viewing - 0 1",
  "2 - Full Stereo Listening - 0 2",
  "3 - NET Radio Listening - 0 3",
  "4 - Radio Listening - 0 4",
  "5 - Analog Audio Listening - 0 5",
  "6 - Game Playing - 0 6",
  "7 - PC Viewing - 0 7",
  "8 - AV Viewing - 0 8"
]

Ok, I’ll hold off on that then…

I don’t see any trailing spaces in that list. Does the change get rid of the error?