Script to map a number to a label (input_number to input_text)

I’ve been working on a pool light that you set the color by turning the light off/on a certain number of times. I’ve got a button for each color that when you press the button it sets an index_number entity to the corresponding number of off/on loops.

Then i have an automation that gets triggered when this number changes and runs the following code:

action:
  - service: switch.turn_on
    entity_id: switch.pool_light
  - delay: '00:00:03'
  - condition: template
    value_template: '{{ trigger.to_state.state | int > 0 }}'
  - service: switch.turn_off
    entity_id: switch.pool_light
  - delay: '00:00:02'
  - service: switch.turn_on
    entity_id: switch.pool_light
  - delay: '00:00:02'
  - condition: template
    value_template: '{{ trigger.to_state.state | int > 1 }}'
  - service: switch.turn_off
    entity_id: switch.pool_light
  - delay: '00:00:02'
....
...
..

Saw this from another poster how they did it - i’m sure there is a better way with loops, etc. but this works nicely enough.

What I can’t for the life of me figure out is how to set the corresponding input_text field with the color name that goes along with the input_number. I’m going to use that color name in the label to tell me what the current color is.

In a perfect world i’d have some sort of array with numbers and the matching text and just set that when i press my button. So i can set the input-text field when i press the button, when the automation gets triggered, or something else.

Any ideas how to accomplish this? I’m still learning how the templates work and when to use {{ and when to use {$%- is still a bit of a mystery to me…

Post all of the automations and entities you have for this application. It’ll be easier to understand all of the pieces of this puzzle and how to fit them together to get what you ultimately want.

Well i figured it out and here is the code in case someone else in the future is trying to do something similar:

On my dashboard i have a button:

- type: button
        tap_action:
          action: call-service
          service: input_number.set_value
          service_data:
            value: 6
          target:
            entity_id: input_number.pool_number
        entity: switch.pool_light
        name: Sunset
        icon: 'mdi:weather-sunset'

When that is pressed it changes the state of my “input_number” entity called “pool_number”. Then I have an automation that gets triggered on any state change of the pool_number entity.

trigger:
  - platform: state
    entity_id: input_number.pool_number

Then the automation performs an action to turn the light switch on/off a certain number of times using the value of “pool_number”. I have a section of code for each number starting from 1 to 12. First thing i do is set the name of the color into an input_text entity called “pool_color”

action:
  - service: input_text.set_value
    target:
      entity_id: input_text.pool_color
    data:
      value: All Colors

Then i turn the switch on/off the first time…

- condition: template
    value_template: '{{ trigger.to_state.state | int > 1 }}'
  - service: switch.turn_off
    entity_id: switch.pool_light
  - delay: '00:00:02'
  - service: switch.turn_on
    entity_id: switch.pool_light
  - delay: '00:00:02'

Then i do this again but I only do it if the pool_number is greater than 2…then again if its greater than 3…up to 12. The color text value changes as it does each on/off but thats ok - once it gets to the number of times the final color is set.

Lastly i have custom button card that represents the Pool Light itself. I used the following to grab the name of the color from the input_text.pool_color entity to tell me what the current color is.

 - type: 'custom:button-card'
        color_type: icon
        entity: switch.pool_light
        label: |
          [[[
            return states['input_text.pool_color'].state 
          ]]]
        show_label: true
        size: 15%
        styles:
          card:
            - height: 100px

I’m sure there is a cleaner way to do this - but this way works and i’m happy to keep it this way. If there was a way to pass the color name from the button to the automation that would make this even easier. Or, if this was a programming language like i’m used to i’d call a script passing the number and the script would return the color name. That way i maintain that mapping in once place.

This is for a Pentair Intellibrite 5g pool light btw in case someone is looking to do this. Happy to share the full yaml if someone is interested.

What I had in mind is the button calls a script and passes it the number of times to toggle the light (input_number entity is no longer required). The script uses a repeat to neatly toggle the light the desired number of times. The input_text is set to the appropriate color after the repeat has completed.

However, you already have a Solution so my alternative is moot.