Need help with script and templating

Hi, I got stuck and unable to resolve the issue on my own, many thanks in advance for any help, much appreciated.
I created a script to toggle or turn on light but I cannot turn off light with this script because when I use light.turn_off for the {{ lightmode }} brightness_pct should be omitted but I cannot add if statement. I tried many things but most of the time I get error :“Error rendering data template: Result is not a Dictionary”

script that works :

alias: lights_day90_night10
sequence:
  - choose: []
    default:
      - service: "{{ lightmode }}"
	    data:
          brightness_pct: 100
        target:
          entity_id: "{{ entity }}"
mode: queued
max: 8

this is the kind of script I need but cannot figure out how to do:

alias: lights_day90_night10
sequence:
  - choose: []
    default:
      - service: "{{ lightmode }}"
        data: >-
          {% if lightmode != 'light.turn_off' %} 
            brightness_pct: 10 
          {% else %}
            none 
          {% endif %}
        target:
          entity_id: "{{ entity }}"
mode: queued
max: 8
alias: lights_day90_night10
sequence:
  - if:
      - "{{ lightmode == 'light.turn_off' }}"
    then:
      - service: light.turn_off
        target:
          entity_id: {{ entity }}
    else:
      - service: light.turn_on
        target:
          entity_id: "{{ entity }}"
        data:
          brightness_pct: 10
mode: queued
max: 8

I’m assuming you don’t ever use light.toggle as the light mode.

Just pass on or off to the lightmode variable.

alias: lights_day90_night10
sequence:
  - service: light.turn_on
    data: 
      brightness_pct: "{{ 10 if lightmode == 'on' else 0 }}"
    target:
      entity_id: "{{ entity }}"
mode: queued
max: 8

Thanks tom_I and Taras
your code helped me move in the right direction and both of your solutions worked.
Think that your solution tom_I is more flexible since it allows for more control with ifs due to some HA limitations.
I had some time to thinker with the script and this is what I got in the end to do what i need for the moment, turn on lights different brightness at night and day with same script and allow for flexibility to exclude some lights. Here is the final script:

alias: lights_day90_night10
sequence:
  - choose:
      - conditions:
          - condition: time
            after: "21:00:00"
            before: "07:30:00"
        sequence:
          - service: "{{ lightmode }}"
            data: |
              {% if lightmode == 'light.turn_off' %}
                { }
              {% elif entity == 'light.kitchen1' %}
                {
                 "transition": "1",
                 "brightness_pct": "21"               
                }
              {% else %}
                {
                 "transition": "1",
                 "brightness_pct": "10"
                }
              {% endif %} 
            target:
              entity_id: "{{ entity }}"
    default:
      - service: "{{ lightmode }}"
        data: |
          {% if lightmode != 'light.turn_off' %}
            {
             "transition": "1",
             "brightness_pct": "90"
            }
          {% else %}
            {}
          {% endif %}
        target:
          entity_id: "{{ entity }}"
mode: parallel
max: 24

Once again many thanks for help !

You’re welcome and I appreciate your comment but, honestly, your latest example appears to contain little of what I had suggested.

  1. There’s no need to pass the entire service call in lightmode. The script is already constrained to using a light-based service call due to its use of brightness_pct. The value of lightmode only needs to be either on or off.

  2. Similarly, there’s no need for entity to contain the domain (light) because the script is limited to light-based entities. The value of entity only needs to contain the light’s object_id (i.e. object_id is everything to the right of the period in light.living_room).

  3. Your script basically sets a light’s brightness to either 0, 10, 21, or 90 based on the time and/or if it’s the kitchen light. All of that can be templated in a variable and passed to a single service call

In the following example, all of the “thinking” is performed by the bp variable. If lightmode is off then the value is set to zero. Otherwise, if the current time is not within the specified range, the value is 90. Otherwise it’s 21 if it’s the kitchen light or 10 for any other light.

alias: lights_day90_night10
sequence:
  - variables:
      in_range: "{{ (now().hour, now().minute) < (7, 30) or now().hour >= 21 }}"
      bp: "{{ 0 if lightmode == 'off' else 90 if not in_range else 21 if entity == 'kitchen1' else 10 }}"
  - service: light.turn_on
    target:
      entity_id: "{{ entity }}"
    data:
      brightness_pct: "{{ bp }}"
      transition: 1
mode: parallel
max: 24

Hi Taras
You are right I have not used exactly your code , but your example helped me to move from dead end sort of speak.
Thank you for the latest script, I will examine and test it over the weekends and have some feedback.