Help with YAML anchor and input_select options

Hi everyone, hopefully someone can help me out and shed some light as to why this isn’t working.

I’m just starting to dive into reusing code, so looking at using YAML anchors. I’m currently working on a group of input_select boxes that all share the same list of options, but there is where the wheels are falling off for me.

Here is my code:

input_select:
  - theme_switcher:
    name: Change Theme
    options: &theme-list
      - Backend-selected
      - default
      - PmxMonolight
      - PmxMononight
      - clear-dark
      - default_v2
      - midnight
      - slate
    initial: 'default'
    icon: 'mdi:palette'

  - theme_daytime:
      name: Daytime Theme
      options: 
        *theme-list
      icon: 'mdi:white-balance-sunny'

This seems to validate with every online YAML validation tool, except home assistant keeps failing.

Does anyone have any ideas?

Pretty sure the anchors are only valid in lovelace YAML. Not in configuration.

I’m not following…

I’ve searched so much on this and exhausted everything I can find on this forum and have formatted the same as people who have this working, but for some reason I can’t get it to work…

This is my latest attempt…

homeassistant:
  customize:
    package.node_anchors:
      theme-list: &theme-list
        name: Themes
        options: 
          - Backend-selected
          - default
          - PmxMonolight
          - PmxMononight
          - clear-dark
          - default_v2
          - midnight
          - slate
        initial: 'default'
        icon: 'mdi:palette'

input_select:
  - theme_switcher:
    <<: *theme-list
    name: Change Theme
    icon: 'mdi:palette'

  - theme_daytime:
    <<: *theme-list
    name: Daytime Theme
    icon: 'mdi:white-balance-sunny'

I have tried by defining it at the top in the customize section, I’ve also tried defining in the first use, as per my last example in the first post.

As @jocnnor said, this is only for lovelace (frontend). You can’t use this to define entities as far as I know.

Hmm, well that’s annoying. Hopefully it’s not the case.

May I ask why you use multiple input selects for themes?

I was wanting to have a bunch of selectors that all have the same options. Then I could choose the current theme, just like the normal way, and then 2 different selectors to pick daytime and nighttime themes, that an automation would then use, rather then having to edit the automation each time I want to use something different.

Since they would all share the same list of options, I thought I’d use a YAML anchor and have one list instead of 3. Make it easier to update when I add/remove themes.

Ok, I understand, valid point.

You can do it but you can’t use them in !includes or packages. This should work. They also can be declared in place but it gets tricky that way.

The reason I don’t think your stuff was working was spacing and use of -.

homeassistant:
  customize:
    package.node_anchors:
      theme_list: &theme_list
        name: Themes
        options: 
          - Backend-selected
          - default
          - PmxMonolight
          - PmxMononight
          - clear-dark
          - default_v2
          - midnight
          - slate
        initial: 'default'
        icon: 'mdi:palette'

input_select:
  theme_switcher:
    <<: *theme_list
    name: Change Theme
    icon: 'mdi:palette'

  theme_daytime:
    <<: *theme_list
    name: Daytime Theme
    icon: 'mdi:white-balance-sunny'
1 Like

Thanks. I’ll give it a try tomorrow, but, I have this in a files in my packages folder. I’ll try it in configuration.yaml file.

If you use packages, anchors will only work in the current file that they are declared in. This is inside or outside home assistant. Just the nature of yaml.

It’s all in the one package file, but didn’t seem to work. Just kept getting an error about a scalar, or couldn’t find a dict.

well yah, because your input_selects were incorrectly configured. It’s an ordered dict, not a list of dicts. Check out the differences between my post and your last attempt.

Success!! Yeah, it was that -. After using HA since 0.25, you’d think I’d have learnt about what needs to have a - and what doesn’t.

Thanks for your help @petro. This is working just as I expected!!

1 Like