Feedback wanted for "flash lights" script

I’m looking for feedback on how I can improve my script, or perhaps best practices I’ve not followed or tricks I’ve missed.

Basically how can I improve it.

What it does (or tries to)

  1. Store a scene of lights that are on
  2. Turn a set of lights on to a certain colour based on red, green, blue
  3. Wait a time period based on the dutycycle (e.g. 10% of the time)
  4. Turn the same set of lights off
  5. Wait a time period based on the dutycycle (e.g. 90% of the time)
  6. Goto 2 for Count times
  7. Finally restore the scene stored in 1

A couple of issues/limitations

  • Haven’t done anything about white only lights yet
  • Can’t work out how to store state for only the target selector lights
  • There is no colour selector
  • Running script from the edit script doesn’t obey any defaults - the developer tools run functional would be great here
alias: Flash light
sequence:
  - variables:
      _red: '{{ red | default(128) }}'
      _green: '{{ green | default(128) }}'
      _blue: '{{ blue | default(128) }}'
      _onFor: |-
        {% if (duration | default(0)) > 0 and (dutycycle | default(0)) > 0 %}
          {{ (duration * (dutycycle / 100)) | int }}
        {% else %}
          500
        {% endif %}
      _offFor: |-
        {% if (duration | default(0)) > 0 and (dutycycle | default(0)) > 0  %}
          {{ (duration * ((100 - dutycycle) / 100)) | int }}
        {% else %}
          500
        {% endif %}
  - service: scene.create
    data:
      scene_id: temp_flash_light
      snapshot_entities: >-
        {{ states.light | selectattr('state', 'eq', 'on') |
        map(attribute='entity_id') | join(',') }}
  - repeat:
      count: '{{ count | default(5) }}'
      sequence:
        - service: light.turn_on
          target: '{{ lights }}'
          data:
            rgb_color:
              - '{{ _red }}'
              - '{{ _green }}'
              - '{{ _blue }}'
            brightness: '{{ max(_red, _green, _blue) }}'
        - delay:
            hours: 0
            minutes: 0
            seconds: 0
            milliseconds: '{{ _onFor }}'
        - service: light.turn_off
          target: '{{ lights }}'
        - delay:
            hours: 0
            minutes: 0
            seconds: 0
            milliseconds: '{{ _offFor }}'
  - service: scene.turn_on
    target:
      entity_id: scene.temp_flash_light
    data: {}
fields:
  lights:
    description: Lights
    required: true
    selector:
      target:
        entity:
          domain: light
  red:
    description: Red
    default: 128
    example: 255
    selector:
      number:
        min: 0
        max: 255
  green:
    description: Green
    default: 128
    example: 0
    selector:
      number:
        min: 0
        max: 255
  blue:
    description: Blue
    default: 128
    example: 0
    selector:
      number:
        min: 0
        max: 255
  count:
    description: Number of cycles
    default: 5
    example: 3
    selector:
      number:
        min: 1
        max: 10
  duration:
    description: Duration of each cycle in ms
    default: 1000
    example: 2000
    selector:
      number:
        min: 1
        max: 30000
  dutycycle:
    description: Duty cycle, percentage of time light is on for in each cycle
    default: 50
    example: 10
    selector:
      number:
        min: 1
        max: 99
mode: queued
max: 10