Hide input select option if empty

I have this automation which set options for my input_select


- alias: input_select_workaround

  trigger:

  - platform: state
    entity_id: 
    - sensor.octoprint_file_1_template
    - sensor.octoprint_file_2_template
    - sensor.octoprint_file_3_template
    - sensor.octoprint_file_4_template
    - sensor.octoprint_file_5_template
    - sensor.octoprint_file_6_template
    - sensor.octoprint_file_7_template
    - sensor.octoprint_file_8_template
    - sensor.octoprint_file_9_template
    - sensor.octoprint_file_10_template        

  action:
  - service: input_select.set_options
    data_template:
      entity_id: input_select.file_to_print
      options:
        - Select
        - '{{ states("sensor.octoprint_file_1_template") }}'
        - '{{ states("sensor.octoprint_file_2_template") }}'
        - '{{ states("sensor.octoprint_file_3_template") }}'
        - '{{ states("sensor.octoprint_file_4_template") }}'
        - '{{ states("sensor.octoprint_file_5_template") }}'
        - '{{ states("sensor.octoprint_file_6_template") }}'
        - '{{ states("sensor.octoprint_file_7_template") }}'
        - '{{ states("sensor.octoprint_file_8_template") }}'
        - '{{ states("sensor.octoprint_file_9_template") }}'
        - '{{ states("sensor.octoprint_file_10_template") }}'

When a sensor is empty, an empty box appears as an option in my input select. Is it possible to hide this empty box so it will just show the sensors which are not empty?

The template looks like this:

      octoprint_file_1_template:
        friendly_name: "Octoprint file 1"
        value_template: >-
          {% if is_state("sensor.octoprint_file_1", "unknown") %}

          {% else %} 
            {{ states('sensor.octoprint_file_1') }}
          {% endif %}

The only way I know how to do that would be to call that set_options service from Node RED. HA only lets you template strings and not objects/arrays I don’t know any way to use data to determine the number of elements in that array. Maybe someone else has a trick I’m not aware of.

One thought I did have though, does the UI have to be a dropdown? I think you could make a functional version of that UI using the entity filter card if you’re interested. You could try something like this in your UI instead:

- type: entity-filter
  card:
    title: "Files to print"
    show_header_toggle: false
    tap_action:
      action: call-service
      service: "<Insert your print service here>"
      service_data: 
        <Insert your additional service data fields here>
  state_filter:
    - operator: "!="
      value: unknown
  entities:
    - sensor.octoprint_file_1
    - sensor.octoprint_file_2
    - sensor.octoprint_file_3
    - sensor.octoprint_file_4
    - sensor.octoprint_file_5
    - sensor.octoprint_file_6
    - sensor.octoprint_file_7
    - sensor.octoprint_file_8
    - sensor.octoprint_file_9
    - sensor.octoprint_file_10
  show_empty: false

You didn’t share what your print service actually is so I’m not sure what to call on click, I just left that part as fill in the blank.

If you happen to have the custom auto-entities card we can simplify this even further:

- type: 'custom:auto-entities'
  card:
    type: entities
    title: Files to print
    show_header_toggle: false
  filters:
    include:
    - entity_id: sensor.octoprint_file_*
      options:
        tap_action:
          action: call-service
          service: "<Insert your print service here>"
          service_data: 
            <Insert your additional service data fields here>
    exclude:
    - state: unknown
    - state: ''
  show_empty: false

You wouldn’t need the duplicate _template sensors either since you can just filter out things in state unknown with both of these cards.

Also if you want the card to always show up even if there’s nothing to print then change show_empty to true. And if you’re worried about accidentally printing something you can even add a confirmation dialog now.