Passing a Selected Entity from a Blueprint to a Script

Hello. I have a blueprint (based on the stock motion-activated light blueprint) that calls a script, while passing the entity_id of the target light to turn on:

  - alias: "Turn on the light"
    service: script.xxxxx
      entity: !input light_target

The script, in turn, has several conditionals with differing calls to “light.turn_on”, for example:

    - service: light.turn_on
        color_temp: 400
        brightness: 40
        entity_id: '{{ entity }}'

But I get:

Error executing script. Error for call_service at pos 1: Template rendered invalid entity IDs: {‘entity_id’: ‘light.light_name’}

This seems like a type or syntactic error to me, but I am not sure how to fix this. What do I need to change? Thank you.

It looks like the blueprint is actually using a target selector, not an entity selector.

It is:

      name: Light
            domain: light

Can I not do this?

You can, but you need to change the script to use the target.

If you don’t mind, how would I do that? As above, right now I am passing what I thought to be the selected input to the script.

Retrieve the value from the target dictionary:

    - service: light.turn_on
        color_temp: 400
        brightness: 40
        entity_id: '{{ entity.entity_id }}'

Ah, I see… so it was passing a reference to the entity rather than passing the name itself?

One of the reasons that I have this broken out into a separate script is so that I can also call it from other automations (not based on the blueprint). How would I then change my calls to the script from those if I make this change?


You can do it a bunch of different ways depending on where you want to make the changes

  1. Alter the blueprint to use an entity selector instead of a target selector.

  2. Define a variable in the blueprint to extract the entity ID from the input.

  3. Use conditional logic in your script’s templates to handle both cases: {{ entity.entity_id if entity is mapping else entity }}

Oh, awesome. The third one takes care of it best, IMO.

Thank you for taking the time to respond with your experience; solution selected.