Restore/save dynamic input_select options and state after HA restart

Hello, i have multiple scrips/automations that modifies serval input_select helpers, all work correctly until an HA restart when all the dynamic input_select revert to the static one.
I have searched all topics and info and did not find any solution to this, if someone has any idea will be greatly appreciated.

The closest and best idea that I can see is using pyscript to create a service call to save and restore the input_select after restart, unfortunately I have no knowledge of python, but I can follow some simple coding to a point. Best example is this old post
What i understand is that is creating a service call to run a python script that saved the input_select helper options and state to a .ini file and after restart repopulate the helper.
What I want is to use it with multiple entities (input_select helpers) so it needs to generate the ini file based on the name of the entity.

Here is the code that I coped from the other post:
For save

# Write file from input_select.

import sys

# File I/O is done in a called module - ensure the path includes it. Note that this only needs to be added
# to one file ... pyscript picks it up when loading the files.
if "/config/pyscript_modules" not in sys.path:
    sys.path.append("/config/pyscript_modules")

import file_ops


# This runs as a service, and is normally run from an automation.
@service
def save_input_select(entity=None, result=None):
    log.debug(f"got entity {entity}, result {result}")

    # Inputs are required. They are defined as optional so we can just log an error instead of encountering
    # an exception.
    if entity is None or result is None:
        log.error("entity and result are both required, exiting")

    # Domain must be input_select.
    elif not entity.startswith("input_select."):
        log.error("input entity must be domain input_select, exiting")

    # All seems well.
    else:
        # List of all movies
        movies = result['movies']
        all_movies = []
        for movie in movies:
            all_movies.append(movie['label'])
        all_movies.sort()
        log.debug(f"movie list {all_movies}")

        # Populate the input select.
#        input_select.set_options(entity_id=entity, options=all_movies)

        # Write the list to a file so we can restore the input select after a restart.
        task.executor(file_ops.write_movie_file, all_movies)

For restoring the helper

# Restore the given input select from the previously saved list.

import file_ops


# This runs as a service, and is normally run from an automation.
@service
def restore_input_select(entity=None):
    log.debug(f"got entity {entity}")

    # Input entity is required. It is defined as optional so we can just log an error instead of encountering
    # an exception.
    if entity is None:
        log.error("entity is required, exiting")

    # Domain must be input_select.
    elif not entity.startswith("input_select."):
        log.error("input entity must be domain input_select, exiting")

    # All seems well.
    else:

        # Read the backup file.
        try:
            all_movies = task.executor(file_ops.read_movie_file)
        except FileNotFoundError:
            log.error(f"saved movie file not found, exiting")
            sys.exit(f"file not found")
        except pickle.UnpicklingError:
            log.error(f"saved movie file not usable, exiting")
            sys.exit(f"file not usable")

        log.debug(f"movie list {all_movies}")

        # Populate the input select.
        input_select.set_options(entity_id=entity, options=all_movies)

Anyone can help me with modifications to the code to fit my needs?

Maybe I’m wrong and your use case is different from mine, but I faced the same problem when I wanted to dynamically display the Hue scenes of my lamps listed as attributes.
Since then I’ve been using Template Select in combination with input_text and have not had any problems, even after a restart.

Thanks for the info but it does not help me, in my automations I create a dynamic input_select by removing options one by one and is necessary to keep that after restart for the automation to continue to work correctly.
Example here:

service: input_select.set_options
data:
  options: >-
    ({% set ns = namespace(list=[]) %} {% for option in
    state_attr("input_select.type_of_meal_filtered", "options") if option !=
    states("input_text.type_of_meal_random") %} {% set ns.list =
    ns.list+[option] %} {%endfor%} {{ns.list}})
target:
  entity_id: input_select.type_of_meal_filtered
enabled: true

How large is the complete list? If it isn’t too long, you might be able to change the way your input_text works so that it hold all the “used” options. That would let you add a trigger for home assistant restart to run your input_select.set_options.

The lists can get pretty large, and they will grow with time.
The functionality of them all is to recommend an random meal every day, but do not repeat any category or meal until all of them where recommended. I use two lists for every category/meal type/salad, one “full” that is fixed and can be edited from the HA UI to add meals, one “filtered” where the meals remaining to be recommended are stored.
All is functional until restart when the “filtered” list get’s reset and then some of the meals can’t get recommended and randomly you can get consecutive previously recommended meals.

The UI is still a work in progress but the main functionality is here.

I think your best bet is to move your filtered lists to Trigger-based template sensor attributes which will survive restart.

Can you share the configuration of your food choices?

Hello, sure i can share the script but the issue with the restart is still not resolved. i had just made a workaround to not error out once is restarted but is not cycling thru all the food recommendations in this case.

for this to work you need to create the following helpers:

  • Entity: counter.days_to_special_meal
  • Entity: input_select.meal_beff_filtered
  • Entity: input_select.meal_beff_full
  • Entity: input_select.meal_chicken_filtered
  • Entity: input_select.meal_chicken_full
  • Entity: input_select.meal_eggs_filtered
  • Entity: input_select.meal_eggs_full
  • Entity: input_select.meal_fish_filtered
  • Entity: input_select.meal_fish_full
  • Entity: input_select.meal_poark_filtered
  • Entity: input_select.meal_pork_full
  • Entity: input_select.meal_salad_filtered
  • Entity: input_select.meal_salad_full
  • Entity: input_select.meal_special_filtered
  • Entity: input_select.meal_special_full
  • Entity: input_select.meal_vegan_filtered
  • Entity: input_select.meal_vegan_full
  • Entity: input_select.type_of_meal_filtered
  • Entity: input_select.type_of_meal_full
  • Entity: input_text.meal_random
  • Entity: input_text.meal_salad_random
  • Entity: input_text.type_of_meal_random
  • in the input.select “FULL” - you will add your prefered foods from each chategory
  • in the input.select “FILTERED” - you will add just one option “RESET”
  • the counter is set for 10 with pase of 1 - for 9 days to special meal

The Lovelace config:

type: vertical-stack
cards:
  - type: tile
    entity: input_text.type_of_meal_random
    name: Category of Meal
    color: red
    tap_action:
      action: none
    icon_tap_action:
      action: call-service
      service: script.choose_meal_type_repick
      target: {}
  - type: tile
    entity: input_text.meal_random
    name: 'Recommended Meal for Today '
    color: blue
    tap_action:
      action: none
    icon_tap_action:
      action: call-service
      service: script.choose_meal_repick
      target: {}
  - type: tile
    entity: input_text.meal_salad_random
    name: Recommended Salad for Today
    color: green
    tap_action:
      action: none
    icon_tap_action:
      action: call-service
      service: script.choose_salad_replick
      target: {}

There are additional scripts to repick meals if you want to randomly change the meal.

Here is the main script that I call once per day with an automation:

alias: choose_meal
sequence:
  - if:
      - condition: state
        entity_id: counter.days_to_special_meal
        state: "0"
    then:
      - service: input_text.set_value
        data:
          value: Special
        target:
          entity_id: input_text.type_of_meal_random
    else:
      - if:
          - condition: template
            value_template: >-
              {{ state_attr('input_select.type_of_meal_filtered', 'options')  |
              length ==1 }}
        then:
          - if:
              - condition: state
                entity_id: input_select.type_of_meal_filtered
                state: RESET
            then:
              - service: input_select.set_options
                data:
                  options: >-
                    ({{ state_attr('input_select.type_of_meal_full',
                    'options')}})
                target:
                  entity_id: input_select.type_of_meal_filtered
              - service: input_text.set_value
                data:
                  value: >-
                    {{ state_attr('input_select.type_of_meal_filtered',
                    'options') | reject('in',
                    states('input_text.type_of_meal_random')) |list |random }}
                target:
                  entity_id: input_text.type_of_meal_random
              - service: input_select.set_options
                data:
                  options: >-
                    ({% set ns = namespace(list=[]) %} {% for option in
                    state_attr("input_select.type_of_meal_filtered", "options")
                    if option != states("input_text.type_of_meal_random") %} {%
                    set ns.list = ns.list+[option] %} {%endfor%} {{ns.list}})
                target:
                  entity_id: input_select.type_of_meal_filtered
            else:
              - service: input_text.set_value
                data:
                  value: >-
                    {{ state_attr('input_select.type_of_meal_filtered',
                    'options') | reject('in',
                    states('input_text.type_of_meal_random')) |list |random }}
                target:
                  entity_id: input_text.type_of_meal_random
              - service: input_select.set_options
                data:
                  options: >-
                    ({{ state_attr('input_select.type_of_meal_full',
                    'options')}})
                target:
                  entity_id: input_select.type_of_meal_filtered
        else:
          - service: input_text.set_value
            data:
              value: >-
                {{ state_attr('input_select.type_of_meal_filtered', 'options') |
                reject('in', states('input_text.type_of_meal_random')) |list
                |random }}
            target:
              entity_id: input_text.type_of_meal_random
          - service: input_select.set_options
            data:
              options: >-
                ({% set ns = namespace(list=[]) %} {% for option in
                state_attr("input_select.type_of_meal_filtered", "options") if
                option != states("input_text.type_of_meal_random") %} {% set
                ns.list = ns.list+[option] %} {%endfor%} {{ns.list}})
            target:
              entity_id: input_select.type_of_meal_filtered
  - if:
      - condition: template
        value_template: >-
          {{ state_attr('input_select.meal_salad_filtered', 'options')  | length
          ==1 }}
    then:
      - if:
          - condition: state
            entity_id: input_select.meal_salad_filtered
            state: RESET
        then:
          - service: input_select.set_options
            data:
              options: ({{ state_attr('input_select.meal_salad_full', 'options')}})
            target:
              entity_id:
                - input_select.meal_salad_filtered
          - service: input_text.set_value
            data:
              value: >-
                {{ state_attr('input_select.meal_salad_filtered', 'options') |
                reject('in', states('input_text.meal_salad_random')) |list
                |random }}
            target:
              entity_id: input_text.meal_salad_random
          - service: input_select.set_options
            data:
              options: >-
                ({% set ns = namespace(list=[]) %} {% for option in
                state_attr("input_select.meal_salad_filtered", "options") if
                option != states("input_text.meal_salad_random") %} {% set
                ns.list = ns.list+[option] %} {%endfor%} {{ns.list}})
            target:
              entity_id: input_select.meal_salad_filtered
            enabled: true
        else:
          - service: input_text.set_value
            data:
              value: >-
                {{ state_attr('input_select.meal_salad_filtered', 'options') |
                reject('in', states('input_text.meal_salad_random')) |list
                |random }}
            target:
              entity_id: input_text.meal_salad_random
          - service: input_select.set_options
            data:
              options: ({{ state_attr('input_select.meal_salad_full', 'options')}})
            target:
              entity_id:
                - input_select.meal_salad_filtered
    else:
      - service: input_text.set_value
        data:
          value: >-
            {{ state_attr('input_select.meal_salad_filtered', 'options') |
            reject('in', states('input_text.meal_salad_random')) |list |random
            }}
        target:
          entity_id: input_text.meal_salad_random
      - service: input_select.set_options
        data:
          options: >-
            ({% set ns = namespace(list=[]) %} {% for option in
            state_attr("input_select.meal_salad_filtered", "options") if option
            != states("input_text.meal_salad_random") %} {% set ns.list =
            ns.list+[option] %} {%endfor%} {{ns.list}})
        target:
          entity_id: input_select.meal_salad_filtered
        enabled: true
  - choose:
      - conditions:
          - condition: state
            entity_id: input_text.type_of_meal_random
            state: Special
        sequence:
          - if:
              - condition: template
                value_template: >-
                  {{ state_attr('input_select.meal_special_filtered',
                  'options')  | length ==1 }}
            then:
              - if:
                  - condition: state
                    entity_id: input_select.meal_special_filtered
                    state: RESET
                then:
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({{ state_attr('input_select.meal_special_full',
                        'options')}})
                    target:
                      entity_id:
                        - input_select.meal_special_filtered
                  - service: input_text.set_value
                    data:
                      value: >-
                        {{ state_attr('input_select.meal_special_filtered',
                        'options') | reject('in',
                        states('input_text.meal_random')) |list |random }}
                    target:
                      entity_id: input_text.meal_random
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({% set ns = namespace(list=[]) %} {% for option in
                        state_attr("input_select.meal_special_filtered",
                        "options") if option != states("input_text.meal_random")
                        %} {% set ns.list = ns.list+[option] %} {%endfor%}
                        {{ns.list}})
                    target:
                      entity_id:
                        - input_select.meal_special_filtered
                  - service: counter.reset
                    data: {}
                    target:
                      entity_id: counter.days_to_special_meal
                else:
                  - service: input_text.set_value
                    data:
                      value: >-
                        {{ state_attr('input_select.meal_special_filtered',
                        'options') | reject('in',
                        states('input_text.meal_random')) |list |random }}
                    target:
                      entity_id: input_text.meal_random
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({{ state_attr('input_select.meal_special_full',
                        'options')}})
                    target:
                      entity_id:
                        - input_select.meal_special_filtered
                  - service: counter.reset
                    data: {}
                    target:
                      entity_id: counter.days_to_special_meal
            else:
              - service: input_text.set_value
                data:
                  value: >-
                    {{ state_attr('input_select.meal_special_filtered',
                    'options') | reject('in', states('input_text.meal_random'))
                    |list |random }}
                target:
                  entity_id: input_text.meal_random
              - service: input_select.set_options
                data:
                  options: >-
                    ({% set ns = namespace(list=[]) %} {% for option in
                    state_attr("input_select.meal_special_filtered", "options")
                    if option != states("input_text.meal_random") %} {% set
                    ns.list = ns.list+[option] %} {%endfor%} {{ns.list}})
                target:
                  entity_id:
                    - input_select.meal_special_filtered
              - service: counter.reset
                data: {}
                target:
                  entity_id: counter.days_to_special_meal
      - conditions:
          - condition: state
            entity_id: input_text.type_of_meal_random
            state: Pork
        sequence:
          - if:
              - condition: template
                value_template: >-
                  {{ state_attr('input_select.meal_poark_filtered', 'options') 
                  | length ==1 }}
            then:
              - if:
                  - condition: state
                    entity_id: input_select.meal_poark_filtered
                    state: RESET
                then:
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({{ state_attr('input_select.meal_pork_full',
                        'options')}})
                    target:
                      entity_id: input_select.meal_poark_filtered
                  - service: input_text.set_value
                    data:
                      value: >-
                        {{ state_attr('input_select.meal_poark_filtered',
                        'options') | random }}
                    target:
                      entity_id: input_text.meal_random
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({% set ns = namespace(list=[]) %} {% for option in
                        state_attr("input_select.meal_poark_filtered",
                        "options") if option != states("input_text.meal_random")
                        %} {% set ns.list = ns.list+[option] %} {%endfor%}
                        {{ns.list}})
                    target:
                      entity_id: input_select.meal_poark_filtered
                else:
                  - service: input_text.set_value
                    data:
                      value: >-
                        {{ state_attr('input_select.meal_poark_filtered',
                        'options') | random }}
                    target:
                      entity_id: input_text.meal_random
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({{ state_attr('input_select.meal_pork_full',
                        'options')}})
                    target:
                      entity_id: input_select.meal_poark_filtered
            else:
              - service: input_text.set_value
                data:
                  value: >-
                    {{ state_attr('input_select.meal_poark_filtered', 'options')
                    | random }}
                target:
                  entity_id: input_text.meal_random
              - service: input_select.set_options
                data:
                  options: >-
                    ({% set ns = namespace(list=[]) %} {% for option in
                    state_attr("input_select.meal_poark_filtered", "options") if
                    option != states("input_text.meal_random") %} {% set ns.list
                    = ns.list+[option] %} {%endfor%} {{ns.list}})
                target:
                  entity_id: input_select.meal_poark_filtered
          - service: counter.decrement
            data: {}
            target:
              entity_id: counter.days_to_special_meal
            enabled: true
      - conditions:
          - condition: state
            entity_id: input_text.type_of_meal_random
            state: Beef
        sequence:
          - if:
              - condition: template
                value_template: >-
                  {{ state_attr('input_select.meal_beff_filtered', 'options')  |
                  length ==1 }}
            then:
              - if:
                  - condition: state
                    entity_id: input_select.meal_beff_filtered
                    state: RESET
                then:
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({{ state_attr('input_select.meal_beff_full',
                        'options')}})
                    target:
                      entity_id: input_select.meal_beff_filtered
                  - service: input_text.set_value
                    data:
                      value: >-
                        {{ state_attr('input_select.meal_beff_filtered',
                        'options') | random }}
                    target:
                      entity_id: input_text.meal_random
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({% set ns = namespace(list=[]) %} {% for option in
                        state_attr("input_select.meal_beff_filtered", "options")
                        if option != states("input_text.meal_random") %} {% set
                        ns.list = ns.list+[option] %} {%endfor%} {{ns.list}})
                    target:
                      entity_id:
                        - input_select.meal_beff_filtered
                else:
                  - service: input_text.set_value
                    data:
                      value: >-
                        {{ state_attr('input_select.meal_beff_filtered',
                        'options') | random }}
                    target:
                      entity_id: input_text.meal_random
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({{ state_attr('input_select.meal_beff_full',
                        'options')}})
                    target:
                      entity_id: input_select.meal_beff_filtered
            else:
              - service: input_text.set_value
                data:
                  value: >-
                    {{ state_attr('input_select.meal_beff_filtered', 'options')
                    | random }}
                target:
                  entity_id: input_text.meal_random
              - service: input_select.set_options
                data:
                  options: >-
                    ({% set ns = namespace(list=[]) %} {% for option in
                    state_attr("input_select.meal_beff_filtered", "options") if
                    option != states("input_text.meal_random") %} {% set ns.list
                    = ns.list+[option] %} {%endfor%} {{ns.list}})
                target:
                  entity_id:
                    - input_select.meal_beff_filtered
          - service: counter.decrement
            data: {}
            target:
              entity_id: counter.days_to_special_meal
            enabled: true
      - conditions:
          - condition: state
            entity_id: input_text.type_of_meal_random
            state: Vegan
        sequence:
          - if:
              - condition: template
                value_template: >-
                  {{ state_attr('input_select.meal_vegan_filtered', 'options') 
                  | length ==1 }}
            then:
              - if:
                  - condition: state
                    entity_id: input_select.meal_vegan_filtered
                    state: RESET
                then:
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({{ state_attr('input_select.meal_vegan_full',
                        'options')}})
                    target:
                      entity_id:
                        - input_select.meal_vegan_filtered
                  - service: input_text.set_value
                    data:
                      value: >-
                        {{ state_attr('input_select.meal_vegan_filtered',
                        'options') | random }}
                    target:
                      entity_id: input_text.meal_random
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({% set ns = namespace(list=[]) %} {% for option in
                        state_attr("input_select.meal_vegan_filtered",
                        "options") if option != states("input_text.meal_random")
                        %} {% set ns.list = ns.list+[option] %} {%endfor%}
                        {{ns.list}})
                    target:
                      entity_id:
                        - input_select.meal_vegan_filtered
                else:
                  - service: input_text.set_value
                    data:
                      value: >-
                        {{ state_attr('input_select.meal_vegan_filtered',
                        'options') | random }}
                    target:
                      entity_id: input_text.meal_random
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({{ state_attr('input_select.meal_vegan_full',
                        'options')}})
                    target:
                      entity_id:
                        - input_select.meal_vegan_filtered
            else:
              - service: input_text.set_value
                data:
                  value: >-
                    {{ state_attr('input_select.meal_vegan_filtered', 'options')
                    | random }}
                target:
                  entity_id: input_text.meal_random
              - service: input_select.set_options
                data:
                  options: >-
                    ({% set ns = namespace(list=[]) %} {% for option in
                    state_attr("input_select.meal_vegan_filtered", "options") if
                    option != states("input_text.meal_random") %} {% set ns.list
                    = ns.list+[option] %} {%endfor%} {{ns.list}})
                target:
                  entity_id:
                    - input_select.meal_vegan_filtered
          - service: counter.decrement
            data: {}
            target:
              entity_id: counter.days_to_special_meal
            enabled: true
      - conditions:
          - condition: state
            entity_id: input_text.type_of_meal_random
            state: Fish
        sequence:
          - if:
              - condition: template
                value_template: >-
                  {{ state_attr('input_select.meal_fish_filtered', 'options')  |
                  length ==1 }}
            then:
              - if:
                  - condition: state
                    entity_id: input_select.meal_fish_filtered
                    state: RESET
                then:
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({{ state_attr('input_select.meal_fish_full',
                        'options')}})
                    target:
                      entity_id:
                        - input_select.meal_fish_filtered
                  - service: input_text.set_value
                    data:
                      value: >-
                        {{ state_attr('input_select.meal_fish_filtered',
                        'options') | random }}
                    target:
                      entity_id: input_text.meal_random
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({% set ns = namespace(list=[]) %} {% for option in
                        state_attr("input_select.meal_fish_filtered", "options")
                        if option != states("input_text.meal_random") %} {% set
                        ns.list = ns.list+[option] %} {%endfor%} {{ns.list}})
                    target:
                      entity_id:
                        - input_select.meal_fish_filtered
                else:
                  - service: input_text.set_value
                    data:
                      value: >-
                        {{ state_attr('input_select.meal_fish_filtered',
                        'options') | random }}
                    target:
                      entity_id: input_text.meal_random
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({{ state_attr('input_select.meal_fish_full',
                        'options')}})
                    target:
                      entity_id:
                        - input_select.meal_fish_filtered
            else:
              - service: input_text.set_value
                data:
                  value: >-
                    {{ state_attr('input_select.meal_fish_filtered', 'options')
                    | random }}
                target:
                  entity_id: input_text.meal_random
              - service: input_select.set_options
                data:
                  options: >-
                    ({% set ns = namespace(list=[]) %} {% for option in
                    state_attr("input_select.meal_fish_filtered", "options") if
                    option != states("input_text.meal_random") %} {% set ns.list
                    = ns.list+[option] %} {%endfor%} {{ns.list}})
                target:
                  entity_id:
                    - input_select.meal_fish_filtered
          - service: counter.decrement
            data: {}
            target:
              entity_id: counter.days_to_special_meal
            enabled: true
      - conditions:
          - condition: state
            entity_id: input_text.type_of_meal_random
            state: Eggs
        sequence:
          - if:
              - condition: template
                value_template: >-
                  {{ state_attr('input_select.meal_eggs_filtered', 'options')  |
                  length ==1 }}
            then:
              - if:
                  - condition: state
                    entity_id: input_select.meal_eggs_filtered
                    state: RESET
                then:
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({{ state_attr('input_select.meal_eggs_full',
                        'options')}})
                    target:
                      entity_id:
                        - input_select.meal_eggs_filtered
                  - service: input_text.set_value
                    data:
                      value: >-
                        {{ state_attr('input_select.meal_eggs_filtered',
                        'options') | random }}
                    target:
                      entity_id: input_text.meal_random
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({% set ns = namespace(list=[]) %} {% for option in
                        state_attr("input_select.meal_eggs_filtered", "options")
                        if option != states("input_text.meal_random") %} {% set
                        ns.list = ns.list+[option] %} {%endfor%} {{ns.list}})
                    target:
                      entity_id:
                        - input_select.meal_eggs_filtered
                else:
                  - service: input_text.set_value
                    data:
                      value: >-
                        {{ state_attr('input_select.meal_eggs_filtered',
                        'options') | random }}
                    target:
                      entity_id: input_text.meal_random
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({{ state_attr('input_select.meal_eggs_full',
                        'options')}})
                    target:
                      entity_id:
                        - input_select.meal_eggs_filtered
            else:
              - service: input_text.set_value
                data:
                  value: >-
                    {{ state_attr('input_select.meal_eggs_filtered', 'options')
                    | random }}
                target:
                  entity_id: input_text.meal_random
              - service: input_select.set_options
                data:
                  options: >-
                    ({% set ns = namespace(list=[]) %} {% for option in
                    state_attr("input_select.meal_eggs_filtered", "options") if
                    option != states("input_text.meal_random") %} {% set ns.list
                    = ns.list+[option] %} {%endfor%} {{ns.list}})
                target:
                  entity_id:
                    - input_select.meal_eggs_filtered
          - service: counter.decrement
            data: {}
            target:
              entity_id: counter.days_to_special_meal
            enabled: true
      - conditions:
          - condition: state
            entity_id: input_text.type_of_meal_random
            state: Chicken
        sequence:
          - if:
              - condition: template
                value_template: >-
                  {{ state_attr('input_select.meal_chicken_filtered',
                  'options')  | length ==1 }}
            then:
              - if:
                  - condition: state
                    entity_id: input_select.meal_chicken_filtered
                    state: RESET
                then:
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({{ state_attr('input_select.meal_chicken_full',
                        'options')}})
                    target:
                      entity_id:
                        - input_select.meal_chicken_filtered
                  - service: input_text.set_value
                    data:
                      value: >-
                        {{ state_attr('input_select.meal_chicken_filtered',
                        'options') | random }}
                    target:
                      entity_id: input_text.meal_random
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({% set ns = namespace(list=[]) %} {% for option in
                        state_attr("input_select.meal_chicken_filtered",
                        "options") if option != states("input_text.meal_random")
                        %} {% set ns.list = ns.list+[option] %} {%endfor%}
                        {{ns.list}})
                    target:
                      entity_id:
                        - input_select.meal_chicken_filtered
                else:
                  - service: input_text.set_value
                    data:
                      value: >-
                        {{ state_attr('input_select.meal_chicken_filtered',
                        'options') | random }}
                    target:
                      entity_id: input_text.meal_random
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({{ state_attr('input_select.meal_chicken_full',
                        'options')}})
                    target:
                      entity_id:
                        - input_select.meal_chicken_filtered
            else:
              - service: input_text.set_value
                data:
                  value: >-
                    {{ state_attr('input_select.meal_chicken_filtered',
                    'options') | random }}
                target:
                  entity_id: input_text.meal_random
              - service: input_select.set_options
                data:
                  options: >-
                    ({% set ns = namespace(list=[]) %} {% for option in
                    state_attr("input_select.meal_chicken_filtered", "options")
                    if option != states("input_text.meal_random") %} {% set
                    ns.list = ns.list+[option] %} {%endfor%} {{ns.list}})
                target:
                  entity_id:
                    - input_select.meal_chicken_filtered
          - service: counter.decrement
            data: {}
            target:
              entity_id: counter.days_to_special_meal
            enabled: true
    default:
      - service: notify.notify_marius
        data:
          title: Home Assistant
          message: Error in chose food for the day.
mode: single
icon: mdi:food

Thank you very much, you shared what I was looking for a long time ago, a great project

if you are interested in the full functionality here are the repick scripts:
repick meal:

alias: choose_meal_repick
sequence:
  - if:
      - condition: and
        conditions:
          - condition: not
            conditions:
              - condition: state
                entity_id: input_text.type_of_meal_random
                state: Special
          - condition: state
            entity_id: counter.days_to_special_meal
            state: "0"
    then:
      - service: counter.increment
        data: {}
        target:
          entity_id: counter.days_to_special_meal
  - choose:
      - conditions:
          - condition: state
            entity_id: input_text.type_of_meal_random
            state: Special
        sequence:
          - if:
              - condition: template
                value_template: >-
                  {{ state_attr('input_select.meal_special_filtered',
                  'options')  | length ==1 }}
            then:
              - if:
                  - condition: state
                    entity_id: input_select.meal_special_filtered
                    state: RESET
                then:
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({{ state_attr('input_select.meal_special_full',
                        'options')}})
                    target:
                      entity_id:
                        - input_select.meal_special_filtered
                  - service: input_text.set_value
                    data:
                      value: >-
                        {{ state_attr('input_select.meal_special_filtered',
                        'options') | reject('in',
                        states('input_text.meal_random')) |list |random }}
                    target:
                      entity_id: input_text.meal_random
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({% set ns = namespace(list=[]) %} {% for option in
                        state_attr("input_select.meal_special_filtered",
                        "options") if option != states("input_text.meal_random")
                        %} {% set ns.list = ns.list+[option] %} {%endfor%}
                        {{ns.list}})
                    target:
                      entity_id:
                        - input_select.meal_special_filtered
                else:
                  - service: input_text.set_value
                    data:
                      value: >-
                        {{ state_attr('input_select.meal_special_filtered',
                        'options') | reject('in',
                        states('input_text.meal_random')) |list |random }}
                    target:
                      entity_id: input_text.meal_random
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({{ state_attr('input_select.meal_special_full',
                        'options')}})
                    target:
                      entity_id:
                        - input_select.meal_special_filtered
            else:
              - service: input_text.set_value
                data:
                  value: >-
                    {{ state_attr('input_select.meal_special_filtered',
                    'options') | reject('in', states('input_text.meal_random'))
                    |list |random }}
                target:
                  entity_id: input_text.meal_random
              - service: input_select.set_options
                data:
                  options: >-
                    ({% set ns = namespace(list=[]) %} {% for option in
                    state_attr("input_select.meal_special_filtered", "options")
                    if option != states("input_text.meal_random") %} {% set
                    ns.list = ns.list+[option] %} {%endfor%} {{ns.list}})
                target:
                  entity_id:
                    - input_select.meal_special_filtered
      - conditions:
          - condition: state
            entity_id: input_text.type_of_meal_random
            state: Pork
        sequence:
          - if:
              - condition: template
                value_template: >-
                  {{ state_attr('input_select.meal_poark_filtered', 'options') 
                  | length ==1 }}
            then:
              - if:
                  - condition: state
                    entity_id: input_select.meal_poark_filtered
                    state: RESET
                then:
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({{ state_attr('input_select.meal_pork_full',
                        'options')}})
                    target:
                      entity_id: input_select.meal_poark_filtered
                  - service: input_text.set_value
                    data:
                      value: >-
                        {{ state_attr('input_select.meal_poark_filtered',
                        'options') | random }}
                    target:
                      entity_id: input_text.meal_random
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({% set ns = namespace(list=[]) %} {% for option in
                        state_attr("input_select.meal_poark_filtered",
                        "options") if option != states("input_text.meal_random")
                        %} {% set ns.list = ns.list+[option] %} {%endfor%}
                        {{ns.list}})
                    target:
                      entity_id: input_select.meal_poark_filtered
                else:
                  - service: input_text.set_value
                    data:
                      value: >-
                        {{ state_attr('input_select.meal_poark_filtered',
                        'options') | random }}
                    target:
                      entity_id: input_text.meal_random
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({{ state_attr('input_select.meal_pork_full',
                        'options')}})
                    target:
                      entity_id: input_select.meal_poark_filtered
            else:
              - service: input_text.set_value
                data:
                  value: >-
                    {{ state_attr('input_select.meal_poark_filtered', 'options')
                    | random }}
                target:
                  entity_id: input_text.meal_random
              - service: input_select.set_options
                data:
                  options: >-
                    ({% set ns = namespace(list=[]) %} {% for option in
                    state_attr("input_select.meal_poark_filtered", "options") if
                    option != states("input_text.meal_random") %} {% set ns.list
                    = ns.list+[option] %} {%endfor%} {{ns.list}})
                target:
                  entity_id: input_select.meal_poark_filtered
      - conditions:
          - condition: state
            entity_id: input_text.type_of_meal_random
            state: Beef
        sequence:
          - if:
              - condition: template
                value_template: >-
                  {{ state_attr('input_select.meal_beff_filtered', 'options')  |
                  length ==1 }}
            then:
              - if:
                  - condition: state
                    entity_id: input_select.meal_beff_filtered
                    state: RESET
                then:
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({{ state_attr('input_select.meal_beff_full',
                        'options')}})
                    target:
                      entity_id: input_select.meal_beff_filtered
                  - service: input_text.set_value
                    data:
                      value: >-
                        {{ state_attr('input_select.meal_beff_filtered',
                        'options') | random }}
                    target:
                      entity_id: input_text.meal_random
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({% set ns = namespace(list=[]) %} {% for option in
                        state_attr("input_select.meal_beff_filtered", "options")
                        if option != states("input_text.meal_random") %} {% set
                        ns.list = ns.list+[option] %} {%endfor%} {{ns.list}})
                    target:
                      entity_id:
                        - input_select.meal_beff_filtered
                else:
                  - service: input_text.set_value
                    data:
                      value: >-
                        {{ state_attr('input_select.meal_beff_filtered',
                        'options') | random }}
                    target:
                      entity_id: input_text.meal_random
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({{ state_attr('input_select.meal_beff_full',
                        'options')}})
                    target:
                      entity_id: input_select.meal_beff_filtered
            else:
              - service: input_text.set_value
                data:
                  value: >-
                    {{ state_attr('input_select.meal_beff_filtered', 'options')
                    | random }}
                target:
                  entity_id: input_text.meal_random
              - service: input_select.set_options
                data:
                  options: >-
                    ({% set ns = namespace(list=[]) %} {% for option in
                    state_attr("input_select.meal_beff_filtered", "options") if
                    option != states("input_text.meal_random") %} {% set ns.list
                    = ns.list+[option] %} {%endfor%} {{ns.list}})
                target:
                  entity_id:
                    - input_select.meal_beff_filtered
      - conditions:
          - condition: state
            entity_id: input_text.type_of_meal_random
            state: Vegan
        sequence:
          - if:
              - condition: template
                value_template: >-
                  {{ state_attr('input_select.meal_vegan_filtered', 'options') 
                  | length ==1 }}
            then:
              - if:
                  - condition: state
                    entity_id: input_select.meal_vegan_filtered
                    state: RESET
                then:
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({{ state_attr('input_select.meal_vegan_full',
                        'options')}})
                    target:
                      entity_id:
                        - input_select.meal_vegan_filtered
                  - service: input_text.set_value
                    data:
                      value: >-
                        {{ state_attr('input_select.meal_vegan_filtered',
                        'options') | random }}
                    target:
                      entity_id: input_text.meal_random
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({% set ns = namespace(list=[]) %} {% for option in
                        state_attr("input_select.meal_vegan_filtered",
                        "options") if option != states("input_text.meal_random")
                        %} {% set ns.list = ns.list+[option] %} {%endfor%}
                        {{ns.list}})
                    target:
                      entity_id:
                        - input_select.meal_vegan_filtered
                else:
                  - service: input_text.set_value
                    data:
                      value: >-
                        {{ state_attr('input_select.meal_vegan_filtered',
                        'options') | random }}
                    target:
                      entity_id: input_text.meal_random
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({{ state_attr('input_select.meal_vegan_full',
                        'options')}})
                    target:
                      entity_id:
                        - input_select.meal_vegan_filtered
            else:
              - service: input_text.set_value
                data:
                  value: >-
                    {{ state_attr('input_select.meal_vegan_filtered', 'options')
                    | random }}
                target:
                  entity_id: input_text.meal_random
              - service: input_select.set_options
                data:
                  options: >-
                    ({% set ns = namespace(list=[]) %} {% for option in
                    state_attr("input_select.meal_vegan_filtered", "options") if
                    option != states("input_text.meal_random") %} {% set ns.list
                    = ns.list+[option] %} {%endfor%} {{ns.list}})
                target:
                  entity_id:
                    - input_select.meal_vegan_filtered
      - conditions:
          - condition: state
            entity_id: input_text.type_of_meal_random
            state: Fish
        sequence:
          - if:
              - condition: template
                value_template: >-
                  {{ state_attr('input_select.meal_fish_filtered', 'options')  |
                  length ==1 }}
            then:
              - if:
                  - condition: state
                    entity_id: input_select.meal_fish_filtered
                    state: RESET
                then:
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({{ state_attr('input_select.meal_fish_full',
                        'options')}})
                    target:
                      entity_id:
                        - input_select.meal_fish_filtered
                  - service: input_text.set_value
                    data:
                      value: >-
                        {{ state_attr('input_select.meal_fish_filtered',
                        'options') | random }}
                    target:
                      entity_id: input_text.meal_random
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({% set ns = namespace(list=[]) %} {% for option in
                        state_attr("input_select.meal_fish_filtered", "options")
                        if option != states("input_text.meal_random") %} {% set
                        ns.list = ns.list+[option] %} {%endfor%} {{ns.list}})
                    target:
                      entity_id:
                        - input_select.meal_fish_filtered
                else:
                  - service: input_text.set_value
                    data:
                      value: >-
                        {{ state_attr('input_select.meal_fish_filtered',
                        'options') | random }}
                    target:
                      entity_id: input_text.meal_random
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({{ state_attr('input_select.meal_fish_full',
                        'options')}})
                    target:
                      entity_id:
                        - input_select.meal_fish_filtered
            else:
              - service: input_text.set_value
                data:
                  value: >-
                    {{ state_attr('input_select.meal_fish_filtered', 'options')
                    | random }}
                target:
                  entity_id: input_text.meal_random
              - service: input_select.set_options
                data:
                  options: >-
                    ({% set ns = namespace(list=[]) %} {% for option in
                    state_attr("input_select.meal_fish_filtered", "options") if
                    option != states("input_text.meal_random") %} {% set ns.list
                    = ns.list+[option] %} {%endfor%} {{ns.list}})
                target:
                  entity_id:
                    - input_select.meal_fish_filtered
      - conditions:
          - condition: state
            entity_id: input_text.type_of_meal_random
            state: Eggs
        sequence:
          - if:
              - condition: template
                value_template: >-
                  {{ state_attr('input_select.meal_eggs_filtered', 'options')  |
                  length ==1 }}
            then:
              - if:
                  - condition: state
                    entity_id: input_select.meal_eggs_filtered
                    state: RESET
                then:
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({{ state_attr('input_select.meal_eggs_full',
                        'options')}})
                    target:
                      entity_id:
                        - input_select.meal_eggs_filtered
                  - service: input_text.set_value
                    data:
                      value: >-
                        {{ state_attr('input_select.meal_eggs_filtered',
                        'options') | random }}
                    target:
                      entity_id: input_text.meal_random
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({% set ns = namespace(list=[]) %} {% for option in
                        state_attr("input_select.meal_eggs_filtered", "options")
                        if option != states("input_text.meal_random") %} {% set
                        ns.list = ns.list+[option] %} {%endfor%} {{ns.list}})
                    target:
                      entity_id:
                        - input_select.meal_eggs_filtered
                else:
                  - service: input_text.set_value
                    data:
                      value: >-
                        {{ state_attr('input_select.meal_eggs_filtered',
                        'options') | random }}
                    target:
                      entity_id: input_text.meal_random
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({{ state_attr('input_select.meal_eggs_full',
                        'options')}})
                    target:
                      entity_id:
                        - input_select.meal_eggs_filtered
            else:
              - service: input_text.set_value
                data:
                  value: >-
                    {{ state_attr('input_select.meal_eggs_filtered', 'options')
                    | random }}
                target:
                  entity_id: input_text.meal_random
              - service: input_select.set_options
                data:
                  options: >-
                    ({% set ns = namespace(list=[]) %} {% for option in
                    state_attr("input_select.meal_eggs_filtered", "options") if
                    option != states("input_text.meal_random") %} {% set ns.list
                    = ns.list+[option] %} {%endfor%} {{ns.list}})
                target:
                  entity_id:
                    - input_select.meal_eggs_filtered
      - conditions:
          - condition: state
            entity_id: input_text.type_of_meal_random
            state: Chicken
        sequence:
          - if:
              - condition: template
                value_template: >-
                  {{ state_attr('input_select.meal_chicken_filtered',
                  'options')  | length ==1 }}
            then:
              - if:
                  - condition: state
                    entity_id: input_select.meal_chicken_filtered
                    state: RESET
                then:
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({{ state_attr('input_select.meal_chicken_full',
                        'options')}})
                    target:
                      entity_id:
                        - input_select.meal_chicken_filtered
                  - service: input_text.set_value
                    data:
                      value: >-
                        {{ state_attr('input_select.meal_chicken_filtered',
                        'options') | random }}
                    target:
                      entity_id: input_text.meal_random
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({% set ns = namespace(list=[]) %} {% for option in
                        state_attr("input_select.meal_chicken_filtered",
                        "options") if option != states("input_text.meal_random")
                        %} {% set ns.list = ns.list+[option] %} {%endfor%}
                        {{ns.list}})
                    target:
                      entity_id:
                        - input_select.meal_chicken_filtered
                else:
                  - service: input_text.set_value
                    data:
                      value: >-
                        {{ state_attr('input_select.meal_chicken_filtered',
                        'options') | random }}
                    target:
                      entity_id: input_text.meal_random
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({{ state_attr('input_select.meal_chicken_full',
                        'options')}})
                    target:
                      entity_id:
                        - input_select.meal_chicken_filtered
            else:
              - service: input_text.set_value
                data:
                  value: >-
                    {{ state_attr('input_select.meal_chicken_filtered',
                    'options') | random }}
                target:
                  entity_id: input_text.meal_random
              - service: input_select.set_options
                data:
                  options: >-
                    ({% set ns = namespace(list=[]) %} {% for option in
                    state_attr("input_select.meal_chicken_filtered", "options")
                    if option != states("input_text.meal_random") %} {% set
                    ns.list = ns.list+[option] %} {%endfor%} {{ns.list}})
                target:
                  entity_id:
                    - input_select.meal_chicken_filtered
    default:
      - service: notify.notify_marius
        data:
          title: Home Assistant
          message: Error in chose food for the day.
  - service: notify.notify_all
    data:
      title: Home Assistant
      message: >-
        Repick meal was triggerd, new for today:
        {{states("input_text.meal_random")}}.
    enabled: true
mode: single
icon: mdi:food

repick meal type:

alias: choose_meal_type_repick
sequence:
  - if:
      - condition: template
        value_template: >-
          {{ state_attr('input_select.type_of_meal_filtered', 'options')  |
          length ==1 }}
    then:
      - if:
          - condition: state
            entity_id: input_select.type_of_meal_filtered
            state: RESET
        then:
          - service: input_select.set_options
            data:
              options: ({{ state_attr('input_select.type_of_meal_full', 'options')}})
            target:
              entity_id: input_select.type_of_meal_filtered
          - service: input_text.set_value
            data:
              value: >-
                {{ state_attr('input_select.type_of_meal_filtered', 'options') |
                reject('in', states('input_text.type_of_meal_random')) |list
                |random }}
            target:
              entity_id: input_text.type_of_meal_random
          - service: input_select.set_options
            data:
              options: >-
                ({% set ns = namespace(list=[]) %} {% for option in
                state_attr("input_select.type_of_meal_filtered", "options") if
                option != states("input_text.type_of_meal_random") %} {% set
                ns.list = ns.list+[option] %} {%endfor%} {{ns.list}})
            target:
              entity_id: input_select.type_of_meal_filtered
        else:
          - service: input_text.set_value
            data:
              value: >-
                {{ state_attr('input_select.type_of_meal_filtered', 'options') |
                reject('in', states('input_text.type_of_meal_random')) |list
                |random }}
            target:
              entity_id: input_text.type_of_meal_random
          - service: input_select.set_options
            data:
              options: ({{ state_attr('input_select.type_of_meal_full', 'options')}})
            target:
              entity_id: input_select.type_of_meal_filtered
    else:
      - service: input_text.set_value
        data:
          value: >-
            {{ state_attr('input_select.type_of_meal_filtered', 'options') |
            reject('in', states('input_text.type_of_meal_random')) |list |random
            }}
        target:
          entity_id: input_text.type_of_meal_random
      - service: input_select.set_options
        data:
          options: >-
            ({% set ns = namespace(list=[]) %} {% for option in
            state_attr("input_select.type_of_meal_filtered", "options") if
            option != states("input_text.type_of_meal_random") %} {% set ns.list
            = ns.list+[option] %} {%endfor%} {{ns.list}})
        target:
          entity_id: input_select.type_of_meal_filtered
  - if:
      - condition: and
        conditions:
          - condition: not
            conditions:
              - condition: state
                entity_id: input_text.type_of_meal_random
                state: Special
          - condition: state
            entity_id: counter.days_to_special_meal
            state: "0"
    then:
      - service: counter.increment
        data: {}
        target:
          entity_id: counter.days_to_special_meal
  - choose:
      - conditions:
          - condition: state
            entity_id: input_text.type_of_meal_random
            state: Pork
        sequence:
          - if:
              - condition: template
                value_template: >-
                  {{ state_attr('input_select.meal_poark_filtered', 'options') 
                  | length ==1 }}
            then:
              - if:
                  - condition: state
                    entity_id: input_select.meal_poark_filtered
                    state: RESET
                then:
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({{ state_attr('input_select.meal_pork_full',
                        'options')}})
                    target:
                      entity_id: input_select.meal_poark_filtered
                  - service: input_text.set_value
                    data:
                      value: >-
                        {{ state_attr('input_select.meal_poark_filtered',
                        'options') | random }}
                    target:
                      entity_id: input_text.meal_random
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({% set ns = namespace(list=[]) %} {% for option in
                        state_attr("input_select.meal_poark_filtered",
                        "options") if option != states("input_text.meal_random")
                        %} {% set ns.list = ns.list+[option] %} {%endfor%}
                        {{ns.list}})
                    target:
                      entity_id: input_select.meal_poark_filtered
                else:
                  - service: input_text.set_value
                    data:
                      value: >-
                        {{ state_attr('input_select.meal_poark_filtered',
                        'options') | random }}
                    target:
                      entity_id: input_text.meal_random
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({{ state_attr('input_select.meal_pork_full',
                        'options')}})
                    target:
                      entity_id: input_select.meal_poark_filtered
            else:
              - service: input_text.set_value
                data:
                  value: >-
                    {{ state_attr('input_select.meal_poark_filtered', 'options')
                    | random }}
                target:
                  entity_id: input_text.meal_random
              - service: input_select.set_options
                data:
                  options: >-
                    ({% set ns = namespace(list=[]) %} {% for option in
                    state_attr("input_select.meal_poark_filtered", "options") if
                    option != states("input_text.meal_random") %} {% set ns.list
                    = ns.list+[option] %} {%endfor%} {{ns.list}})
                target:
                  entity_id: input_select.meal_poark_filtered
      - conditions:
          - condition: state
            entity_id: input_text.type_of_meal_random
            state: Beef
        sequence:
          - if:
              - condition: template
                value_template: >-
                  {{ state_attr('input_select.meal_beff_filtered', 'options')  |
                  length ==1 }}
            then:
              - if:
                  - condition: state
                    entity_id: input_select.meal_beff_filtered
                    state: RESET
                then:
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({{ state_attr('input_select.meal_beff_full',
                        'options')}})
                    target:
                      entity_id: input_select.meal_beff_filtered
                  - service: input_text.set_value
                    data:
                      value: >-
                        {{ state_attr('input_select.meal_beff_filtered',
                        'options') | random }}
                    target:
                      entity_id: input_text.meal_random
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({% set ns = namespace(list=[]) %} {% for option in
                        state_attr("input_select.meal_beff_filtered", "options")
                        if option != states("input_text.meal_random") %} {% set
                        ns.list = ns.list+[option] %} {%endfor%} {{ns.list}})
                    target:
                      entity_id:
                        - input_select.meal_beff_filtered
                else:
                  - service: input_text.set_value
                    data:
                      value: >-
                        {{ state_attr('input_select.meal_beff_filtered',
                        'options') | random }}
                    target:
                      entity_id: input_text.meal_random
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({{ state_attr('input_select.meal_beff_full',
                        'options')}})
                    target:
                      entity_id: input_select.meal_beff_filtered
            else:
              - service: input_text.set_value
                data:
                  value: >-
                    {{ state_attr('input_select.meal_beff_filtered', 'options')
                    | random }}
                target:
                  entity_id: input_text.meal_random
              - service: input_select.set_options
                data:
                  options: >-
                    ({% set ns = namespace(list=[]) %} {% for option in
                    state_attr("input_select.meal_beff_filtered", "options") if
                    option != states("input_text.meal_random") %} {% set ns.list
                    = ns.list+[option] %} {%endfor%} {{ns.list}})
                target:
                  entity_id:
                    - input_select.meal_beff_filtered
      - conditions:
          - condition: state
            entity_id: input_text.type_of_meal_random
            state: Vegan
        sequence:
          - if:
              - condition: template
                value_template: >-
                  {{ state_attr('input_select.meal_vegan_filtered', 'options') 
                  | length ==1 }}
            then:
              - if:
                  - condition: state
                    entity_id: input_select.meal_vegan_filtered
                    state: RESET
                then:
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({{ state_attr('input_select.meal_vegan_full',
                        'options')}})
                    target:
                      entity_id:
                        - input_select.meal_vegan_filtered
                  - service: input_text.set_value
                    data:
                      value: >-
                        {{ state_attr('input_select.meal_vegan_filtered',
                        'options') | random }}
                    target:
                      entity_id: input_text.meal_random
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({% set ns = namespace(list=[]) %} {% for option in
                        state_attr("input_select.meal_vegan_filtered",
                        "options") if option != states("input_text.meal_random")
                        %} {% set ns.list = ns.list+[option] %} {%endfor%}
                        {{ns.list}})
                    target:
                      entity_id:
                        - input_select.meal_vegan_filtered
                else:
                  - service: input_text.set_value
                    data:
                      value: >-
                        {{ state_attr('input_select.meal_vegan_filtered',
                        'options') | random }}
                    target:
                      entity_id: input_text.meal_random
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({{ state_attr('input_select.meal_vegan_full',
                        'options')}})
                    target:
                      entity_id:
                        - input_select.meal_vegan_filtered
            else:
              - service: input_text.set_value
                data:
                  value: >-
                    {{ state_attr('input_select.meal_vegan_filtered', 'options')
                    | random }}
                target:
                  entity_id: input_text.meal_random
              - service: input_select.set_options
                data:
                  options: >-
                    ({% set ns = namespace(list=[]) %} {% for option in
                    state_attr("input_select.meal_vegan_filtered", "options") if
                    option != states("input_text.meal_random") %} {% set ns.list
                    = ns.list+[option] %} {%endfor%} {{ns.list}})
                target:
                  entity_id:
                    - input_select.meal_vegan_filtered
      - conditions:
          - condition: state
            entity_id: input_text.type_of_meal_random
            state: Fish
        sequence:
          - if:
              - condition: template
                value_template: >-
                  {{ state_attr('input_select.meal_fish_filtered', 'options')  |
                  length ==1 }}
            then:
              - if:
                  - condition: state
                    entity_id: input_select.meal_fish_filtered
                    state: RESET
                then:
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({{ state_attr('input_select.meal_fish_full',
                        'options')}})
                    target:
                      entity_id:
                        - input_select.meal_fish_filtered
                  - service: input_text.set_value
                    data:
                      value: >-
                        {{ state_attr('input_select.meal_fish_filtered',
                        'options') | random }}
                    target:
                      entity_id: input_text.meal_random
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({% set ns = namespace(list=[]) %} {% for option in
                        state_attr("input_select.meal_fish_filtered", "options")
                        if option != states("input_text.meal_random") %} {% set
                        ns.list = ns.list+[option] %} {%endfor%} {{ns.list}})
                    target:
                      entity_id:
                        - input_select.meal_fish_filtered
                else:
                  - service: input_text.set_value
                    data:
                      value: >-
                        {{ state_attr('input_select.meal_fish_filtered',
                        'options') | random }}
                    target:
                      entity_id: input_text.meal_random
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({{ state_attr('input_select.meal_fish_full',
                        'options')}})
                    target:
                      entity_id:
                        - input_select.meal_fish_filtered
            else:
              - service: input_text.set_value
                data:
                  value: >-
                    {{ state_attr('input_select.meal_fish_filtered', 'options')
                    | random }}
                target:
                  entity_id: input_text.meal_random
              - service: input_select.set_options
                data:
                  options: >-
                    ({% set ns = namespace(list=[]) %} {% for option in
                    state_attr("input_select.meal_fish_filtered", "options") if
                    option != states("input_text.meal_random") %} {% set ns.list
                    = ns.list+[option] %} {%endfor%} {{ns.list}})
                target:
                  entity_id:
                    - input_select.meal_fish_filtered
      - conditions:
          - condition: state
            entity_id: input_text.type_of_meal_random
            state: Eggs
        sequence:
          - if:
              - condition: template
                value_template: >-
                  {{ state_attr('input_select.meal_eggs_filtered', 'options')  |
                  length ==1 }}
            then:
              - if:
                  - condition: state
                    entity_id: input_select.meal_eggs_filtered
                    state: RESET
                then:
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({{ state_attr('input_select.meal_eggs_full',
                        'options')}})
                    target:
                      entity_id:
                        - input_select.meal_eggs_filtered
                  - service: input_text.set_value
                    data:
                      value: >-
                        {{ state_attr('input_select.meal_eggs_filtered',
                        'options') | random }}
                    target:
                      entity_id: input_text.meal_random
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({% set ns = namespace(list=[]) %} {% for option in
                        state_attr("input_select.meal_eggs_filtered", "options")
                        if option != states("input_text.meal_random") %} {% set
                        ns.list = ns.list+[option] %} {%endfor%} {{ns.list}})
                    target:
                      entity_id:
                        - input_select.meal_eggs_filtered
                else:
                  - service: input_text.set_value
                    data:
                      value: >-
                        {{ state_attr('input_select.meal_eggs_filtered',
                        'options') | random }}
                    target:
                      entity_id: input_text.meal_random
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({{ state_attr('input_select.meal_eggs_full',
                        'options')}})
                    target:
                      entity_id:
                        - input_select.meal_eggs_filtered
            else:
              - service: input_text.set_value
                data:
                  value: >-
                    {{ state_attr('input_select.meal_eggs_filtered', 'options')
                    | random }}
                target:
                  entity_id: input_text.meal_random
              - service: input_select.set_options
                data:
                  options: >-
                    ({% set ns = namespace(list=[]) %} {% for option in
                    state_attr("input_select.meal_eggs_filtered", "options") if
                    option != states("input_text.meal_random") %} {% set ns.list
                    = ns.list+[option] %} {%endfor%} {{ns.list}})
                target:
                  entity_id:
                    - input_select.meal_eggs_filtered
      - conditions:
          - condition: state
            entity_id: input_text.type_of_meal_random
            state: Chicken
        sequence:
          - if:
              - condition: template
                value_template: >-
                  {{ state_attr('input_select.meal_chicken_filtered',
                  'options')  | length ==1 }}
            then:
              - if:
                  - condition: state
                    entity_id: input_select.meal_chicken_filtered
                    state: RESET
                then:
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({{ state_attr('input_select.meal_chicken_full',
                        'options')}})
                    target:
                      entity_id:
                        - input_select.meal_chicken_filtered
                  - service: input_text.set_value
                    data:
                      value: >-
                        {{ state_attr('input_select.meal_chicken_filtered',
                        'options') | random }}
                    target:
                      entity_id: input_text.meal_random
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({% set ns = namespace(list=[]) %} {% for option in
                        state_attr("input_select.meal_chicken_filtered",
                        "options") if option != states("input_text.meal_random")
                        %} {% set ns.list = ns.list+[option] %} {%endfor%}
                        {{ns.list}})
                    target:
                      entity_id:
                        - input_select.meal_chicken_filtered
                else:
                  - service: input_text.set_value
                    data:
                      value: >-
                        {{ state_attr('input_select.meal_chicken_filtered',
                        'options') | random }}
                    target:
                      entity_id: input_text.meal_random
                  - service: input_select.set_options
                    data:
                      options: >-
                        ({{ state_attr('input_select.meal_chicken_full',
                        'options')}})
                    target:
                      entity_id:
                        - input_select.meal_chicken_filtered
            else:
              - service: input_text.set_value
                data:
                  value: >-
                    {{ state_attr('input_select.meal_chicken_filtered',
                    'options') | random }}
                target:
                  entity_id: input_text.meal_random
              - service: input_select.set_options
                data:
                  options: >-
                    ({% set ns = namespace(list=[]) %} {% for option in
                    state_attr("input_select.meal_chicken_filtered", "options")
                    if option != states("input_text.meal_random") %} {% set
                    ns.list = ns.list+[option] %} {%endfor%} {{ns.list}})
                target:
                  entity_id:
                    - input_select.meal_chicken_filtered
    default:
      - service: notify.notify_marius
        data:
          title: Home Assistant
          message: Error in chose food for the day.
  - service: notify.notify_all
    data:
      title: Home Assistant
      message: >-
        Repick meal type was triggerd, new for today:
        {{states("input_text.type_of_meal_random")}}, new recomanded meal is:
        {{states("input_text.meal_random")}}.
    enabled: true
mode: single
icon: mdi:food

random repick just salad:

alias: choose_salad_replick
sequence:
  - if:
      - condition: template
        value_template: >-
          {{ state_attr('input_select.meal_salad_filtered', 'options')  | length
          ==1 }}
    then:
      - if:
          - condition: state
            entity_id: input_select.meal_salad_filtered
            state: RESET
        then:
          - service: input_select.set_options
            data:
              options: ({{ state_attr('input_select.meal_salad_full', 'options')}})
            target:
              entity_id:
                - input_select.meal_salad_filtered
          - service: input_text.set_value
            data:
              value: >-
                {{ state_attr('input_select.meal_salad_filtered', 'options') |
                reject('in', states('input_text.meal_salad_random')) |list
                |random }}
            target:
              entity_id: input_text.meal_salad_random
          - service: input_select.set_options
            data:
              options: >-
                ({% set ns = namespace(list=[]) %} {% for option in
                state_attr("input_select.meal_salad_filtered", "options") if
                option != states("input_text.meal_salad_random") %} {% set
                ns.list = ns.list+[option] %} {%endfor%} {{ns.list}})
            target:
              entity_id: input_select.meal_salad_filtered
            enabled: true
        else:
          - service: input_text.set_value
            data:
              value: >-
                {{ state_attr('input_select.meal_salad_filtered', 'options') |
                reject('in', states('input_text.meal_salad_random')) |list
                |random }}
            target:
              entity_id: input_text.meal_salad_random
          - service: input_select.set_options
            data:
              options: ({{ state_attr('input_select.meal_salad_full', 'options')}})
            target:
              entity_id:
                - input_select.meal_salad_filtered
    else:
      - service: input_text.set_value
        data:
          value: >-
            {{ state_attr('input_select.meal_salad_filtered', 'options') |
            reject('in', states('input_text.meal_salad_random')) |list |random
            }}
        target:
          entity_id: input_text.meal_salad_random
      - service: input_select.set_options
        data:
          options: >-
            ({% set ns = namespace(list=[]) %} {% for option in
            state_attr("input_select.meal_salad_filtered", "options") if option
            != states("input_text.meal_salad_random") %} {% set ns.list =
            ns.list+[option] %} {%endfor%} {{ns.list}})
        target:
          entity_id: input_select.meal_salad_filtered
        enabled: true
  - service: notify.notify_all
    data:
      title: Home Assistant
      message: >-
        Repick salad was triggerd, new for today:
        {{states("input_text.meal_salad_random")}}.
    enabled: true
mode: single
icon: mdi:food

manual operations
because sometimes the random salad do not mix well with the meal I have an "admin panel " to manually add salad, the random salad will be readded to the filtered list and reused for the next days.

Lovelace panel:

type: vertical-stack
cards:
  - type: horizontal-stack
    cards:
      - type: tile
        entity: sensor.uptime
        name: HA last restart
      - type: tile
        entity: counter.days_to_special_meal
        name: Days to Special meal
  - type: tile
    entity: sensor.type_of_meal_remain
    name: Meal type remaining to be recommended
  - type: tile
    entity: sensor.meal_salad_remain
    name: Salad remaining to be recommended
    tap_action:
      action: more-info
    icon_tap_action:
      action: none
    vertical: false
    show_entity_picture: false
  - type: horizontal-stack
    cards:
      - type: entities
        entities:
          - entity: input_select.meal_salad_full
        title: Select Salad
      - type: entities
        entities:
          - entity: script.meal_salad_manual
            secondary_info: none
            name: Manual
        title: Replace for today

Script for the manual salad operations:

alias: meal_salad_manual
sequence:
  - service: input_select.set_options
    data:
      options: >-
        {% set ns = namespace(list=[]) %} {% for option in
        state_attr("input_select.meal_salad_filtered", "options")%} {% set
        ns.list = ns.list+[option] %} {%endfor%} {{ns.list +
        [states("input_text.meal_salad_random")]}} 
    target:
      entity_id: input_select.meal_salad_filtered
  - service: input_text.set_value
    data:
      value: "{{ states('input_select.meal_salad_full')}}"
    target:
      entity_id: input_text.meal_salad_random
  - service: input_select.set_options
    data:
      options: >-
        ({% set ns = namespace(list=[]) %} {% for option in
        state_attr("input_select.meal_salad_filtered", "options") if option !=
        states("input_text.meal_salad_random") %} {% set ns.list =
        ns.list+[option] %} {%endfor%} {{ns.list}})
    target:
      entity_id: input_select.meal_salad_filtered
    enabled: true
mode: single
icon: mdi:leaf-circle

I had to use multiple posts because of the forum limiting characters/post.

do you have github? You can repackage it into one package

For now is still a work in progress, wanted to fix the restart issue before I can think about sharing the project.

for now I have two leads:

  • pyscript as initial described problem
  • Trigger-based template sensor attributes
    Unfortunately struggling with them, no real examples that I can adapt or understand.

You can use a sample sensor of this type to see if it’s feasible. It will read the status when restarting hass

template:

  • trigger:
    • platform: homeassistant
      event: start

    • platform: event
      event_type: event_template_reloaded

I have found a way to do this but it’s a bit long winded, but may still be helpful
Basically each time I add or delete from an input_select I also write the options to a file, then restore the file after restart. Need to use python to write and restore from the file. I use the HACS program Python Scripts Pro to run the python scripting as I find it works best
In case it’s useful to people here’s what I’ve done

  • create a folder input_select_files in the home assistant directory
  • create a sensor that picks up the names of any files in the folder. The names are the names of the input_select entities, and the file contains the options
    sensor:
      - platform: folder
        folder: "/config/input_select_files"

Create the input_select as normal, and create one option called ‘Dummy’. If you use this in an automation you just ignore the Dummy. You need to have one option, otherwise the scripts don’t work if you remove all options
To add or remove an option from the input_select, I’ve got a script that does that and also creates the txt file

To call the script, use a service call with two options, one with the info to add, the other with the input_select entity to add to

  - service: script.add_or_remove_option_from_input_select
    data:
      option_to_add: <<< the option you want to add >>>
      is_to_add_to: <<< the input_select entity to add to >>>

Here’s the script

alias: Add Or Remove Option From Input Select
sequence:
  - variables:
      option_to_add: "{{ option_to_add }}"
      is_to_add_to: "{{ is_to_add_to }}"
      group_list: >
        {% if not states(option_to_add) in state_attr(is_to_add_to, "options")
        %} {% set ns = namespace( members = []) %} 
          {% for entity_id in state_attr(is_to_add_to, "options") | list %} 
            {% set ns.members = ns.members + [entity_id] %} 
          {% endfor %} 
          {% set ns.members = ns.members + [states(option_to_add)] %}
                {{ ns.members }}
        {% else %} {% set ns = namespace( members = []) %} 
          {% for entity_id in state_attr(is_to_add_to, "options") | list %} 
            {% if not states(option_to_add) == entity_id %}
              {% set ns.members = ns.members + [entity_id] %} 
            {% endif %}
          {% endfor %} 
                {{ ns.members }}
        {% endif %}
      add_to_file_list: >
        {% if not states(option_to_add) in state_attr(is_to_add_to, "options")
        %} {% set ns = namespace( members = []) %} 
          {% for entity_id in state_attr(is_to_add_to, "options") | list %} 
            {% set ns.members = ns.members + [{"entity_id": entity_id}] %} 
          {% endfor %} 
          {% set ns.members = ns.members + [{"entity_id": states(option_to_add)}] %}
                {{ ns.members }}
        {% else %} {% set ns = namespace( members = []) %} 
          {% for entity_id in state_attr(is_to_add_to, "options") | list %} 
            {% if not states(option_to_add) == entity_id %}
              {% set ns.members = ns.members + [{"entity_id": entity_id}] %} 
            {% endif %}
          {% endfor %} 
                {{ ns.members }}
        {% endif %}
  - service: input_select.set_options
    metadata: {}
    data:
      options: "{{ group_list }}"
    target:
      entity_id: "{{ is_to_add_to }}"
  - service: script.python_clear_file
    data:
      target_file: "{{ is_to_add_to }}"
      target_folder: input_select_files
  - repeat:
      for_each: "{{ add_to_file_list }}"
      sequence:
        - service: script.python_add_line
          data:
            line_to_add: "{{ repeat.item.entity_id }}"
            target_file: "{{ is_to_add_to }}"
            target_folder: input_select_files

It needs two python scripts to work, one to add a line and one to clear a file
Here are the scripts

alias: Python Add Line
sequence:
  - variables:
      line_to_add: "{{ line_to_add }}"
      target_file: "{{ target_file }}"
      target_folder: "{{target_folder }}"
  - service: python_script.exec
    data_template:
      file: python_script/add_line.py
      cache: false
      filename: "{{ target_file }}"
      foldername: "{{target_folder }}"
      addline: "{{ line_to_add }}"
mode: single

alias: Python Clear File
sequence:
  - variables:
      target_folder: "{{ target_folder }}"
      target_file: "{{ target_file }}"
  - service: python_script.exec
    data_template:
      file: python_script/clear_file.py
      cache: false
      filename: "{{ target_file }}"
      foldername: "{{ target_folder }}"
mode: single

It also needs 2 python programs to work, that the scripts call.
First one is add_line.py

import csv
import requests
import os
from pathlib import Path

logger.debug(data)


txt_file_folder = Path(data['foldername'])
fn = data['filename']

f = open(txt_file_folder / fn , 'a')# opening the file for writing



f.write(data['addline'] + "\n") #writing the content into that file
f.close()

Second one is clear_file.py

import csv
import requests
import os
from pathlib import Path

logger.debug(data)

txt_file_folder = Path(data['foldername'])
fn = data['filename']

with open(txt_file_folder / fn, 'r+') as fp:
    # read an store all lines into list
    lines = fp.readlines()
    # move file pointer to the beginning of a file
    fp.seek(0)
    # truncate the file
    fp.truncate()

    # start writing lines except the last line
    # lines[:-1] from line 0 to the second last line
    fp.writelines(lines[:-lines])

And finally a script to restore after a restart. You need to call this script through an automation after restart or embed it in an automation

alias: Restore Input Select From TxT File Combined
sequence:
  - variables:
      group_list: >
        {% set x = 'sensor.input_select_files' %} {% set ns = namespace( members
        = []) %}  {% for entity_id in state_attr(x, "file_list") | list %} 
          {% set entity_id = entity_id.split("files/")[1] %}
          {% set ns.members = ns.members + [{"entity_id": entity_id}] %} 
        {% endfor %}  {{ ns.members }}    
  - repeat:
      for_each: "{{ group_list }}"
      sequence:
        - variables:
            target_file: "{{ repeat.item.entity_id }}"
            target_folder: input_select_files
        - service: python_script.exec
          data_template:
            file: python_script/read_file.py
            filename: "{{ target_file }}"
            foldername: "{{target_folder }}"
          response_variable: target_output
        - service: input_select.set_options
          metadata: {}
          data:
            options: |
              {% set x = target_output|string %}
              {% set y = x.split("output': ")[1] %}
              {% set y = y.replace("n'","'") %}
              {% set y = y.replace("\\'","'") %}
              {{ y.split("}")[0] }}
          target:
            entity_id: "{{ target_file }}"
mode: single

A bit long winded and I’m sure my code isn’t that efficient but it works well! Hope it’s helpful

Thank you! I will try to adapt it tomorrow and let you know how it worked for me.

Sorry but lost a lot of time on this, dos not work for me. Pyton scripts work as I tested with an example from the repository of “PythonScriptsPro”
the folder sensor is not showing after I added in config yaml acording documentation of folder sensor
Not sure if I need to do something else that is not described in your post.

you are missing the read.py for the read script in the last post so i think your information is incomplete.

hi, sorry for the late reply. Yes, missed the read_file.py script, have attached it below. Sorry

import csv
import requests
import os
from pathlib import Path

logger.debug(data)


txt_file_folder = Path(data['foldername'])
#filename = 'datax' % name
f = open(txt_file_folder / data['filename']  , 'r')# opening the file for writing

#f = open("log_data/log_summary.txt" , 'a')# opening the file for writing

output = f.readlines()
f.close()