Error when using if...else to select service target in motion triggered lights automation

Hello HA experts,

I would like to implement and expand one of the most classic automations ever: activating the lights through a motion sensor.
I am using Danielbook’s blueprint as a basis for this. To increase the acceptance of HA automations with my girlfriend, only one lamp in the hallway (flur) should be turned on at night and all three lamps during the day.
To achieve this, I have extended Daniel’s blueprint with an additional “light input” (light_target_night) and declared variables for the inputs. At night, “light_target_night” should now be used instead of “light_target” for the “light.turn_off” and “light.turn_on” services.
I thought I could achieve this most easily by implementing the selection of the service target through an if…else condition:

- service: light.turn_off
  target: >
    {% if today_at(states(local_night_end)) < now() or now() < today_at(states(local_night_start))  %}
      {{ local_light }}
    {% else %}
      {{ local_light_night }}
    {% endif %} 
  data:
    transition: !input dim_time

Unfortunately, this implementation leads to the following error message when the automation runs (trace timeline):

Error in describing action: Cannot use ‘in’ operator to search for ‘area_id’ in {% if today_at(states(local_night_end)) < now() or now() < today_at(states(local_night_start)) %} {{ local_light }} {% else %} {{ local_light_night }} {% endif %}

All lights in the area or Zigbee group (entity) are always turned on, regardless of the time. I am surprised by this because the else branch does not refer to the Zigbee group/area, but to the single light entity.
My automation is set up as follows:

alias: Motion (Flur) night-lights
description: ""
use_blueprint:
  path: AlexL00/motion-illuminance-dimmable-nightmode_night_lights.yaml
  input:
    motion_entity: binary_sensor.signify_netherlands_b_v_sml003_opening_3
    lux_entity: sensor.signify_netherlands_b_v_sml003_illuminance_3
    lux_level: 1000
    light_target:
      entity_id: light.flur_decke
    light_target_night:
      entity_id: light.deckenlicht_3_flur
    dim_time: 5
    no_motion_wait_day: 60
    no_motion_wait_night: 30
    night_time_start_helper: input_datetime.night_start
    night_time_end_helper: input_datetime.day_start

What am I doing wrong? Where does this error message come from and what does it mean?

Thank you very much for the hint! It makes sense! I’ll test it.

So I guess in Daniels blueprint this works because the direct !input of the target was used instead of a variable so the “context” (area/group/entity) gets delivered with it and gets lost when writing the !input into a variable?

What if I don’t use the variables and use the direct !input again?

In the end there will be three automations using this blueprint. Why?

Okay I tried this. There is a diffrent error now:
Error: Template rendered invalid entity IDs: {'entity_id': 'light.flur_decke'}
‘light.flur_decke’ is the Zigbee group entity.

Is there maybe a problem with the variables?

Blueprint Input:

    light_target:
      name: Light
      selector:
        target:
          entity:
          - domain:
            - light
    light_target_night:
      name: Light (night)
      selector:
        target:
          entity:
          - domain:
            - light

Blueprint variable declaration:

variables:
  local_night_end: !input night_time_end_helper
  local_night_start: !input night_time_start_helper
  local_motion_delay_day: !input no_motion_wait_day
  local_motion_delay_night: !input no_motion_wait_night
  local_motion_dim_time: !input dim_time
  local_light: !input light_target
  local_light_night: !input light_target_night

blueprint service call:

- service: light.turn_off
  target: 
    entity_id: >
      {% if today_at(states(local_night_end)) < now() or now() < today_at(states(local_night_start))  %}
        {{ local_light }}
      {% else %}
        {{ local_light_night }}
      {% endif %} 
  data:
    transition: !input dim_time

complete YAML see first post - I do commit there.

This is the automation now:

alias: Motion-activated Light (Flur)
description: ""
use_blueprint:
  path: AlexL00/motion-illuminance-dimmable-nightmode-different-lights.yaml
  input:
    motion_entity: binary_sensor.signify_netherlands_b_v_sml003_opening_3
    lux_entity: sensor.signify_netherlands_b_v_sml003_illuminance_3
    lux_level: 1000
    light_target:
      entity_id: light.flur_decke
    light_target_night:
      entity_id: light.deckenlicht_3_flur
    dim_time: 10
    no_motion_wait_day: 60
    no_motion_wait_night: 20
    night_time_start_helper: input_datetime.night_start
    night_time_end_helper: input_datetime.day_start

Mh am I doing something (obvious) wrong or why is no one responding?

I’ve solved this!
Regarding to this issue I can’t use templating with the lights.turn_on service.

So instead of using templating to select the entity_id / device_id / area_id to be used by the service I had to re-write the whole action-part of the blueprint:

action:
  - if:
      - condition: or
        conditions:
          - condition: template
            #evaluates to true if now() is between helper entity "Start of Day/End of night" and  "End of Day/Begin of night"
            value_template: >-
              {{ today_at(states(local_night_end)) < now() and now() <
              today_at(states(local_night_start)) }}
    #this happens during the day
    then:
      - service: light.turn_on
        target: !input light_target
      - wait_for_trigger:
          platform: state
          entity_id: !input motion_entity
          from: 'on'
- delay:
          seconds: "{{ local_motion_delay_day }} - {{ local_motion_dim_time }}"
      - service: light.turn_off
        data: {transition: !input dim_time}
        target: !input light_target
    #this happens during the night
    else:
      - service: light.turn_on
        target: !input light_target_night
      - wait_for_trigger:
          platform: state
          entity_id: !input motion_entity
          from: 'on'
      - delay:
          seconds: "{{ local_motion_delay_night }} - {{ local_motion_dim_time}}"
      - service: light.turn_off
        data: {transition: !input dim_time}
        target: !input light_target_night

The link to the blueprint gist can still be useful if someone has a similar problem to solve.