How to script a choice

I have 8 choices ( radio frequencies).
People can choose 4 out of those 8.
So you only can check 4. How do I create a script where only 4 can be checked, and when a fift one is checked the first checked will be unchecked ?

afbeelding

what I have done so far…

made 8 tap actions to pass options to script

    tap_action:
      action: call-service
      service: script.persoon1_keuze_muziek
      service_data:
        option: "pop"

made 8 input.booleans

      icon: >
        {% if is_state("input_boolean.persoon1_muziekkeuze_8", 'on') %}
        mdi:toggle-switch
        {% else %}
        mdi:toggle-switch-off 
        {% endif %}

in the script … choose between 8 options

choose:
  - conditions:
      - condition: template
        value_template: "{{option == 'radio1'}}"
    sequence: []
  - conditions:
      - condition: template
        value_template: "{{option == 'radio2'}}"
    sequence: []
  - conditions:
      - condition: template
        value_template: "{{option == 'radiowilly'}}"
...

maybe I have to make a helper counter ? but then ?

At the end of your script you could do something like this to check if there are more than 4 selected and if so to turn off the one selected least recently. I’ve assumed you’ve put a label called stations on all of your input booleans.

  - if:
      - condition: template
        value_template: >
          {{ expand(label_entities('stations')) | selectattr('state','eq','on') | list | count > 4 }}
    then:
      - service: input_boolean.turn_off
        metadata: {}
        data: {}
        target:
          entity_id: >
            {{ expand(label_entities('stations')) | selectattr('state','eq', 'on') | sort(attribute="last_changed",reverse=true)  | map(attribute='entity_id') | first}}
1 Like

for those who are interested… this is my script

alias: persoon1_keuze_muziek
sequence:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{option == 'radio1'}}"
        sequence:
          - if:
              - condition: state
                entity_id: input_boolean.persoon1_muziekkeuze_1
                state: "on"
            then:
              - service: input_boolean.turn_off
                metadata: {}
                data: {}
                target:
                  entity_id: input_boolean.persoon1_muziekkeuze_1
            else:
              - service: input_boolean.turn_on
                metadata: {}
                data: {}
                target:
                  entity_id: input_boolean.persoon1_muziekkeuze_1
          - if:
              - condition: template
                value_template: >
                  {{ expand(label_entities('stations')) |
                  selectattr('state','eq','on') | list | count > 4 }}
            then:
              - service: input_boolean.turn_off
                metadata: {}
                data: {}
                target:
                  entity_id: >
                    {{ expand(label_entities('stations')) |
                    selectattr('state','eq', 'on') |
                    sort(attribute="last_changed",reverse=true)  |
                    map(attribute='entity_id') | first}}
      - conditions:
          - condition: template
            value_template: "{{option == 'radio2'}}"
        sequence:
          - if:
              - condition: state
                entity_id: input_boolean.persoon1_muziekkeuze_2
                state: "on"
            then:
              - service: input_boolean.turn_off
                metadata: {}
                data: {}
                target:
                  entity_id: input_boolean.persoon1_muziekkeuze_2
            else:
              - service: input_boolean.turn_on
                metadata: {}
                data: {}
                target:
                  entity_id: input_boolean.persoon1_muziekkeuze_2
          - if:
              - condition: template
                value_template: >
                  {{ expand(label_entities('stations')) |
                  selectattr('state','eq','on') | list | count > 4 }}
            then:
              - service: input_boolean.turn_off
                metadata: {}
                data: {}
                target:
                  entity_id: >
                    {{ expand(label_entities('stations')) |
                    selectattr('state','eq', 'on') |
                    sort(attribute="last_changed",reverse=true)  |
                    map(attribute='entity_id') | first}}
      - conditions:
          - condition: template
            value_template: "{{option == 'radiowilly'}}"
        sequence:
          - if:
              - condition: state
                entity_id: input_boolean.persoon1_muziekkeuze_3
                state: "on"
            then:
              - service: input_boolean.turn_off
                metadata: {}
                data: {}
                target:
                  entity_id: input_boolean.persoon1_muziekkeuze_3
            else:
              - service: input_boolean.turn_on
                metadata: {}
                data: {}
                target:
                  entity_id: input_boolean.persoon1_muziekkeuze_3
          - if:
              - condition: template
                value_template: >
                  {{ expand(label_entities('stations')) |
                  selectattr('state','eq','on') | list | count > 4 }}
            then:
              - service: input_boolean.turn_off
                metadata: {}
                data: {}
                target:
                  entity_id: >
                    {{ expand(label_entities('stations')) |
                    selectattr('state','eq', 'on') |
                    sort(attribute="last_changed",reverse=true)  |
                    map(attribute='entity_id') | first}}
      - conditions:
          - condition: template
            value_template: "{{option == 'stubru'}}"
        sequence:
          - if:
              - condition: state
                entity_id: input_boolean.persoon1_muziekkeuze_4
                state: "on"
            then:
              - service: input_boolean.turn_off
                metadata: {}
                data: {}
                target:
                  entity_id: input_boolean.persoon1_muziekkeuze_4
            else:
              - service: input_boolean.turn_on
                metadata: {}
                data: {}
                target:
                  entity_id: input_boolean.persoon1_muziekkeuze_4
          - if:
              - condition: template
                value_template: >
                  {{ expand(label_entities('stations')) |
                  selectattr('state','eq','on') | list | count > 4 }}
            then:
              - service: input_boolean.turn_off
                metadata: {}
                data: {}
                target:
                  entity_id: >
                    {{ expand(label_entities('stations')) |
                    selectattr('state','eq', 'on') |
                    sort(attribute="last_changed",reverse=true)  |
                    map(attribute='entity_id') | first}}
      - conditions:
          - condition: template
            value_template: "{{option == 'topradio'}}"
        sequence:
          - if:
              - condition: state
                entity_id: input_boolean.persoon1_muziekkeuze_5
                state: "on"
            then:
              - service: input_boolean.turn_off
                metadata: {}
                data: {}
                target:
                  entity_id: input_boolean.persoon1_muziekkeuze_5
            else:
              - service: input_boolean.turn_on
                metadata: {}
                data: {}
                target:
                  entity_id: input_boolean.persoon1_muziekkeuze_5
          - if:
              - condition: template
                value_template: >
                  {{ expand(label_entities('stations')) |
                  selectattr('state','eq','on') | list | count > 4 }}
            then:
              - service: input_boolean.turn_off
                metadata: {}
                data: {}
                target:
                  entity_id: >
                    {{ expand(label_entities('stations')) |
                    selectattr('state','eq', 'on') |
                    sort(attribute="last_changed",reverse=true)  |
                    map(attribute='entity_id') | first}}
      - conditions:
          - condition: template
            value_template: "{{option == 'dance'}}"
        sequence:
          - if:
              - condition: state
                entity_id: input_boolean.persoon1_muziekkeuze_6
                state: "on"
            then:
              - service: input_boolean.turn_off
                metadata: {}
                data: {}
                target:
                  entity_id: input_boolean.persoon1_muziekkeuze_6
            else:
              - service: input_boolean.turn_on
                metadata: {}
                data: {}
                target:
                  entity_id: input_boolean.persoon1_muziekkeuze_6
          - if:
              - condition: template
                value_template: >
                  {{ expand(label_entities('stations')) |
                  selectattr('state','eq','on') | list | count > 4 }}
            then:
              - service: input_boolean.turn_off
                metadata: {}
                data: {}
                target:
                  entity_id: >
                    {{ expand(label_entities('stations')) |
                    selectattr('state','eq', 'on') |
                    sort(attribute="last_changed",reverse=true)  |
                    map(attribute='entity_id') | first}}
      - conditions:
          - condition: template
            value_template: "{{option == 'easy'}}"
        sequence:
          - if:
              - condition: state
                entity_id: input_boolean.persoon1_muziekkeuze_7
                state: "on"
            then:
              - service: input_boolean.turn_off
                metadata: {}
                data: {}
                target:
                  entity_id: input_boolean.persoon1_muziekkeuze_7
            else:
              - service: input_boolean.turn_on
                metadata: {}
                data: {}
                target:
                  entity_id: input_boolean.persoon1_muziekkeuze_7
          - if:
              - condition: template
                value_template: >
                  {{ expand(label_entities('stations')) |
                  selectattr('state','eq','on') | list | count > 4 }}
            then:
              - service: input_boolean.turn_off
                metadata: {}
                data: {}
                target:
                  entity_id: >
                    {{ expand(label_entities('stations')) |
                    selectattr('state','eq', 'on') |
                    sort(attribute="last_changed",reverse=true)  |
                    map(attribute='entity_id') | first}}
      - conditions:
          - condition: template
            value_template: "{{option == 'pop'}}"
        sequence:
          - if:
              - condition: state
                entity_id: input_boolean.persoon1_muziekkeuze_8
                state: "on"
            then:
              - service: input_boolean.turn_off
                metadata: {}
                data: {}
                target:
                  entity_id: input_boolean.persoon1_muziekkeuze_8
            else:
              - service: input_boolean.turn_on
                metadata: {}
                data: {}
                target:
                  entity_id: input_boolean.persoon1_muziekkeuze_8
          - if:
              - condition: template
                value_template: >
                  {{ expand(label_entities('stations')) |
                  selectattr('state','eq','on') | list | count > 4 }}
            then:
              - service: input_boolean.turn_off
                metadata: {}
                data: {}
                target:
                  entity_id: >
                    {{ expand(label_entities('stations')) |
                    selectattr('state','eq', 'on') |
                    sort(attribute="last_changed",reverse=true)  |
                    map(attribute='entity_id') | first}}
description: ""
fields:
  option:
    name: option

So, I’ve made it possible ( with some help) to select max 4 radio stations.
Now I want those 4 radio stations in the box below. How do I achieve this ?

this is part of the code of the box on top…
this is the code from 1 button

choose:
  - conditions:
      - condition: template
        value_template: "{{option == 'radio1'}}"
    sequence:
      - if:
          - condition: state
            entity_id: input_boolean.persoon1_muziekkeuze_1
            state: "on"
        then:
          - service: input_boolean.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: input_boolean.persoon1_muziekkeuze_1
        else:
          - service: input_boolean.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: input_boolean.persoon1_muziekkeuze_1
      - if:
          - condition: template
            value_template: >
              {{ expand(label_entities('stations')) |
              selectattr('state','eq','on') | list | count > 4 }}
        then:
          - service: input_boolean.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: >
                {{ expand(label_entities('stations')) | selectattr('state','eq',
                'on') | sort(attribute="last_changed",reverse=true)  |
                map(attribute='entity_id') | first}}
1 Like

@atlflyer - thats a really neat solution. I was imaging having to create some kind of dynamic array using a text helper, but this is super simple.